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

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> pre(n + 10, 0);

  for (ll i = 1; i <= n; i++) {
    ll ii = i;
    while (ii) {
      ii %= __builtin_popcount(ii);
      pre[i]++;
    }
  }

  ll cnt = 0;
  for (ll i = n - 1; i >= 0; i--)
    cnt += s[i] == '1';
  ll m1 = 0;
  ll m2 = 0;

  if (!cnt) {
    for (ll i = 0; i < n; i++)
      cout << 1 << endl;
    return;
  }

  for (ll i = 0; i < n; i++)
    if (s[i] == '1') {
      m1 = (m1 + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        m2 = (m2 + modpow(2, n - i - 1, cnt - 1)) % (cnt - 1);
    }

  ll c1 = 0;
  ll c2 = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1' && cnt == 1) {
      cout << 0 << endl;
      continue;
    }
    c1 = modpow(2, n - i - 1, cnt + 1);
    if (cnt > 1)
      c2 = modpow(2, n - i - 1, cnt - 1);
    else
      c2 = -1;
    if (s[i] == '0')
      cout << pre[(m1 + c1) % (cnt + 1)] + 1 << endl;
    else
      cout << pre[((m2 - c2) + (cnt - 1)) % (cnt - 1)] + 1 << endl;
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> pre(n + 10, 0);

  for (ll i = 1; i <= n; i++) {
    ll ii = i;
    while (ii) {
      ii %= __builtin_popcount(ii);
      pre[i]++;
    }
  }

  ll cnt = 0;
  for (ll i = n - 1; i >= 0; i--)
    cnt += s[i] == '1';
  ll m1 = 0;
  ll m2 = 0;

  if (!cnt) {
    for (ll i = 0; i < n; i++)
      cout << 1 << endl;
    return;
  }

  for (ll i = 0; i < n; i++)
    if (s[i] == '1') {
      m1 = (m1 + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        m2 = (m2 + modpow(2, n - i - 1, cnt - 1)) % (cnt - 1);
    }

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1' && cnt == 1) {
      cout << 0 << endl;
      continue;
    }
    ll c1 = modpow(2, n - i - 1, cnt + 1);
    ll c2 = modpow(2, n - i - 1, cnt - 1);
    if (s[i] == '0')
      cout << pre[(m1 + c1) % (cnt + 1)] + 1 << endl;
    else
      cout << pre[((m2 - c2) + (cnt - 1)) % (cnt - 1)] + 1 << endl;
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}