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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000 * 100 + 1

#define numberOfSetBits(S)                                                     \\
  __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x, y) memset(x, y, sizeof(x))
#define gcd(a, b) __gcd(a, b)
#define all(x) x.begin(), x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << ' ';                                                         \\
  cout << endl;
#define printii(x)                                                             \\
  for (auto it : x)                                                            \\
    cout << it.F << ' ' << it.S << '\\t';                                       \\
  cout << endl;
#define in(x, n)                                                               \\
  for (int e = 0; e < n; e++) {                                                \\
    ll y;                                                                      \\
    cin >> y;                                                                  \\
    x.pb(y);                                                                   \\
  }

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll, ll>
#define pll pair<ll, ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii, ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a, b, c) mp(mp(a, b), c)

ll power(ll x, unsigned ll y, unsigned ll m = MOD) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, x, t, m, y, z, p1, p2;
  cin >> n;
  vi ans(n, 1), a(n);
  string s;
  cin >> s;
  reverse(all(s));
  z = 0;
  for (int i = 0; i < n; i++) {
    z += (s[i] == '1');
  }
  p1 = z - 1;
  p2 = z + 1;
  x = 0;
  y = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (p1) {
        x += (power(2, i, p1));
        x %= p1;
      }
      y += (power(2, i, p2));
      y %= p2;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      a[i] = x;
      if (p1) {
        a[i] -= power(2, i, p1);
        a[i] %= p1;
        if (a[i] < 0)
          a[i] += p1;
      } else {
        ans[i] = 0;
      }
    } else {
      a[i] = y;
      a[i] += power(2, i, p2);
      a[i] %= p2;
      if (a[i] < 0)
        a[i] += p2;
    }
  }
  for (int i = 0; i < n; i++) {
    while (a[i]) {
      ans[i]++;
      a[i] %= __builtin_popcountll(a[i]);
    }
  }
  reverse(all(ans));
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000 * 100 + 1

#define numberOfSetBits(S)                                                     \\
  __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x, y) memset(x, y, sizeof(x))
#define gcd(a, b) __gcd(a, b)
#define all(x) x.begin(), x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << ' ';                                                         \\
  cout << endl;
#define printii(x)                                                             \\
  for (auto it : x)                                                            \\
    cout << it.F << ' ' << it.S << '\\t';                                       \\
  cout << endl;
#define in(x, n)                                                               \\
  for (int e = 0; e < n; e++) {                                                \\
    ll y;                                                                      \\
    cin >> y;                                                                  \\
    x.pb(y);                                                                   \\
  }

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll, ll>
#define pll pair<ll, ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii, ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a, b, c) mp(mp(a, b), c)

ll power(ll x, unsigned ll y, unsigned ll m = MOD) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, x, t, m, y, z, p1, p2;
  cin >> n;
  vi ans(n, 1), a(n);
  string s;
  cin >> s;
  reverse(all(s));
  z = 0;
  for (int i = 0; i < n; i++) {
    z += (s[i] == '1');
  }
  p1 = z - 1;
  p2 = z + 1;
  x = 0;
  y = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (p1) {
        x += (power(2, i, p1));
        x %= p1;
      }
      y += (power(2, i, p2));
      y %= p2;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      a[i] = x;
      a[i] -= power(2, i, p1);
      if (p1) {
        a[i] %= p1;
        if (a[i] < 0)
          a[i] += p1;
      }
    } else {
      a[i] = y;
      a[i] += power(2, i, p2);
      a[i] %= p2;
      if (a[i] < 0)
        a[i] += p2;
    }
  }
  for (int i = 0; i < n; i++) {
    while (a[i]) {
      ans[i]++;
      a[i] %= __builtin_popcountll(a[i]);
    }
  }
  reverse(all(ans));
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\\n";
  }

  return 0;
}