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

#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(0);                                                                \\
  }
#define ll long long
#define pi pair<ll, ll>
ll a[2000000], b[300000];
string s;
ll f(ll n, ll mod, ll b) {
  ll ans = 1, aa;
  if (!b)
    return 1;
  if (b % 2)
    ans = n;
  aa = f(n, mod, b / 2);
  return ((aa * aa) % mod * ans) % mod;
}
ll g(ll n) {
  ll cnt, ans = 0;
  while (n) {
    cnt = __builtin_popcount(n);
    n %= cnt;
    ans++;
  }
  return ans;
}
int main() {
  IO;
  ll i, j, k, l = 0, n, m, h, mod = 1e9 + 7, cnt = 0, mx = 0, t, ans = 0,
              tc = 0, r, l1, l2 = 0;

  cin >> n >> s;
  for (i = 1; i <= n; i++) {
    if (s[i - 1] == '1')
      cnt++;
  }
  l1 = 0;
  for (i = 1; i <= n; i++) {
    if (s[i - 1] == '1')
      l1 = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
  }
  if (cnt > 1)
    for (i = 1; i <= n; i++) {
      if (s[i - 1] == '1')
        l2 = (l2 + f(2, cnt - 1, n - i)) % (cnt - 1);
    }
  for (i = 1; i <= n; i++) {
    if (s[i - 1] == '0') {
      a[i] = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
      b[i] = 1;
    } else {
      if (cnt != 1) {
        a[i] = (l2 - f(2, cnt - 1, n - i)) % (cnt - 1);
        b[i] = 1;
        if (a[i] < 0)
          a[i] += cnt - 1;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    b[i] += g(a[i]);
  }
  for (i = 1; i <= n; i++)
    cout << b[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(0);                                                                \\
  }
#define ll long long
#define pi pair<ll, ll>
ll a[2000000], b[300000];
string s;
ll f(ll n, ll mod, ll b) {
  ll ans = 1, aa;
  if (!b)
    return 1;
  if (b % 2)
    ans = n;
  aa = f(n, mod, b / 2);
  return ((aa * aa) % mod * ans) % mod;
}
ll g(ll n) {
  ll cnt, ans = 0;
  while (n) {
    cnt = __builtin_popcount(n);
    n %= cnt;
    ans++;
  }
  return ans;
}
int main() {
  IO;
  ll i, j, k, l = 0, n, m, h, mod = 1e9 + 7, cnt = 0, mx = 0, t, ans = 0,
              tc = 0, r, l1, l2 = 0;

  cin >> n >> s;
  for (i = 1; i <= n; i++) {
    if (s[i - 1] == '1')
      cnt++;
  }
  l1 = 0;
  for (i = 1; i <= n; i++) {
    if (s[i - 1] == '1')
      l1 = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
  }
  if (cnt)
    for (i = 1; i <= n; i++) {
      if (s[i - 1] == '1')
        l2 = (l2 + f(2, cnt - 1, n - i)) % (cnt - 1);
    }
  for (i = 1; i <= n; i++) {
    if (s[i - 1] == '0') {
      a[i] = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
      b[i] = 1;
    } else {
      if (cnt != 1) {
        a[i] = (l2 - f(2, cnt - 1, n - i)) % (cnt - 1);
        b[i] = 1;
        if (a[i] < 0)
          a[i] += cnt - 1;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    b[i] += g(a[i]);
  }
  for (i = 1; i <= n; i++)
    cout << b[i] << endl;
  return 0;
}