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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define X first;
#define Y second;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const ll INF = 2e18;
const int inf = 1e9 + 10;
const int maxN = 2e5 + 10;
const int maxD = 15;
const int maxH = 100;
const int MOD = 1e9 + 7;

int a[maxN];
int b[maxN];
ll get(ll x) {
  ll cnt = 0;
  while (x) {
    x = x % __builtin_popcount(x);
    cnt++;
  }
  return cnt;
}
ll power(ll x, ll p, ll m) {
  if (p == 0) {
    return 1;
  }
  ll t = power(x, p / 2, m);
  t = t * t % m;
  if (p % 2 == 1) {
    t = t * x % m;
  }
  return t;
}
ll ans[maxN];
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = s[i - 1] - '0';
    if (a[i])
      cnt++;
  }
  for (int i = 1; i <= n; i++) {
    b[i] = get(i);
  }
  if (cnt > 1) {

    ll sum1 = 0; // mod cnt-1
    for (int i = 1; i <= n; i++) {
      sum1 = (sum1 * 2 % (cnt - 1) + a[i]) % (cnt - 1);
    }
    ll sum2 = 0; // mod cnt+1;
    for (int i = 1; i <= n; i++) {
      sum2 = (sum2 * 2 % (cnt + 1) + a[i]) % (cnt + 1);
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0) {
        ll x = sum2 + power(2, n - i, cnt + 1);
        x %= (cnt + 1);
        ans[i] = b[x] + 1;
      } else {
        ll x = sum1 - power(2, n - i, cnt - 1) + cnt - 1;
        x %= (cnt - 1);
        ans[i] = b[x] + 1;
      }
    }
  } else if (cnt == 1) {
    ll sum2 = 0; // mod cnt+1;
    for (int i = 1; i <= n; i++) {
      sum2 = (sum2 * 2 % (cnt + 1) + a[i]) % (cnt + 1);
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0) {
        ll x = sum2 + power(2, n - i, cnt + 1);
        x %= (cnt + 1);
        ans[i] = b[x] + 1;
      }
    }
  } else {
    for (int i = 1; i <= n; i++) {
      ans[i] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define X first;
#define Y second;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const ll INF = 2e18;
const int inf = 1e9 + 10;
const int maxN = 2e5 + 10;
const int maxD = 15;
const int maxH = 100;
const int MOD = 1e9 + 7;

int a[maxN];
int b[maxN];
ll get(ll x) {
  ll cnt = 0;
  while (x) {
    x = x % __builtin_popcount(x);
    cnt++;
  }
  return cnt;
}
ll power(ll x, ll p, ll m) {
  if (p == 0) {
    return 1;
  }
  ll t = power(x, p / 2, m);
  t = t * t % m;
  if (p % 2 == 1) {
    t = t * x % m;
  }
  return t;
}
ll ans[maxN];
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = s[i - 1] - '0';
    if (a[i])
      cnt++;
  }
  for (int i = 1; i <= n; i++) {
    b[i] = get(i);
  }
  ll sum1 = 0; // mod cnt-1
  for (int i = 1; i <= n; i++) {
    sum1 = (sum1 * 2 % (cnt - 1) + a[i]) % (cnt - 1);
  }
  ll sum2 = 0; // mod cnt+1;
  for (int i = 1; i <= n; i++) {
    sum2 = (sum2 * 2 % (cnt + 1) + a[i]) % (cnt + 1);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) {
      ll x = sum2 + power(2, n - i, cnt + 1);
      x %= (cnt + 1);
      ans[i] = get(x) + 1;
    } else {
      ll x = sum1 - power(2, n - i, cnt - 1) + cnt - 1;
      x %= (cnt - 1);
      ans[i] = get(x) + 1;
    }
    cout << ans[i] << endl;
  }
  return 0;
}