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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;
ll const DIGIT = 2 * 1e5;

ll mod_pow(ll a, ll n, ll p) {
  if (n == 0)
    return 1 % p;
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (mod_pow(a, 1, p) * mod_pow(a, n - 1, p)) % p;
  ll t = mod_pow(a, n / 2, p);
  return (t * t) % p;
}

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

  bitset<DIGIT> bs2(x);
  ll cnt = bs2.count();

  ll plus_mod = 0;
  ll minus_mod = 0;
  rep(i, n) {
    if (x[n - 1 - i] - '0') {
      plus_mod = (plus_mod + mod_pow(2, i, cnt + 1)) % (cnt + 1);
      if (cnt != 1)
        minus_mod = (minus_mod + mod_pow(2, i, cnt - 1)) % (cnt - 1);
    }
    // cout << i << " " << plus_mod << " " << minus_mod << endl;
  }
  // cout << plus_mod << " " << minus_mod << endl;
  // ll xx = bs2.to_ullong();
  // ll max_digit = ll(pow(2, n-1));
  // bitset<DIGIT> md2(max_digit);
  // cout << max_digit << endl;
  // cout << DIGIT << endl;

  rep(i, n) {
    ll mod;
    // cout << i << endl;
    if (x[i] == '1') {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      mod = (minus_mod - mod_pow(2, n - 1 - i, cnt - 1)) % (cnt - 1);
      if (mod < 0)
        mod += (cnt - 1);
    } else {
      mod = (plus_mod + mod_pow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      if (mod < 0)
        mod += (cnt + 1);
    }

    // bitset<DIGIT> tmp = (md2 >> i) ^ bs2;
    // cout << i << " " << tmp << " " << xx << " " << max_digit << endl;
    ll ans = 1;
    while (mod != 0) {
      // cout << mod << endl;
      mod %= __builtin_popcountll(mod);
      ans++;
    }
    cout << ans << endl;
  }
  // 00         1       10       1       1      100      10     1     1   0 1
  // 100   1110 (4194304)  1048576 524288   131072  65536  32768    4096   1024
  // 512 0 128  64  008420

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;
ll const DIGIT = 2 * 1e5;

ll mod_pow(ll a, ll n, ll p) {
  if (n == 0)
    return 1 % p;
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (mod_pow(a, 1, p) * mod_pow(a, n - 1, p)) % p;
  ll t = mod_pow(a, n / 2, p);
  return (t * t) % p;
}

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

  bitset<DIGIT> bs2(x);
  ll cnt = bs2.count();

  ll plus_mod = 0;
  ll minus_mod = 0;
  rep(i, n) {
    if (x[n - 1 - i] - '0') {
      plus_mod = (plus_mod + mod_pow(2, i, cnt + 1)) % (cnt + 1);
      minus_mod = (minus_mod + mod_pow(2, i, cnt - 1)) % (cnt - 1);
    }
    // cout << i << " " << plus_mod << " " << minus_mod << endl;
  }
  // cout << plus_mod << " " << minus_mod << endl;
  // ll xx = bs2.to_ullong();
  // ll max_digit = ll(pow(2, n-1));
  // bitset<DIGIT> md2(max_digit);
  // cout << max_digit << endl;
  // cout << DIGIT << endl;

  rep(i, n) {
    ll mod;
    if (x[i] == '1') {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      mod = (minus_mod - mod_pow(2, n - 1 - i, cnt - 1)) % (cnt - 1);
      if (mod < 0)
        mod += (cnt - 1);
    } else {
      mod = (plus_mod + mod_pow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      if (mod < 0)
        mod += (cnt + 1);
    }

    // bitset<DIGIT> tmp = (md2 >> i) ^ bs2;
    // cout << i << " " << tmp << " " << xx << " " << max_digit << endl;
    ll ans = 1;
    while (mod != 0) {
      // cout << mod << endl;
      mod %= __builtin_popcountll(mod);
      ans++;
    }
    cout << ans << endl;
  }
  // 00         1       10       1       1      100      10     1     1   0 1
  // 100   1110 (4194304)  1048576 524288   131072  65536  32768    4096   1024
  // 512 0 128  64  008420

  return 0;
}