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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v)                                                                \\
  sort(all(v));                                                                \\
  v.resize(unique(all(v)) - v.begin());
#define INF (ll)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) {
  ++y;
  return (rng() % (y - x)) + x;
} // inclusivesss

ll n, minu, add, xpop;
string x;

ll qexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1, base = a;
  while (b) {
    if (b & 1) {
      res *= base;
      res %= m;
    }
    base *= base;
    base %= m;
    b >>= 1;
  }
  return res % m;
}

int main() {
  fastio;
  cin >> n >> x;
  for (auto it : x)
    if (it == '1')
      xpop++;
  FOR(i, 0, n - 1) if (x[i] == '1') {
    if (xpop > 1)
      minu = (minu + qexp(2, n - 1 - i, xpop - 1)) % (xpop - 1);
    add = (add + qexp(2, n - 1 - i, xpop + 1)) % (xpop + 1);
  }
  FOR(i, 0, n - 1) {
    ll y = 0, ans = 1;
    if (x[i] == '0')
      y = (add + qexp(2, n - 1 - i, xpop + 1)) % (xpop + 1);
    else if (xpop > 1)
      y = (minu - qexp(2, n - 1 - i, xpop - 1) + xpop - 1) % (xpop - 1);
    else {
      cout << "0\\n";
      continue;
    }
    while (y != 0) {
      y %= __builtin_popcount(y);
      ans++;
    }
    cout << ans << "\\n";
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v)                                                                \\
  sort(all(v));                                                                \\
  v.resize(unique(all(v)) - v.begin());
#define INF (ll)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) {
  ++y;
  return (rng() % (y - x)) + x;
} // inclusivesss

ll n, minu, add, xpop;
string x;

ll qexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1, base = a;
  while (b) {
    if (b & 1) {
      res *= base;
      res %= m;
    }
    base *= base;
    base %= m;
    b >>= 1;
  }
  return res % m;
}

int main() {
  fastio;
  cin >> n >> x;
  for (auto it : x)
    if (it == '1')
      xpop++;
  FOR(i, 0, n - 1) if (x[i] == '1') {
    if (xpop > 0)
      minu = (minu + qexp(2, n - 1 - i, xpop - 1)) % (xpop - 1);
    add = (add + qexp(2, n - 1 - i, xpop + 1)) % (xpop + 1);
  }
  FOR(i, 0, n - 1) {
    if (x[i] == '0') {
      ll x = (add + qexp(2, n - 1 - i, xpop + 1)) % (xpop + 1), ans = 1;
      while (x != 0) {
        x %= __builtin_popcount(x);
        ans++;
      }
      cout << ans << "\\n";
    } else if (x[i] == '1') {
      ll x = (minu - qexp(2, n - 1 - i, xpop - 1) + xpop - 1) % (xpop - 1),
         ans = 1;
      while (x != 0) {
        x %= __builtin_popcount(x);
        ans++;
      }
      cout << ans << "\\n";
    }
  }
}