Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int, int>, int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// コンビネーションを計算する関数
ll modpow(ll a, ll n, ll mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cout << fixed << setprecision(15);
  ll N;
  string X;
  cin >> N;
  cin >> X;

  // 10進数→2進数-----------------------------------
  vector<ll> Binary(N);
  ll count = 0;
  REP(i, N) {
    Binary[i] = X[N - i - 1] - '0';
    if (Binary[i] == 1) {
      count++;
    }
  }

  vector<ll> P(N, 0);
  vector<ll> M(N, 0);
  REP(i, N) {
    P[i] = modpow(2, i, count + 1);
    if (count - 1 == 0) {
      M[i] = 0;
    } else {
      M[i] = modpow(2, i, count - 1);
    }
  }

  ll ptotal = 0;
  ll mtotal = 0;
  REP(i, N) {
    // cout << "P:" << P[i] << " M:" << M[i] << endl;
    if (Binary[i] == 1) {
      ptotal += P[i];
      mtotal += M[i];
    }
  }

  // N以下の数が来たときの計算を事前にやっておく
  vector<ll> A(N + 1, 0);
  for (ll i = 1; i <= N; i++) {
    ll temp = i;
    // cout << "temp:" << temp << endl;
    ll num = 0;
    while (temp > 0) {
      // vector<ll> Binary;
      ll bina = temp;
      ll one = 0;
      for (int j = 0; bina > 0; j++) {
        // Binary.push_back(bina%2);
        if (bina % 2 == 1)
          one++;
        bina = bina / 2;
      }
      temp = temp % one;
      num++;
    }
    // cout << "i:" << i << " num:" << num << endl;
    A[i] = num;
  }

  // for(ll i=1; i<=N; i++){
  //     cout << i << " : " << A[i] << endl;
  // }

  for (ll i = N - 1; i >= 0; i--) {
    if (Binary[i] == 1) {
      if (count - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      ll m = (mtotal - M[i] + (count - 1)) % (count - 1);
      // cout << "m:" << m << " " << A[m]+1 << endl;
      cout << A[m] + 1 << endl;
    } else {
      ll p = (ptotal + P[i] + (count + 1)) % (count + 1);
      // cout << "p:" << p << " " <<A[p]+1 << endl;
      cout << A[p] + 1 << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int, int>, int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// コンビネーションを計算する関数
ll modpow(ll a, ll n, ll mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cout << fixed << setprecision(15);
  ll N;
  string X;
  cin >> N;
  cin >> X;

  // 10進数→2進数-----------------------------------
  vector<ll> Binary(N);
  ll count = 0;
  REP(i, N) {
    Binary[i] = X[N - i - 1] - '0';
    if (Binary[i] == 1) {
      count++;
    }
  }

  vector<ll> P(N, 0);
  vector<ll> M(N, 0);
  REP(i, N) {
    P[i] = modpow(2, i, count + 1);
    M[i] = modpow(2, i, count - 1);
  }

  ll ptotal = 0;
  ll mtotal = 0;
  REP(i, N) {
    // cout << "P:" << P[i] << " M:" << M[i] << endl;
    if (Binary[i] == 1) {
      ptotal += P[i];
      mtotal += M[i];
    }
  }

  // N以下の数が来たときの計算を事前にやっておく
  vector<ll> A(N + 1, 0);
  for (ll i = 1; i <= N; i++) {
    ll temp = i;
    // cout << "temp:" << temp << endl;
    ll num = 0;
    while (temp > 0) {
      // vector<ll> Binary;
      ll bina = temp;
      ll one = 0;
      for (int j = 0; bina > 0; j++) {
        // Binary.push_back(bina%2);
        if (bina % 2 == 1)
          one++;
        bina = bina / 2;
      }
      temp = temp % one;
      num++;
    }
    // cout << "i:" << i << " num:" << num << endl;
    A[i] = num;
  }

  // for(ll i=1; i<=N; i++){
  //     cout << i << " : " << A[i] << endl;
  // }

  for (ll i = N - 1; i >= 0; i--) {
    if (Binary[i] == 1) {
      ll m = (mtotal - M[i] + (count - 1)) % (count - 1);
      // cout << "m:" << m << " " << A[m]+1 << endl;
      cout << A[m] + 1 << endl;
    } else {
      ll p = (ptotal + P[i] + (count + 1)) % (count + 1);
      // cout << "p:" << p << " " <<A[p]+1 << endl;
      cout << A[p] + 1 << endl;
    }
  }

  return 0;
}