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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define wzh(x) cerr << #x << ' ' << x << endl
int n;
char a[N];
LL pm(LL x, LL y, LL z) {
  LL q = 1;
  while (y) {
    if (y & 1)
      q = q * x % z;
    x = x * x % z;
    y >>= 1;
  }
  return q;
}
int f[N][2];
int s[N][2];
void gao(int x) {
  if (!x) {
    cout << 1 << '\\n';
    return;
  }
  int ans = 1;
  while (x) {
    x %= __builtin_popcount(x);
    // cout<<x<<endl;
    ans++;
  }
  cout << ans << '\\n';
  return;
}
int main() {
  ios::sync_with_stdio(false);
  // 111 001 010
  cin >> n >> a + 1;
  int sz = 0, pos;
  for (int i = 1; i <= n; i++) {
    sz += a[i] == '1';
    if (a[i] == '1')
      pos = i;
  }
  // sz+1 or sz-1
  if (sz == 0) {
    for (int i = 1; i <= n; i++) {
      cout << 1 << '\\n';
    }
    return 0;
  }
  if (sz == 1) {
    int x = pm(2, n - pos + 1, 2);
    for (int i = 1; i <= n; i++) {
      if (a[i] == '1')
        cout << 0 << '\\n';
      else {
        gao((x + pm(2, n - i, 2)) % 2);
      }
    }
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    f[i][0] = (a[i] - '0') * pm(2, n - i, sz - 1);
    s[i][0] = (s[i - 1][0] + f[i][0]) % (sz - 1);
    f[i][1] = (a[i] - '0') * pm(2, n - i, sz + 1);
    s[i][1] = (s[i - 1][1] + f[i][1]) % (sz + 1);
  }
  LL pre = 0;
  for (int i = 1; i <= n; i++) {
    pre = (pre + (a[i] - '0') * pm(2, n - i, sz + 1)) % (sz + 1);
    if (i == n)
      pre = (pre + pm(2, n - i, sz + 1)) % (sz + 1);
    // cout<<i<<' '<<pre<<endl;
  }
  // cout<<pre<<endl;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '1') {
      // sz-1
      int go = (s[n][0] - pm(2, n - i, sz - 1) + sz - 1) % (sz - 1);
      gao(go);
    } else {
      int go = (s[n][1] + pm(2, n - i, sz + 1)) % (sz + 1);
      gao(go);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define wzh(x) cerr << #x << ' ' << x << endl
int n;
char a[N];
LL pm(LL x, LL y, LL z) {
  LL q = 1;
  while (y) {
    if (y & 1)
      q = q * x % z;
    x = x * x % z;
    y >>= 1;
  }
  return q;
}
int f[N][2];
int s[N][2];
void gao(int x) {

  if (!x) {
    cout << 1 << '\\n';
    return;
  }
  int ans = 1;
  while (x) {
    x %= __builtin_popcount(x);
    // cout<<x<<endl;
    ans++;
  }
  cout << ans << '\\n';
  return;
}
int main() {
  ios::sync_with_stdio(false);
  // 111 001 010
  cin >> n >> a + 1;
  int sz = 0, pos;
  for (int i = 1; i <= n; i++) {
    sz += a[i] == '1';
    if (a[i] == '1')
      pos = i;
  }
  // sz+1 or sz-1
  if (sz == 0) {
    for (int i = 1; i <= n; i++) {
      cout << 1 << '\\n';
    }
    return 0;
  }
  if (sz == 1) {
    int x = pm(2, n - pos + 1, 2);
    for (int i = 1; i <= n; i++) {
      if (a[i] == '1')
        cout << 0 << '\\n';
      else {
        gao((x + pm(2, n - i + 1, 2)) % 2);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    f[i][0] = (a[i] - '0') * pm(2, n - i, sz - 1);
    s[i][0] = (s[i - 1][0] + f[i][0]) % (sz - 1);
    f[i][1] = (a[i] - '0') * pm(2, n - i, sz + 1);
    s[i][1] = (s[i - 1][1] + f[i][1]) % (sz + 1);
  }
  LL pre = 0;
  for (int i = 1; i <= n; i++) {
    pre = (pre + (a[i] - '0') * pm(2, n - i, sz + 1)) % (sz + 1);
    if (i == n)
      pre = (pre + pm(2, n - i, sz + 1)) % (sz + 1);
    // cout<<i<<' '<<pre<<endl;
  }
  // cout<<pre<<endl;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '1') {
      // sz-1
      int go = (s[n][0] - pm(2, n - i, sz - 1) + sz - 1) % (sz - 1);
      gao(go);
    } else {
      int go = (s[n][1] + pm(2, n - i, sz + 1)) % (sz + 1);
      gao(go);
    }
  }
  return 0;
}