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

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

using ll = long long;

#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

ll f(ll X) {
  if (X == 0)
    return 0;
  ll pc = __builtin_popcountll(X);
  return f(X % pc) + 1;
}

void solve(ll N, std::string X) {
  ll pc = 0; // x's popcount
  rep(i, N) if (X.at(i) == '1') pc++;

  vector<ll> ans(N);

  rep(b, 2) { // b=0: 変更する桁が0の場合、 b=1: 反転する桁が1の場合
    ll npc = b == 0 ? pc + 1 : pc - 1;
    if (npc <= 0)
      continue;
    ll r0 = 0; // X (mod Popcount(X) +- 1)
    rep(i, N) {
      r0 = (r0 * 2) % npc;
      if (X.at(i) == '1')
        r0++;
    }

    ll k = 1; // 2^i
    repr(i, N - 1) {
      if (X.at(i) - '0' == b) { // 反転対象の桁の場合
        ll r = r0;              // (X + 2^k) (mod Popcount(X) +- 1)
        if (b == 0) {
          r = (r + k) % npc;
        } else {
          r = (r - k + npc) % npc;
        }
        ans.at(i) = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  rep(i, N) { cout << ans.at(i) << endl; }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  string X;
  cin >> X;

  solve(N, X);
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;

#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

map<ll, ll> memo;

ll f(ll X, int cnt) {
  if (memo.count(X))
    return memo.at(X);
  if (X <= 0)
    return cnt;

  ll pc = __builtin_popcountll(X);
  ll ret = f(X % pc, cnt + 1);
  memo.insert({X, ret});
  return ret;
}

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

void solve(ll N, std::string X) {
  ll xpc = 0; // x's popcount
  rep(i, N) {
    if (X.at(i) == '1')
      xpc++;
  }

  ll vp = 0; // X (mod Popcount(X) + 1)
  ll vm = 0; // X (mod Popcount(X) - 1)
  rep(i, N) {
    if (X.at(i) == '1') {
      vp += modpow(2, N - i - 1, xpc + 1);
      vp %= (xpc + 1);
      vm += modpow(2, N - i - 1, xpc - 1);
      if (xpc - 1 != 0) {
        vm %= (xpc - 1);
      }
    }
  }

  rep(i, N) {
    int p = N - i - 1;
    if (X.at(i) == '0') {
      ll p2 = modpow(2, p, xpc + 1);
      ll v = (vp + p2) % (xpc + 1);
      if (v < 0)
        v += (xpc + 1);
      cout << f(v, 1) << endl;
    } else {
      if (xpc - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      // xpc = 0 の場合は X.at(i) == 1
      // になり得ないのでここでは考慮しなくて良い。 以降は xpc > 1 の前提。
      ll p2 = modpow(2, p, xpc - 1);
      ll v = (vm - p2) % (xpc - 1);
      if (v < 0)
        v += (xpc - 1);
      cout << f(v, 1) << endl;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  string X;
  cin >> X;

  solve(N, X);
  return 0;
}