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

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  rep(i, N) if (X[i] == '1') cnt++;
  if (cnt == 0) {
    rep(i, N) cout << 1 << endl;
    return 0;
  }

  int pow2 = 1;
  int pow2mod[N][2];
  rep(i, N) {
    rep(j, 2) {
      int MOD = cnt + 2 * j - 1;
      if (MOD == 0) {
        pow2mod[i][j] = 0;
        continue;
      }
      if (i == 0) {
        pow2mod[0][j] = pow2 % MOD;
        continue;
      }
      pow2mod[i][j] = (pow2mod[i - 1][j] * 2) % MOD;
      // cout << pow2mod[i][j] << " " << pow_mod(2,i,MOD) << endl;
    }
  }

  int Xint_mod[2] = {0, 0};
  rep(i, N) {
    rep(j, 2) {
      int MOD = cnt + 2 * j - 1;
      if (MOD == 0) {
        Xint_mod[j] = 0;
        continue;
      }
      if (X[i] == '1')
        (Xint_mod[j] += pow2mod[N - 1 - i][j]) %= MOD;
    }
  }

  vector<int> f(200009, 0);
  f[0] = 0;
  for (int i = 1; i <= 200008; i++) {
    bitset<30> s(i);
    int p = s.count();
    f[i] = f[i % p] + 1;
  }

  /*
  rep(i,30){
    cout << i << " " << f[i] << endl;
  }
  */

  rep(i, N) {
    if (X[i] == '1') { // j=0に相当
      int MOD = cnt - 1;
      if (MOD == 0) {
        cout << 0 << endl;
        continue;
      }
      ll tmp = (Xint_mod[0] - pow2mod[N - 1 - i][0] + MOD) % MOD;
      ll ans = f[tmp] + 1;
      cout << ans << endl;
    } else { // j=1に相当
      int MOD = cnt + 1;
      ll tmp = (Xint_mod[1] + pow2mod[N - 1 - i][1] + MOD) % MOD;
      ll ans = f[tmp] + 1;
      cout << ans << endl;
    }
  }

  // cout << fixed << setprecision(10);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

ll pow_mod(ll a, ll n, ll MOD) {
  ll m[60];
  m[0] = a; // m[i] : a^(2^i)をMODで割った余り
  rep(i, 59) m[i + 1] = (m[i] * m[i]) % MOD;

  ll retval = 1;
  bitset<60> s(n);
  rep(i, 60) if (s.test(i))(retval *= m[i]) %= MOD;

  return retval;
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  rep(i, N) if (X[i] == '1') cnt++;
  if (cnt == 0) {
    rep(i, N) cout << 0 << endl;
    return 0;
  }

  int pow2 = 1;
  int pow2mod[N][2];
  rep(i, N) {
    rep(j, 2) {
      int MOD = cnt + 2 * j - 1;
      if (i == 0) {
        pow2mod[0][j] = pow2 % MOD;
        continue;
      }
      pow2mod[i][j] = (pow2mod[i - 1][j] * 2) % MOD;
      // cout << pow2mod[i][j] << " " << pow_mod(2,i,MOD) << endl;
    }
  }

  int Xint_mod[2] = {0, 0};
  rep(i, N) {
    rep(j, 2) {
      int MOD = cnt + 2 * j - 1;
      if (X[i] == '1')
        (Xint_mod[j] += pow2mod[N - 1 - i][j]) %= MOD;
    }
  }

  vector<int> f(200009, 0);
  f[0] = 0;
  for (int i = 1; i <= 200008; i++) {
    bitset<30> s(i);
    int p = s.count();
    f[i] = f[i % p] + 1;
  }

  /*
  rep(i,30){
    cout << i << " " << f[i] << endl;
  }
  */

  rep(i, N) {
    if (X[i] == '1') { // j=0に相当
      int MOD = cnt - 1;
      ll tmp = (Xint_mod[0] - pow2mod[N - 1 - i][0] + MOD) % MOD;
      ll ans = f[tmp] + 1;
      cout << ans << endl;
    } else { // j=1に相当
      int MOD = cnt + 1;
      ll tmp = (Xint_mod[1] + pow2mod[N - 1 - i][1] + MOD) % MOD;
      ll ans = f[tmp] + 1;
      cout << ans << endl;
    }
  }

  // cout << fixed << setprecision(10);

  return 0;
}