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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

ll mod_pow(ll x, ll n, ll mod = MOD) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;

  return res;
}

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

  string X;
  cin >> X;
  reverse(ALL(X));

  vector<int> dp(N + 100);
  dp[0] = 0;
  FOR(i, 1, N + 10) {
    int p = 0;
    REP(j, 30) if (i >> j & 1)++ p;
    dp[i] = dp[i % p] + 1;
  }

  int bp = 0;
  REP(i, N) if (X[i] == '1')++ bp;

  int base[3]{}; // -1, 0, 1
  REP(i, 3) {
    int bias = i - 1;
    // bias >= 0
    if (bp + bias == 0)
      continue;
    // bp + bias 個で割る
    REP(j, N) if (X[j] == '1') {
      base[i] += mod_pow(2, j, bp + bias);
      base[i] %= bp + bias;
    }
  }

  reverse(ALL(X));
  REP(i, N) {
    int bias = X[i] == '1' ? -1 : 1;

    if (bp + bias == 0) {
      puts("0");
      continue;
    }
    int ch = mod_pow(2, N - 1 - i, bp + bias);
    if (X[i] == '1')
      ch = (-ch + bp + bias) % (bp + bias);
    int nem = (ch + base[bias == -1 ? 0 : 2]) % (bp + bias);
    printf("%d\\n", dp[nem] + 1);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

ll mod_pow(ll x, ll n, ll mod = MOD) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;

  return res;
}

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

  string X;
  cin >> X;
  reverse(ALL(X));

  vector<int> dp(N + 100);
  dp[0] = 0;
  FOR(i, 1, N + 10) {
    int p = 0;
    REP(j, 30) if (i >> j & 1)++ p;
    dp[i] = dp[i % p] + 1;
  }

  int bp = 0;
  REP(i, N) if (X[i] == '1')++ bp;

  int base[3]{}; // -1, 0, 1
  REP(i, 3) {
    int bias = i - 1;
    // bias >= 0
    if (bias == 0)
      continue;
    // bp + bias 個で割る
    REP(j, N) if (X[j] == '1') {
      base[i] += mod_pow(2, j, bp + bias);
      base[i] %= bp + bias;
    }
  }

  reverse(ALL(X));
  REP(i, N) {
    int bias = X[i] == '1' ? -1 : 1;

    if (bp + bias == 0) {
      puts("0");
      continue;
    }
    int ch = mod_pow(2, N - 1 - i, bp + bias);
    if (X[i] == '1')
      ch = (-ch + bp + bias) % (bp + bias);
    int nem = (ch + base[bias == -1 ? 0 : 2]) % (bp + bias);
    printf("%d\\n", dp[nem] + 1);
  }

  return 0;
}