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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// pow2[0][j] = 2^i mod (popcnt(x)+1)
// pow2[1][j] = 2^i mod (popcnt(x)-1) ただし popcnt(x)=0 のときは 0
constexpr int MAX_N = 200005;
ll pow2[2][MAX_N] = {};
ll f[MAX_N] = {};
int main() {
  int n;
  string x;
  cin >> n >> x;
  // popcount(x) を計算する
  int popcount_x = 0;
  for (const auto &xi : x) {
    if (xi == '1') {
      popcount_x += 1;
    }
  }
  // 2^i mod (popcount(x) \\pm 1) を計算する
  for (int i = 0; i < 2; ++i) {
    const ll mod = popcount_x + ((i == 0) ? 1 : (-1));
    pow2[i][0] = 1;
    if (mod <= 0)
      continue; // mod 負のときはスルー
    for (int j = 1; j < n; ++j) {
      pow2[i][j] = (2 * pow2[i][j - 1]) % mod;
    }
  }
  // popcount(x) 以下について f(m) を計算する
  f[0] = 0;
  for (int i = 1; i <= popcount_x + 1; ++i) {
    int popcount = 0;
    for (int d = 0; d < 30; ++d) {
      if (i & (1 << d))
        popcount++;
    }
    f[i] = f[i % popcount] + 1;
  }
  // x mod (popcount(x) \\pm 1) を計算する
  ll x_mod_popcnt[2] = {};
  for (int i = 0; i < n; ++i) {
    const int d = n - i - 1;
    if (x[i] == '1') {
      for (int j = 0; j < 2; ++j) {
        x_mod_popcnt[j] += pow2[j][d];
      }
    }
  }
  // f(x_i) を計算する
  for (int i = 0; i < n; ++i) {
    const int d = n - i - 1;
    if (x[i] == '0') {
      // 反転して1が増える
      const ll mod = popcount_x + 1;
      const ll xi_mod_popcnt = (x_mod_popcnt[0] + pow2[0][d]) % mod;
      cout << f[xi_mod_popcnt] + 1 << endl;
    } else {
      // 反転して1が減る
      const ll mod = popcount_x - 1;
      if (mod == 0) {
        cout << 0 << endl;
        continue;
      }
      const ll xi_mod_popcnt = (x_mod_popcnt[1] + mod - pow2[1][d]) % mod;
      cout << f[xi_mod_popcnt] + 1 << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// pow2[0][j] = 2^i mod (popcnt(x)+1)
// pow2[1][j] = 2^i mod (popcnt(x)-1) ただし popcnt(x)=0 のときは 0
constexpr int MAX_N = 200005;
ll pow2[2][MAX_N] = {};
ll f[MAX_N] = {};
int main() {
  int n;
  string x;
  cin >> n >> x;
  // popcount(x) を計算する
  int popcount_x = 0;
  for (const auto &xi : x) {
    if (xi == '1') {
      popcount_x += 1;
    }
  }
  // 2^i mod (popcount(x) \\pm 1) を計算する
  for (int i = 0; i < 2; ++i) {
    const ll mod = popcount_x + ((i == 0) ? 1 : (-1));
    pow2[i][0] = 1;
    if (mod < 0)
      continue; // mod 負のときはスルー
    for (int j = 1; j < n; ++j) {
      pow2[i][j] = (2 * pow2[i][j - 1]) % mod;
    }
  }
  // popcount(x) 以下について f(m) を計算する
  f[0] = 0;
  for (int i = 1; i <= popcount_x + 1; ++i) {
    int popcount = 0;
    for (int d = 0; d < 30; ++d) {
      if (i & (1 << d))
        popcount++;
    }
    f[i] = f[i % popcount] + 1;
  }
  // x mod (popcount(x) \\pm 1) を計算する
  ll x_mod_popcnt[2] = {};
  for (int i = 0; i < n; ++i) {
    const int d = n - i - 1;
    if (x[i] == '1') {
      for (int j = 0; j < 2; ++j) {
        x_mod_popcnt[j] += pow2[j][d];
      }
    }
  }
  // f(x_i) を計算する
  for (int i = 0; i < n; ++i) {
    const int d = n - i - 1;
    if (x[i] == '0') {
      // 反転して1が増える
      const ll mod = popcount_x + 1;
      const ll xi_mod_popcnt = (x_mod_popcnt[0] + pow2[0][d]) % mod;
      cout << f[xi_mod_popcnt] + 1 << endl;
    } else {
      // 反転して1が減る
      const ll mod = popcount_x - 1;
      const ll xi_mod_popcnt = (x_mod_popcnt[1] + mod - pow2[1][d]) % mod;
      cout << f[xi_mod_popcnt] + 1 << endl;
    }
  }
  return 0;
}