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;

ll modpow(ll base, ll index, ll mod) {
  ll res = 1;
  while (index) {
    if (index & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    index /= 2;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen(".in.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, oc = 0;
  cin >> n;
  ll m1 = 0, m2 = 0;
  string x;
  cin >> x;
  vector<int> pre(n + 1);
  for (int i = 1; i <= n; i++) {
    int ii = i;
    while (ii) {
      ii %= __builtin_popcount(ii);
      pre[i]++;
    }
  }
  for (int i = 0; i < n; i++)
    oc += x[i] == '1';
  if (!oc) {
    for (int i = 0; i < n; i++)
      cout << 1 << endl;
    return 0;
  }
  for (ll i = n - 1; i > -1; i--)
    if (x[i] == '1') {
      if (oc > 1)
        m1 = (m1 + modpow(2, n - i - 1, oc - 1)) % (oc - 1);
      m2 = (m2 + modpow(2, n - i - 1, oc + 1)) % (oc + 1);
    }
  for (int i = 0; i < n; i++) {
    if (x[i] == '1' && oc == 1) {
      cout << 0 << endl;
      continue;
    }
    ll dec = (oc == 1) ? -1 : modpow(2, n - i - 1, oc - 1),
       inc = modpow(2, n - i - 1, oc + 1);
    if (x[i] == '1')
      cout << pre[(((m1 - dec) % (oc - 1)) + (oc - 1)) % (oc - 1)] + 1 << endl;
    else
      cout << pre[(m2 + inc) % (oc + 1)] + 1 << endl;
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


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;

ll modpow(ll base, ll index, ll mod) {
  ll res = 1;
  while (index) {
    if (index & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    index /= 2;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen(".in.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, oc = 0;
  cin >> n;
  ll m1 = 0, m2 = 0;
  string x;
  cin >> x;
  vector<int> pre(n + 1);
  for (int i = 1; i <= n; i++) {
    int ii = i, c = 0;
    while (ii) {
      ii %= __builtin_popcount(ii);
      c++;
    }
    pre[i] = c;
  }
  for (int i = 0; i < n; i++)
    oc += x[i] == '1';
  for (ll i = n - 1; i > -1; i--)
    if (x[i] == '1') {
      m1 = (m1 + modpow(2, n - i - 1, oc - 1)) % (oc - 1);
      m2 = (m2 + modpow(2, n - i - 1, oc + 1)) % (oc + 1);
    }
  for (int i = 0; i < n; i++) {
    if (x[i] == '1' && oc == 1) {
      cout << 0 << endl;
      continue;
    }
    ll dec = modpow(2, n - i - 1, oc - 1), inc = modpow(2, n - i - 1, oc + 1);
    if (x[i] == '1')
      cout << pre[(((m1 - dec) % (oc - 1)) + (oc - 1)) % (oc - 1)] + 1 << endl;
    else
      cout << pre[(m2 + inc) % (oc + 1)] + 1 << endl;
  }

  return 0;
}