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

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 200010;
int f[N], pop[N];
int a[N], n;
ll qmi(ll a, ll b, ll p) {
  ll res = 1 % p;
  a %= p;
  while (b) {
    if (b & 1)
      res = res * a % p;
    b >>= 1;
    a = a * a % p;
  }
  return res;
}
int calc(ll x) {
  int res = 0;
  while (x) {
    x -= x & -x;
    res++;
  }
  return res;
}
int init() {
  for (int i = 1; i < N; i++)
    pop[i] = calc(i);
  f[1] = 1, f[2] = 1;
  for (int i = 3; i < N; i++)
    f[i] = f[i % pop[i]] + 1;
}
ll md(ll x, ll y) { return (x % y + y) % y; }
int main() {
  cin >> n;
  int s = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%1d", &a[i]);
    s += a[i];
  }
  if (s > 1) {
    ll x = 0, y = 0;
    // init();
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0)
        continue;
      x = md(x + qmi(2, n - i, s - 1), s - 1);
      y = md(y + qmi(2, n - i, s + 1), s + 1);
    }
    for (int i = 1; i <= n; i++) {
      /*
      if(a[i]==0) cout<<f[md(y+qmi(2,n-i,s+1),s+1)]+1<<endl;
      else cout<<f[md(x-qmi(2,n-i,s-1),s-1)]+1<<endl;*/
      ll ans = 0;
      int cnt = 1;
      if (a[i] == 0)
        ans = md(y + qmi(2, n - i, s + 1), s + 1);
      else
        ans = md(x - qmi(2, n - i, s - 1), s - 1);
      while (ans) {
        ans %= calc(ans);
        cnt++;
      }
      cout << cnt << endl;
    }
  } else if (s == 0)
    for (int i = 1; i <= n; i++)
      cout << 1 << endl;
  else {
    for (int i = 1; i <= n; i++) {
      if (a[i] == 1)
        cout << 0 << endl;
      else {
        if (a[n] == 1 || i == n)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 200010;
int f[N], pop[N];
int a[N], n;
ll qmi(ll a, int b, int p) {
  ll res = 1 % p;
  a %= p;
  while (b) {
    if (b & 1)
      res = res * a % p;
    b >>= 1;
    a = a * a % p;
  }
  return res;
}
int calc(int x) {
  int res = 0;
  while (x) {
    x -= x & -x;
    res++;
  }
  return res;
}
int init() {
  for (int i = 1; i < N; i++)
    pop[i] = calc(i);
  f[1] = 1, f[2] = 1;
  for (int i = 3; i < N; i++)
    f[i] = f[i % pop[i]] + 1;
}
int md(ll x, int y) { return (x % y + y) % y; }
int main() {
  cin >> n;
  int s = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%1d", &a[i]);
    s += a[i];
  }
  if (s > 1) {
    ll x = 0, y = 0;
    init();
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0)
        continue;
      x = md(x + qmi(2, n - i, s - 1), s - 1);
      y = md(y + qmi(2, n - i, s + 1), s + 1);
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0)
        cout << f[md(y + qmi(2, n - i, s + 1), s + 1)] + 1 << endl;
      else
        cout << f[md(x - qmi(2, n - i, s - 1), s - 1)] + 1 << endl;
    }
  } else if (s == 0)
    for (int i = 1; i <= n; i++)
      cout << 1 << endl;
  else {
    for (int i = 1; i <= n; i++) {
      if (a[i] == 1)
        cout << 0 << endl;
      else {
        if (a[n] == 1 || i == n)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
  }
  return 0;
}