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

#include <bits/stdc++.h>

// isDebug?
#ifndef ONLINE_JUDGE
#define _DEBUG
#endif

// Function Macros:
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define setpre(n) cout << fixed << setprecision(n)

// -* https://xn--kst.jp/blog/2019/08/29/cpp-comp/ *- Thx :)
// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  cout << __FILE__ << ": " << __LINE__ << ": " << __func__ << " -> ";          \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif

// namespace / type / class / struct - alias, def:
using namespace std;

using ll = long long;
using ull = unsigned long long;

// Constants:
const int INF = (1ll << 30) - 1; // approx 1.07e9
const ll INFLL = 1ll << 60;      // approx 1.15e18
const int MOD = 1e9 + 7;         // 998244353
const long double EPS = 1.0e-14;
const int FACN = 510000;

// Global Variables:
// ll fac[FACN], finv[FACN], inv[FACN];

// Prototype Declarations:
ll gcd(ll, ll);
ll lcm(ll, ll);
template <class T> inline bool chmin(T &, T);
template <class T> inline bool chmax(T &, T);

// ================ Main Loop ================
int main() {
  int n, initPops = 0;
  string x;
  cin >> n >> x;
  vector<bool> bx(n);
  vector<int> ans(n);
  rep(i, n) {
    bx[i] = (x[i] == '1');
    initPops += (x[i] == '1');
  }
  int popP, popN, curBaseP = 0, curBaseN = 0;
  if (n == 1) {
    if (bx[0]) {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }
    return 0;
  }
  if (initPops >= n) {
    popP = n; // expt.
    popN = n - 1;
  } else if (initPops <= 0) {
    rep(i, n) { cout << 1 << '\\n'; }
    return 0;
  } else if (initPops <= 1) {
    popP = 2;
    popN = 1;
  } else {
    popP = initPops + 1;
    popN = initPops - 1;
  }
  rep(i, n) {
    curBaseP *= 2;
    curBaseN *= 2;
    curBaseP += bx[i];
    curBaseN += bx[i];
    curBaseP %= popP;
    curBaseN %= popN;
  }

  int baseP = 1, baseN = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (bx[i] && initPops == 1) {
      ans[i] = 0;
      continue;
    }

    if (i != n - 1) {
      baseP *= 2;
      baseN *= 2;
      baseP %= popP;
      baseN %= popN;
    }

    int cur, popcnt;
    if (bx[i]) {
      cur = curBaseN;
      popcnt = initPops - 1;
      cur -= baseN;
    } else {
      cur = curBaseP;
      popcnt = initPops + 1;
      cur += baseP;
    }

    if (popcnt == 0) {
      ans[i] = 0;
      continue;
    }

    cur %= popcnt;
    if (cur < 0)
      cur += popcnt;
    ++ans[i];

    while (cur != 0) {
      cur = cur % __builtin_popcount(cur);
      ++ans[i];
    }
    // debug(i, ans[i]);
  }
  rep(i, n) { cout << ans[i] << '\\n'; }

  /*
  rep(i, n) {
      pb = bx;
      pb[i] = !pb[i];
      int popcnt = initPops + pb[i] - !pb[i];

      int j = 0, cur = 0;
      for(j = 0; j < n; j++) {
          cur *= 2;
          cur += pb[j];
          cur %= popcnt;
      }
  }
  */

  return 0;
}
// ===========================================

// Functions:
// 最大公約数 O(log(min(a, b)))
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数 O(log(min(a, b)))
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// isDebug?
#ifndef ONLINE_JUDGE
#define _DEBUG
#endif

// Function Macros:
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define setpre(n) cout << fixed << setprecision(n)

// -* https://xn--kst.jp/blog/2019/08/29/cpp-comp/ *- Thx :)
// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  cout << __FILE__ << ": " << __LINE__ << ": " << __func__ << " -> ";          \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif

// namespace / type / class / struct - alias, def:
using namespace std;

using ll = long long;
using ull = unsigned long long;

// Constants:
const int INF = (1ll << 30) - 1; // approx 1.07e9
const ll INFLL = 1ll << 60;      // approx 1.15e18
const int MOD = 1e9 + 7;         // 998244353
const long double EPS = 1.0e-14;
const int FACN = 510000;

// Global Variables:
// ll fac[FACN], finv[FACN], inv[FACN];

// Prototype Declarations:
ll gcd(ll, ll);
ll lcm(ll, ll);
template <class T> inline bool chmin(T &, T);
template <class T> inline bool chmax(T &, T);

// ================ Main Loop ================
int main() {
  int n, initPops = 0;
  string x;
  cin >> n >> x;
  vector<bool> bx(n);
  vector<ll> ans(n);
  rep(i, n) {
    bx[i] = (x[i] == '1');
    initPops += (x[i] == '1');
  }
  ll popLcm, curBase = 0;
  if (initPops >= n) {
    popLcm = n - 1;
  } else if (initPops <= 0) {
    rep(i, n) { cout << 1 << '\\n'; }
    return 0;
  } else if (initPops <= 1) {
    popLcm = 2;
  } else {
    popLcm = lcm(initPops + 1, initPops - 1);
  }
  rep(i, n) {
    curBase *= 2;
    curBase += bx[i];
    curBase %= popLcm;
  }

  ll base = 1;
  for (int i = n - 1; i >= 0; i--) {
    ll cur = curBase;
    int popcnt = initPops + !bx[i] - bx[i];
    if (i != n - 1)
      base *= 2;
    base %= popLcm;
    cur -= base * bx[i];
    cur += base * !bx[i];
    if (popcnt == 0) {
      ans[i] = 0;
      continue;
    }
    cur %= popcnt;
    if (cur < 0)
      cur += popcnt;
    ++ans[i];
    while (cur != 0) {
      cur = cur % __builtin_popcountll(cur);
      ++ans[i];
    }
    // debug(ans[i]);
  }
  rep(i, n) { cout << ans[i] << '\\n'; }

  /*
  rep(i, n) {
      pb = bx;
      pb[i] = !pb[i];
      int popcnt = initPops + pb[i] - !pb[i];

      int j = 0, cur = 0;
      for(j = 0; j < n; j++) {
          cur *= 2;
          cur += pb[j];
          cur %= popcnt;
      }
  }
  */

  return 0;
}
// ===========================================

// Functions:
// 最大公約数 O(log(min(a, b)))
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数 O(log(min(a, b)))
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}