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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define int ll
int N;
string X;

ll qexp(ll x, ll y, int MOD) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
      res %= MOD;
    }
    y = y >> 1;
    x = x * x;
    x %= MOD;
  }
  return res;
}

int32_t main() {
  fast;

  cin >> N >> X;

  int popc = 0;
  aFOR(i, X) if (i == '1') popc++;

  int numm = 0, nump = 0;
  FOR(i, 0, N - 1) {
    if (X[i] == '1' && popc > 1) {
      numm += qexp(2, N - i - 1, popc - 1);
      numm %= popc - 1;
    }
  }

  FOR(i, 0, N - 1) {
    if (X[i] == '1') {
      nump += qexp(2, N - i - 1, popc + 1);
      nump %= popc + 1;
    }
  }

  FOR(i, 0, N - 1) {
    int curnum;
    if (X[i] == '1') {
      if (popc == 1) {
        cout << 0 << '\\n';
        continue;
      }
      curnum = numm + popc - 1 - qexp(2, N - i - 1, popc - 1);
      curnum %= popc - 1;
      while (curnum < 0)
        curnum += popc - 1;
    } else {
      curnum = nump + qexp(2, N - i - 1, popc + 1);
      curnum %= popc + 1;
    }

    int ans = 1;
    while (curnum > 0) {
      int curpopc = __builtin_popcountll(curnum);
      // FOR(j,0,50) if ((1LL << j) & curnum) curpopc++;
      curnum %= curpopc;
      ans++;
    }
    cout << ans << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define int ll
int N;
string X;

ll qexp(ll x, ll y, int MOD) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
      res %= MOD;
    }
    y = y >> 1;
    x = x * x;
    x %= MOD;
  }
  return res;
}

int32_t main() {
  fast;

  cin >> N >> X;

  int popc = 0;
  aFOR(i, X) if (i == '1') popc++;

  int numm = 0, nump = 0;
  FOR(i, 0, N - 1) {
    if (X[i] == '1') {
      numm += qexp(2, N - i - 1, popc - 1);
      numm %= popc - 1;
    }
  }

  FOR(i, 0, N - 1) {
    if (X[i] == '1') {
      nump += qexp(2, N - i - 1, popc + 1);
      nump %= popc + 1;
    }
  }

  FOR(i, 0, N - 1) {
    int curnum;
    if (X[i] == '1') {
      curnum = numm + popc - 1 - qexp(2, N - i - 1, popc - 1);
      curnum %= popc - 1;
    } else {
      curnum = nump + qexp(2, N - i - 1, popc + 1);
      curnum %= popc + 1;
    }

    int ans = 1;
    while (curnum > 0) {
      int curpopc = 0;
      FOR(j, 0, 50) if ((1LL << j) & curnum) curpopc++;
      curnum %= curpopc;
      ans++;
    }
    cout << ans << '\\n';
  }
}