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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define PRINT(x) cout << (x) << endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7
#define PB push_back
#define MP make_pair

int n;
string x;

int popcount(int);
int f(int);

int main() {
  cin >> n >> x;

  int popcount_x = 0;
  REP(i, n) if (x[i] == '1') popcount_x++;
  int pcx0 = popcount_x + 1;
  int pcx1 = popcount_x - 1;

  vector<int> pow2i_mod_pcx0(n);
  pow2i_mod_pcx0[0] = 1;
  REP2(i, 1, n) pow2i_mod_pcx0[i] = pow2i_mod_pcx0[i - 1] * 2 % pcx0;

  int x_mod_pcx0 = 0;
  REP(i, n)
  if (x[i] == '1') x_mod_pcx0 = (x_mod_pcx0 + pow2i_mod_pcx0[n - i - 1]) % pcx0;

  if (popcount_x == 0)
    REP(i, n) PRINT(1);

  else if (popcount_x == 1)
    REP(i, n) {
      if (x[i] == '1')
        PRINT(0);
      else
        PRINT(f((x_mod_pcx0 + pow2i_mod_pcx0[n - i - 1]) % pcx0));
    }

  else {
    vector<int> pow2i_mod_pcx1(n);
    pow2i_mod_pcx1[0] = 1;
    REP2(i, 1, n) pow2i_mod_pcx1[i] = pow2i_mod_pcx1[i - 1] * 2 % pcx1;

    int x_mod_pcx1 = 0;
    REP(i, n)
    if (x[i] == '1') x_mod_pcx1 =
        (x_mod_pcx1 + pow2i_mod_pcx1[n - i - 1]) % pcx1;

    REP(i, n) {
      if (x[i] == '1')
        PRINT(f((x_mod_pcx1 - pow2i_mod_pcx1[n - i - 1] + pcx1) % pcx1));
      else
        PRINT(f((x_mod_pcx0 + pow2i_mod_pcx0[n - i - 1]) % pcx0));
    }
  }

  return 0;
}

int popcount(int v) {
  int res = 0;
  while (v > 0) {
    if (v & 1)
      res++;
    v >>= 1;
  }
  return res;
}

int f(int v) {
  int res = 1;
  while (v > 0) {
    v = v % popcount(v);
    res++;
  }
  return res;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define PRINT(x) cout << (x) << endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_N = 200009;

int N;
string X;

int Xi_mod_popcount_Xi[MAX_N];

vector<int> fs(MAX_N, -1);
int f(int x) {
  if (fs[x] != -1)
    return fs[x];
  if (x == 0)
    return fs[0] = 0;
  int popcount_x = 0;
  int y = x;
  while (y) {
    if (y & 1)
      popcount_x++;
    y >>= 1;
  }
  int nx = x % popcount_x;
  if (nx < 0)
    nx += popcount_x;
  return fs[x] = f(nx) + 1;
}

int main() {
  cin >> N >> X;

  // popcount(X)を求める
  int popcount_X = 0;
  REP(i, N) if (X[i] == '1') popcount_X++;

  // X mod (popcount(X) - 1),
  // X mod (popcount(X) + 1) を求める
  int X_mod_popcount_X_n = 0, X_mod_popcount_X_p = 0;
  int mod_n = popcount_X - 1, mod_p = popcount_X + 1;
  int amari_n = 0, amari_p = 0;
  if (mod_n > 0)
    amari_n = 1 % mod_n;
  if (mod_p > 0)
    amari_p = 1 % mod_p;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      X_mod_popcount_X_n += amari_n;
      X_mod_popcount_X_p += amari_p;
    }
    if (mod_n > 0)
      amari_n = amari_n * 2 % mod_n;
    if (mod_p > 0)
      amari_p = amari_p * 2 % mod_p;
  }

  // X_i mod popcount(X_i)を求める
  if (mod_n > 0)
    amari_n = 1 % mod_n;
  if (mod_p > 0)
    amari_p = 1 % mod_p;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1' && mod_n > 0) {
      Xi_mod_popcount_Xi[i] = (X_mod_popcount_X_n - amari_n) % mod_n;
      if (Xi_mod_popcount_Xi[i] < 0)
        Xi_mod_popcount_Xi[i] += mod_n;
    } else if (mod_p > 0) {
      Xi_mod_popcount_Xi[i] = (X_mod_popcount_X_p + amari_p) % mod_p;
      if (Xi_mod_popcount_Xi[i] < 0)
        Xi_mod_popcount_Xi[i] += mod_p;
    }
    if (mod_n > 0)
      amari_n = amari_n * 2 % mod_n;
    if (mod_p > 0)
      amari_p = amari_p * 2 % mod_p;
  }

  REP(i, N) {
    int ans;
    if (popcount_X == 1 && X[i] == '1')
      ans = 0;
    else
      ans = f(Xi_mod_popcount_Xi[i]) + 1;
    PRINT(ans);
  }

  return 0;
}