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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

ll pow(ll x, ll y, ll mod) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return pow(x * x % mod, y / 2, mod) % mod;
  } else {
    return x * pow(x, y - 1, mod) % mod;
  }
}

void solve() {
  ll N;
  string X;
  cin >> N >> X;
  ll pop_count = 0;
  rep(i, N) {
    if (X[i] == '1')
      pop_count++;
  }
  if (pop_count == 0) {
    rep(i, N) { cout << 1 << '\\n'; }
    return;
  }
  // +1
  ll v1 = 0;
  // -1
  ll v2 = 0;
  rep(i, N) {
    if (X[i] == '1') {
      v1 += pow(2, N - 1 - i, pop_count + 1);
      if (pop_count > 1) {
        v2 += pow(2, N - 1 - i, pop_count - 1);
      }
    }
  }
  vector<ll> B(1e5 * 3 + 1, 0);
  rep2(i, 1, 1e5 * 2 + 1) {
    ll c = __builtin_popcount(i);
    ll r = i % c;
    if (r == 0) {
      B[i] = 1;
    } else {
      B[i] = B[r] + 1;
    }
  }
  rep(i, N) {
    ll x = 0;
    if (X[i] == '1') {
      ll mod = pop_count - 1;
      if (mod == 0) {
        cout << 0 << '\\n';
        continue;
      } else {
        x = v2 - pow(2, N - 1 - i, mod);
        if (x < 0)
          x += mod;
        x %= mod;
      }
    } else {
      ll mod = pop_count + 1;
      x = v1 + pow(2, N - 1 - i, mod);
      x %= mod;
    }
    cout << B[x] + 1 << '\\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

ll pow(ll x, ll y, ll mod) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return pow(x * x % mod, y / 2, mod) % mod;
  } else {
    return x * pow(x, y - 1, mod) % mod;
  }
}

void solve() {
  ll N;
  string X;
  cin >> N >> X;
  ll pop_count = 0;
  rep(i, N) {
    if (X[i] == '1')
      pop_count++;
  }
  if (pop_count == 0) {
    rep(i, N) { cout << 1 << '\\n'; }
    return;
  }
  // +1
  ll v1 = 0;
  // -1
  ll v2 = 0;
  rep(i, N) {
    if (X[i] == '1') {
      v1 += pow(2, N - 1 - i, pop_count + 1);
      v2 += pow(2, N - 1 - i, pop_count - 1);
    }
  }
  vector<ll> B(1e5 * 3 + 1, 0);

  rep2(i, 1, 1e5 * 2 + 1) {
    ll c = __builtin_popcount(i);
    ll r = i % c;
    if (r == 0) {
      B[i] = 1;
    } else {
      B[i] = B[r] + 1;
    }
  }
  rep(i, N) {
    ll x = 0;
    if (X[i] == '1') {
      ll mod = pop_count - 1;
      x = v2 - pow(2, N - 1 - i, mod);
      if (x < 0)
        x += mod;
      x %= mod;
    } else {
      ll mod = pop_count + 1;
      x = v1 + pow(2, N - 1 - i, mod);
      x %= mod;
    }
    cout << B[x] + 1 << '\\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}