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

#include <bits/stdc++.h>
using namespace std;

#define int long long
using ll = long long;
using ld = long double;
using intpair = pair<int, int>;
using intmap = map<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const int MOD = 1e9 + 7;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
// struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;

#define debug(x) cerr << #x << ": " << x << '\\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()

int f(int n) {
  int ret = 0;
  while (n > 0) {
    bitset<32> b = n;
    n = n % b.count();
    ret++;
  }
  return ret;
}

int modpow(int a, int n, int m = MOD) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = (ret * a) % m;
    a = (a * a) % m;
    n >>= 1;
  }
  return ret % m;
}

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

  vi memo(n + 5);
  memo[0] = 0;
  reps(i, 1, n + 4) { memo[i] = f(i); }

  int popcount = 0;
  for (char c : x) {
    if (c == '1')
      popcount++;
  }

  vi calc(2, 0);
  rep(i, 2) {
    if (i)
      popcount += 2;
    else
      popcount--;
    if (popcount <= 0)
      continue;

    int d = 1;
    for (int j = n - 1; j >= 0; j--) {
      if (x[j] == '1') {
        calc[i] = mod(calc[i] + d, popcount);
      }
      d = (d * 2) % popcount;
    }
  }
  popcount--;

  rep(i, n) {
    int t;
    if (x[i] == '1') {
      if (popcount - 1 <= 0) {
        cout << 0 << endl;
        continue;
      }
      t = mod(calc[0] - modpow(2, n - i - 1, popcount - 1), popcount - 1);
    } else {
      t = mod(calc[1] + modpow(2, n - i - 1, popcount + 1), popcount + 1);
    }
    cout << 1 + memo[t] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
using ll = long long;
using ld = long double;
using intpair = pair<int, int>;
using intmap = map<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const int MOD = 1e9 + 7;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
// struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;

#define debug(x) cerr << #x << ": " << x << '\\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()

int f(int n) {
  int ret = 0;
  while (n > 0) {
    bitset<32> b = n;
    n = n % b.count();
    ret++;
  }
  return ret;
}

int modpow(int a, int n, int m = MOD) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = (ret * a) % m;
    a = (a * a) % m;
    n >>= 1;
  }
  return ret % m;
}

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

  vi memo(n + 5);
  memo[0] = 0;
  reps(i, 1, n + 5) { memo[i] = f(i); }

  int popcount = 0;
  for (char c : x) {
    if (c == '1')
      popcount++;
  }

  vi calc(2, 0);
  rep(i, 2) {
    if (i)
      popcount += 2;
    else
      popcount--;

    int d = 1;
    for (int j = n - 1; j >= 0; j--) {
      if (x[j] == '1') {
        calc[i] = mod(calc[i] + d, popcount);
      }
      d *= 2;
    }
  }
  popcount--;

  rep(i, n) {
    int t;
    if (x[i] == '1') {
      if (popcount - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      t = mod(calc[0] - modpow(2, n - i - 1, popcount - 1), popcount - 1);
    } else {
      t = mod(calc[1] + modpow(2, n - i - 1, popcount + 1), popcount + 1);
    }
    cout << 1 + memo[t] << endl;
  }
}