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

#include <bits/stdc++.h>
#define PB push_back
#define PII pair<int, int>
#define PLL pair<long long, long long>
#define FI first
#define SE second
#define mem(a) memset(a, 0, sizeof(a))
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1), eps = 1e-8;
const LL maxn = 1 << 17;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
LL qpow(LL a, LL b, LL p) {
  LL ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return ans;
}
char s[N];
int main() {
  ios;
  int n, cnt = 0;
  int mo1 = 0, mo2 = 0;
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  for (int i = 1; i <= n; i++) {
    mo1 = (mo1 * 2 + (s[i] - '0')) % (cnt + 1);
  }
  if (cnt > 1) {
    for (int i = 1; i <= n; i++) {
      mo2 = (mo2 * 2 + (s[i] - '0')) % (cnt - 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    int now;
    if (s[i] == '0') {
      now = (mo1 + qpow(2, n - i, cnt + 1)) % (cnt + 1);
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      now = (mo2 - qpow(2, n - i, cnt - 1) + cnt - 1) % (cnt - 1);
    }
    int f = 1;
    while (now > 0) {
      now = now % __builtin_popcount(now);
      f++;
    }
    cout << f << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define PII pair<int, int>
#define PLL pair<long long, long long>
#define FI first
#define SE second
#define mem(a) memset(a, 0, sizeof(a))
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1), eps = 1e-8;
const LL maxn = 1 << 17;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL lcm(LL a, LL b) { return a * b / gcd(a, b); }
LL d[N], power[N];
int cnt(int x) {
  int ans = 0;
  while (x) {
    ans += x % 2;
    x /= 2;
  }
  return ans;
}
int main() {
  memset(d, -1, sizeof(d));
  d[1] = 1;
  d[0] = 0;
  for (int i = 2; i <= 2e5; i++) {
    d[i] = d[i % cnt(i)] + 1;
  }
  int n;
  cin >> n;
  string a;
  cin >> a;
  int ans = 0, res = 0;
  for (int i = 0; i < a.length(); i++) {
    if (a[i] == '1')
      ans++;
  }
  LL base = 1;
  for (int i = a.length() - 1; i >= 0; i--) {
    if (a[i] == '1') {
      res = res + base % ans;
    }
    power[i] = base % ans;
    base = base * 2 % ans;
  }
  int len = a.length();
  for (int i = 0; i < len; i++) {
    if (a[i] == '0') {
      cout << res + power[len - 1 - i] << endl;
    } else {
      cout << res - power[len - 1 - i] << endl;
    }
  }
  return 0;
}