alexjercan/bugnet huggingface dataset:
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

typedef long long int ll;
using namespace std;

ll calc(ll num) {
  ll ret = 0;
  while (num != 0) {
    ll cnt = __builtin_popcount(num);
    num %= cnt;
    ret++;
  }
  return ret;
}
int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  string s;
  std::cin >> s;
  ll cnt = 0;

  // mod = cnt + 1 or cnt -1
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      cnt++;
  ll l = cnt - 1, r = cnt + 1;
  if (cnt == 1)
    l = 1;
  vector<ll> ans(n, -1);
  //(2) no 2^x mod
  vector<ll> L(n, 1 % l), R(n, 1 % r);
  for (int i = 1; i < n; i++)
    L[i] = (L[i - 1] * 2) % l, R[i] = (R[i - 1] * 2) % r;
  // l or r no x no mod
  ll y = 0, z = 0;

  for (int i = 0; i < n; i++) {
    if (s[n - 1 - i] == '1') {
      y = (y + L[i]) % l;
      z = (z + R[i]) % r;
    }
  }
  ll only = -1;
  for (int i = 0; i < n; i++) {
    if (only == -1 && s[i] == '1')
      only = i;
    else if (only >= 0 && s[i] == '1')
      only = -2;
  }

  if (only >= 0)
    ans[only] = 0;

  for (int i = 0; i < n; i++) {
    if (ans[i] != -1)
      continue;
    ll num = 0;
    if (s[i] == '1') {
      num = y;
      num = (num - L[n - 1 - i] + l) % l;
    } else {
      num = z;
      num = (num + R[n - 1 - i]) % r;
    }
    ll ret = 1 + calc(num);
    ans[i] = ret;
  }
  for (int i = 0; i < n; i++)
    std::cout << ans[i] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

typedef long long int ll;
using namespace std;

ll calc(ll num) {
  ll ret = 0;
  while (num != 0) {
    ll cnt = __builtin_popcount(num);
    num %= cnt;
    ret++;
  }
  return ret;
}
int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  string s;
  std::cin >> s;
  ll cnt = 0;

  // mod = cnt + 1 or cnt -1
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      cnt++;
  ll l = cnt - 1, r = cnt + 1;
  if (cnt == 1)
    cnt = 2;
  vector<ll> ans(n, -1);
  //(2) no 2^x mod
  vector<ll> L(n, 1 % l), R(n, 1 % r);
  for (int i = 1; i < n; i++)
    L[i] = (L[i - 1] * 2) % l, R[i] = (R[i - 1] * 2) % r;
  // l or r no x no mod
  ll y = 0, z = 0;

  for (int i = 0; i < n; i++) {
    if (s[n - 1 - i] == '1') {
      y = (y + L[i]) % l;
      z = (z + R[i]) % r;
    }
  }
  ll only = -1;

  for (int i = 0; i < n; i++) {
    if (only == -1 && s[i] == '1')
      only = i;
    else if (only >= 0 && s[i] == '1')
      only = -2;
  }

  if (only >= 0)
    ans[only] = 0;

  for (int i = 0; i < n; i++) {
    if (ans[i] != -1)
      continue;
    ll num = 0;
    if (s[i] == '1') {
      num = y;
      num = (num - L[n - 1 - i] + l) % l;
    } else {
      num = z;
      num = (num + R[n - 1 - i]) % r;
    }
    ll ret = 1 + calc(num);
    ans[i] = ret;
  }
  for (int i = 0; i < n; i++)
    std::cout << ans[i] << '\\n';
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  string X;
  cin >> N >> X;

  int PopCount(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1')
      ++PopCount;
  }

  vector<int> pMod(N, 1), nMod(N, 1);
  for (int i = N - 2; i >= 0; --i) {
    pMod[i] = (pMod[i + 1] << 1) % (PopCount + 1);
    if (PopCount - 1 > 0)
      nMod[i] = (nMod[i + 1] << 1) % (PopCount - 1);
  }

  // Xp := X % (PopCount + 1)
  // Xn := X % (PopCount - 1)

  int Xp(0), Xn(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      (Xp += pMod[i]) %= (PopCount + 1);
      if (PopCount - 1 > 0)
        (Xn += nMod[i]) %= (PopCount - 1);
    }
  }

  for (int i = 0; i < N; ++i) {
    int ans = 1;
    if (X[i] == '1') {
      if (PopCount - 1 <= 0) {
        cout << 0 << endl;
        continue;
      }
      int res = (Xn - nMod[i] + (PopCount - 1)) % (PopCount - 1);
      // シミュレーション
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    } else {
      int res = (Xp + pMod[i] + (PopCount + 1)) % (PopCount + 1);
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  string X;
  cin >> N >> X;

  int PopCount(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1')
      ++PopCount;
  }

  vector<int> pMod(N, 1), nMod(N, 1);
  for (int i = N - 2; i >= 0; --i) {
    pMod[i] = (pMod[i + 1] << 1) % (PopCount + 1);
    nMod[i] = (nMod[i + 1] << 1) % (PopCount - 1);
  }

  // Xp := X % (PopCount + 1)
  // Xn := X % (PopCount - 1)

  int Xp(0), Xn(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      (Xp += pMod[i]) %= (PopCount + 1);
      if (PopCount - 1 > 0)
        (Xn += nMod[i]) %= (PopCount - 1);
    }
  }

  for (int i = 0; i < N; ++i) {
    int ans = 1;
    if (X[i] == '1') {
      if (PopCount - 1 <= 0) {
        cout << 0 << endl;
        continue;
      }
      int res = (Xn - nMod[i] + (PopCount - 1)) % (PopCount - 1);
      // シミュレーション
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    } else {
      int res = (Xp + pMod[i] + (PopCount + 1)) % (PopCount + 1);
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
ll A = 0, B = 0, N, x;
ll Q(ll &i, string &S, vector<ll> &F, vector<ll> &G) {
  if (S.at(i) == '0')
    return (A + F.at(i)) % (x + 1);
  return ((B - G.at(i)) % (x - 1) + x - 1) % (x - 1);
}
ll f(ll m) {
  if (m == 0)
    return 0;
  ll i = 0, n = m;
  while (n > 0) {
    if (n % 2 == 1)
      i++;
    n /= 2;
  }
  return 1 + f(m % i);
}
signed main() {
  ll i, s = 1;
  string S, T;
  cin >> N >> S;
  T = S;
  sort(ALL(T));
  x = upper_bound(ALL(T), '0') - T.begin();
  x = N - x;
  vector<ll> F(N);
  REP(i, N) {
    F.at(N - 1 - i) = s;
    if (S.at(N - 1 - i) == '1')
      A += s;
    s *= 2;
    s %= x + 1;
    A %= x + 1;
  }
  s = 1;
  vector<ll> G(N);
  if (x <= 1) {
    REP(i, N) G.at(i) = -1;
  } else
    REP(i, N) {
      G.at(N - 1 - i) = s;
      if (S.at(N - 1 - i) == '1')
        B += s;
      s *= 2;
      s %= x - 1;
      B %= x - 1;
    }
  REP(i, N) {
    if (x == 1 && S.at(i) == '1')
      cout << 0 << endl;
    else {
      ll RES = Q(i, S, F, G);
      cout << 1 + f(RES) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
ll A = 0, B = 0, N, x;
ll Q(ll &i, string &S, vector<ll> &F, vector<ll> &G) {
  if (S.at(i) == '0')
    return (A + F.at(i)) % (x + 1);
  return ((B - G.at(i)) % (x - 1) + x - 1) % (x - 1);
}
ll f(ll m) {
  if (m == 0)
    return 0;
  ll i = 0, n = m;
  while (n > 0) {
    if (n % 2 == 1)
      i++;
    n /= 2;
  }
  return 1 + f(m % i);
}
signed main() {
  ll i, s = 1;
  string S, T;
  cin >> N >> S;
  T = S;
  sort(ALL(T));
  x = upper_bound(ALL(T), '0') - T.begin();
  x = N - x;
  vector<ll> F(N);
  REP(i, N) {
    F.at(N - 1 - i) = s;
    if (S.at(N - 1 - i) == '1')
      A += s;
    s *= 2;
    s %= x + 1;
    A %= x + 1;
  }
  s = 1;
  vector<ll> G(N);
  if (x <= 1) {
    REP(i, N) G.at(i) = -1;
  } else
    REP(i, N) {
      G.at(N - 1 - i) = s;
      if (S.at(N - 1 - i) == '1')
        B += s;
      s *= 2;
      s %= x - 1;
      B %= x - 1;
    }
  REP(i, N) {
    if (x == 1 && S.at(i) == '0')
      cout << 0 << endl;
    else {
      ll RES = Q(i, S, F, G);
      cout << 1 + f(RES) << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

#define all(x) begin(x), end(x)

using i64 = int_fast64_t;
using u64 = uint_fast64_t;
using f64 = double;
using p64 = pair<i64, i64>;

constexpr i64 INF = INT_FAST64_MAX / 2;

i64 f(i64 x) { return x ? 1 + f(x % __builtin_popcountl(x)) : 0; }

void solve() {
  i64 n;
  cin >> n;
  string x;
  cin >> x;
  i64 cnt = count(all(x), '1');

  if (cnt == 1) {
    for (i64 i = 0; i < n; i++) {
      cout << (x[i] == '1'       ? 0
               : x.back() == '1' ? 2
               : i == n - 1      ? 2
                                 : 1)
           << endl;
    }
    return;
  }

  vector<i64> modc(n), modf(n);
  i64 ci = 1, fi = 1;
  for (i64 i = 0; i < n; i++) {
    modc[n - i - 1] = ci;
    modf[n - i - 1] = fi;
    ci = (ci << 1) % (cnt + 1);
    fi = (fi << 1) % (cnt - 1);
  }

  i64 sumc = 0, sumf = 0;
  for (i64 i = 0; i < n; i++) {
    if (x[i] == '0')
      continue;
    sumc = (sumc + modc[i]) % (cnt + 1);
    sumf = (sumf + modf[i]) % (cnt - 1);
  }

  for (i64 i = 0; i < n; i++) {
    i64 y = x[i] == '0' ? (sumc + modc[i]) % (cnt + 1)
                        : (sumf - modf[i] + cnt - 1) % (cnt - 1);
    cout << 1 + f(y) << endl;
  }
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}


Error: 0

Fixed submission:

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

#define all(x) begin(x), end(x)

using i64 = int_fast64_t;
using u64 = uint_fast64_t;
using f64 = double;
using p64 = pair<i64, i64>;

constexpr i64 INF = INT_FAST64_MAX / 2;

i64 popcount(u64 x) {
  i64 res = 0;
  while (x > 0) {
    if (x & 1)
      res++;
    x >>= 1;
  }
  return res;
}

i64 f(i64 x) { return x ? 1 + f(x % popcount(x)) : 0; }

void solve() {
  i64 n;
  cin >> n;
  string x;
  cin >> x;
  i64 cnt = count(all(x), '1');

  vector<i64> modc(n), modf(n);
  i64 ci = 1, fi = 1;
  for (i64 i = 0; i < n; i++) {
    modc[n - i - 1] = ci;
    modf[n - i - 1] = fi;
    ci = (ci << 1) % (cnt + 1);
    fi = (fi << 1) % (cnt - 1);
  }

  i64 sumc = 0, sumf = 0;
  for (i64 i = 0; i < n; i++) {
    if (x[i] == '0')
      continue;
    sumc = (sumc + modc[i]) % (cnt + 1);
    sumf = (sumf + modf[i]) % (cnt - 1);
  }

  for (i64 i = 0; i < n; i++) {
    i64 y = x[i] == '0' ? (sumc + modc[i]) % (cnt + 1)
                        : (sumf - modf[i] + cnt - 1) % (cnt - 1);
    cout << 1 + f(y) << endl;
  }
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  int n;
  cin >> n;
  int count = 0;
  vector<int> vec(n);
  rep(i, n) {
    char c;
    cin >> c;
    if (c == '1') {
      count++;
      vec[i] = 1;
    } else
      vec[i] = 0;
  }
  vector<int> plus(n + 1);
  vector<int> minus(n + 1);
  vector<int> beki(n + 1), beki2(n + 1);
  plus[0] = 0;
  minus[0] = 0;
  rep(i, n) {
    int x = vec[i];
    plus[i + 1] = (plus[i] * 2 + x) % (count + 1);
  }
  if (count != 1)
    rep(i, n) {
      int x = vec[i];
      minus[i + 1] = (minus[i] * 2 + x) % (count - 1);
    }
  beki[0] = 1;
  beki2[0] = 1;
  rep(i, n) { beki[i + 1] = (beki[i] * 2) % (count + 1); }
  if (count != 1)
    rep(i, n) { beki2[i + 1] = (beki2[i] * 2) % (count - 1); }
  rep(i, n) {
    int x = vec[i];

    if (x == 0) {
      int y = plus[n];
      y += beki[n - i - 1];
      y = y % (count + 1);
      int ans = 1;
      while (y != 0) {
        bitset<30> bit(y);
        y = y % (bit.count());
        ans++;
      }
      cout << ans << endl;
    } else if (count != 1) {
      int y = minus[n];

      y = y - beki2[n - i - 1];

      y = (y + count - 1) % (count - 1);

      int ans = 1;
      while (y != 0) {
        bitset<30> bit(y);
        y = y % (bit.count());
        ans++;
      }
      cout << ans << endl;

    } else
      cout << 0 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  int n;
  cin >> n;
  int count = 0;
  vector<int> vec(n);
  rep(i, n) {
    char c;
    cin >> c;
    if (c == '1') {
      count++;
      vec[i] = 1;
    } else
      vec[i] = 0;
  }
  vector<int> plus(n + 1);
  vector<int> minus(n + 1);
  vector<int> beki(n + 1), beki2(n + 1);
  plus[0] = 0;
  minus[0] = 0;
  rep(i, n) {
    int x = vec[i];
    plus[i + 1] = (plus[i] * 2 + x) % (count + 1);
  }
  rep(i, n) {
    int x = vec[i];
    minus[i + 1] = (minus[i] * 2 + x) % (count - 1);
  }
  beki[0] = 1;
  beki2[0] = 1;
  rep(i, n) { beki[i + 1] = (beki[i] * 2) % (count + 1); }
  rep(i, n) { beki2[i + 1] = (beki2[i] * 2) % (count - 1); }
  rep(i, n) {
    int x = vec[i];

    if (x == 0) {
      int y = plus[n];
      y += beki[n - i - 1];
      y = y % (count + 1);
      int ans = 1;
      while (y != 0) {
        bitset<30> bit(y);
        y = y % (bit.count());
        ans++;
      }
      cout << ans << endl;
    } else {
      int y = minus[n];

      y = y - beki2[n - i - 1];

      y = (y + count - 1) % (count - 1);

      int ans = 1;
      while (y != 0) {
        bitset<30> bit(y);
        y = y % (bit.count());
        ans++;
      }
      cout << ans << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = int;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll a, b, c, p, pc, np, h, i, j, k, l, m, n, y;
  ll ans = 0;
  string x, xx;
  cin >> n >> x;
  vector<ll> pcm(n), pcp(n);
  pc = 0;
  for (i = 0; i < n; i++)
    if (x[i] == '1')
      pc++;
  if (pc > 1)
    pcm[n - 1] = 1 % (pc - 1);
  else
    pcm[n - 1] = 0;
  pcp[n - 1] = 1 % (pc + 1);
  for (i = n - 2; i >= 0; i--) {
    if (pc > 1)
      pcm[i] = (pcm[i + 1] * 2) % (pc - 1);
    else
      pcm[i] = 0;
    pcp[i] = (pcp[i + 1] * 2) % (pc + 1);
  }
  a = b = 0;
  for (i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      a += pcm[i];
      if (pc > 1)
        a %= pc - 1;
      b += pcp[i];
      b %= pc + 1;
    }
  }

  // printf("pc=%d a=%d b=%d\\n",pc , a , b);
  for (i = 0; i < n; i++) {
    ans = 0;
    if (x[i] == '0') {
      p = pc + 1;
      np = (b + pcp[i]) % p;
    } else {
      p = pc - 1;
      if (p != 0)
        np = (a + p - pcm[i]) % p;
      else
        np = 0;
    }
    if (p != 0)
      ans++;

    // printf("p=%d np=%d\\n",p,np);

    while (np != 0) {
      ans++;
      p = __builtin_popcount(np);
      np = np % p;
    }

    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = int;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll a, b, c, p, pc, np, h, i, j, k, l, m, n, y;
  ll ans = 0;
  string x, xx;
  cin >> n >> x;
  vector<ll> pcm(n), pcp(n);
  pc = 0;
  for (i = 0; i < n; i++)
    if (x[i] == '1')
      pc++;
  if (pc > 1)
    pcm[n - 1] = 1 % (pc - 1);
  else
    pcm[n - 1] = 0;
  pcp[n - 1] = 1 % (pc + 1);
  for (i = n - 2; i >= 0; i--) {
    if (pc > 1)
      pcm[i] = (pcm[i + 1] * 2) % (pc - 1);
    else
      pcm[i] = 0;
    pcp[i] = (pcp[i + 1] * 2) % (pc + 1);
  }
  a = b = 0;
  for (i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      a += pcm[i];
      a %= pc - 1;
      b += pcp[i];
      b %= pc + 1;
    }
  }

  // printf("pc=%d a=%d b=%d\\n",pc , a , b);
  for (i = 0; i < n; i++) {
    ans = 0;
    if (x[i] == '0') {
      p = pc + 1;
      np = (b + pcp[i]) % p;
    } else {
      p = pc - 1;
      if (p != 0)
        np = (a + p - pcm[i]) % p;
      else
        np = 0;
    }
    if (p != 0)
      ans++;

    // printf("p=%d np=%d\\n",p,np);

    while (np != 0) {
      ans++;
      p = __builtin_popcount(np);
      np = np % p;
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

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

const int MX = 200005;
char s[MX];
int p[2][MX];

int f(int n) {
  int cnt = 0;
  while (n) {
    n %= __builtin_popcount(n);
    cnt++;
  }
  return cnt;
}

int main() {
  int n, x1 = 0, x2 = 0, y = 0;
  scanf("%d %s", &n, s);
  for (int i = 0; i < n; i++)
    y += s[i] - '0';
  p[0][0] = p[1][0] = 1;
  for (int i = 1; i <= 200000; i++) {
    p[0][i] = p[0][i - 1] << 1;
    p[1][i] = p[1][i - 1] << 1;
    p[0][i] %= y + 1;
    if (y == 1)
      p[1][i] = 0;
    else
      p[1][i] %= y - 1;
  }
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    if (s[j] - '0') {
      x1 += p[0][i];
      x2 += p[1][i];
      if (x1 >= y + 1)
        x1 -= y + 1;
      if (x2 >= y - 1)
        x2 -= y - 1;
    }
  }
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    if (s[i] - '0') {
      int z = (x2 < p[1][j]) ? (x2 - p[1][j] + y - 1) : (x2 - p[1][j]);
      if (y == 1)
        printf("0\\n");
      else
        printf("%d\\n", f(z) + 1);
    } else {
      int z = (x1 + p[0][j] >= y + 1) ? (x1 + p[0][j] - y - 1) : (x1 + p[0][j]);
      printf("%d\\n", f(z) + 1);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

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

const int MX = 200005;
char s[MX];
int p[2][MX];

int f(int n) {
  int cnt = 0;
  while (n) {
    n %= __builtin_popcount(n);
    cnt++;
  }
  return cnt;
}

int main() {
  int n, x1 = 0, x2 = 0, y = 0;
  scanf("%d %s", &n, s);
  for (int i = 0; i < n; i++)
    y += s[i] - '0';
  p[0][0] = p[1][0] = 1;
  for (int i = 1; i <= 200000; i++) {
    p[0][i] = p[0][i - 1] << 1;
    p[1][i] = p[1][i - 1] << 1;
    p[0][i] %= y + 1;
    p[1][i] %= y - 1;
  }
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    if (s[j] - '0') {
      x1 += p[0][i];
      x2 += p[1][i];
      if (x1 >= y + 1)
        x1 -= y + 1;
      if (x2 >= y - 1)
        x2 -= y - 1;
    }
  }
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    if (s[i] - '0') {
      int z = (x2 < p[1][j]) ? (x2 - p[1][j] + y - 1) : (x2 - p[1][j]);
      printf("%d\\n", f(z) + 1);
    } else {
      int z = (x1 + p[0][j] >= y + 1) ? (x1 + p[0][j] - y - 1) : (x1 + p[0][j]);
      printf("%d\\n", f(z) + 1);
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

// A = B * C <-> A % N = (B % N) * (C % N) % N
// A = B + C <-> A % N = (B % N) + (C % N) % N

int main(void) {
  u64 N;
  cin >> N;

  boost::dynamic_bitset<> X;
  // string X;
  cin >> X;

  // pre cal
  u64 bcnt = X.count();
  u64 bcnt_p = bcnt + 1;
  u64 bcnt_m = (bcnt > 1) ? bcnt - 1 : 1;
  vector<u64> mod_p(N, 1);
  vector<u64> mod_m(N, 1);

  mod_p[0] = 1 % bcnt_p;
  mod_m[0] = 1 % bcnt_m;
  for (u64 i = 1; i < N; i++) {
    mod_p[i] = mod_p[i - 1] * 2 % bcnt_p;
    mod_m[i] = mod_m[i - 1] * 2 % bcnt_m;
  }

  u64 pre_ans_p = 0;
  u64 pre_ans_m = 0;
  for (u64 i = 0; i < N; i++) {
    if (X[i]) {
      pre_ans_p = (pre_ans_p + mod_p[i]) % bcnt_p;
      pre_ans_m = (pre_ans_m + mod_m[i]) % bcnt_m;
    }
  }

  for (s64 i = N - 1; i >= 0; i--) {
    boost::dynamic_bitset<> Xi = X;
    Xi.flip(i);

    // all 0
    if (Xi.none()) {
      cout << 0 << endl;
      continue;
    }

    // firstly
    u64 val;
    if (Xi[i]) {
      val = (pre_ans_p + mod_p[i]) % bcnt_p;
    } else {
      val = (pre_ans_m >= mod_m[i]) ? (pre_ans_m - mod_m[i]) % bcnt_m
                                    : (pre_ans_m + bcnt_m - mod_m[i]);
      ;
    }

    // secondly
    u64 ans = 1;
    while (val > 0) {
      ans++;
      u64 nxt = val;
      u64 mod = 0;
      while (val > 0) {
        mod += val & 1;
        val /= 2;
      }
      val = nxt % mod;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

// A = B * C <-> A % N = (B % N) * (C % N) % N
// A = B + C <-> A % N = (B % N) + (C % N) % N

int main(void) {
  u64 N;
  cin >> N;

  boost::dynamic_bitset<> X;
  // string X;
  cin >> X;

  // pre cal
  u64 bcnt = X.count();
  u64 bcnt_p = bcnt + 1;
  u64 bcnt_m = (bcnt > 0) ? bcnt - 1 : 1;
  vector<u64> mod_p(N, 1);
  vector<u64> mod_m(N, 1);

  mod_p[0] = 1 % bcnt_p;
  mod_m[0] = 1 % bcnt_m;
  for (u64 i = 1; i < N; i++) {
    mod_p[i] = mod_p[i - 1] * 2 % bcnt_p;
    mod_m[i] = mod_m[i - 1] * 2 % bcnt_m;
  }

  u64 pre_ans_p = 0;
  u64 pre_ans_m = 0;
  for (u64 i = 0; i < N; i++) {
    if (X[i]) {
      pre_ans_p = (pre_ans_p + mod_p[i]) % bcnt_p;
      pre_ans_m = (pre_ans_m + mod_m[i]) % bcnt_m;
    }
  }

  for (s64 i = N - 1; i >= 0; i--) {
    boost::dynamic_bitset<> Xi = X;
    Xi.flip(i);

    // all 0
    if (Xi.none()) {
      cout << 0 << endl;
      continue;
    }

    // firstly
    u64 val;
    if (Xi[i]) {
      val = (pre_ans_p + mod_p[i]) % bcnt_p;
    } else {
      val = (pre_ans_m >= mod_m[i]) ? (pre_ans_m - mod_m[i]) % bcnt_m
                                    : (pre_ans_m + bcnt_m - mod_m[i]);
      ;
    }

    // secondly
    u64 ans = 1;
    while (val > 0) {
      ans++;
      u64 nxt = val;
      u64 mod = 0;
      while (val > 0) {
        mod += val & 1;
        val /= 2;
      }
      val = nxt % mod;
    }

    cout << ans << endl;
  }

  return 0;
}