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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1')
      ++cnt;
  }
  int c[] = {cnt - 1, cnt + 1};
  int _2[n][2];
  if (c[0] != 0)
    _2[n - 1][0] = (1 % c[0]);
  _2[n - 1][1] = (1 % c[1]);
  for (int i = n - 2; i >= 0; --i) {
    for (int j = 0; j < 2; ++j) {
      _2[i][j] = _2[i + 1][j] << 1;
      if (c[0] != 0)
        _2[i][j] %= c[j];
    }
  }
  int num[2] = {};
  for (int i = 0; i < n; ++i) {
    if (x[i] != '1')
      continue;
    for (int j = 0; j < 2; ++j) {
      num[j] += _2[i][j];
      if (c[0] != 0)
        num[j] %= c[j];
    }
  }
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (c[0] == 0) {
        cout << 0 << '\\n';
        continue;
      }
      int a = (c[0] + ((num[0] - _2[i][0]) % c[0])) % c[0];
      int ans = 1;
      while (a > 0) {
        a %= __builtin_popcount(a);
        ++ans;
      }
      cout << ans << '\\n';
    } else {
      int a = (num[1] + _2[i][1]) % c[1];
      int ans = 1;
      while (a > 0) {
        a %= __builtin_popcount(a);
        ++ans;
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1')
      ++cnt;
  }
  int c[] = {cnt - 1, cnt + 1};
  int _2[n][2];
  _2[n - 1][0] = (1 % c[0]);
  _2[n - 1][1] = (1 % c[1]);
  for (int i = n - 2; i >= 0; --i) {
    for (int j = 0; j < 2; ++j) {
      _2[i][j] = _2[i + 1][j] << 1;
      _2[i][j] %= c[j];
    }
  }
  int num[2] = {};
  for (int i = 0; i < n; ++i) {
    if (x[i] != '1')
      continue;
    for (int j = 0; j < 2; ++j) {
      num[j] += _2[i][j];
      num[j] %= c[j];
    }
  }
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (c[0] == 0) {
        cout << 0 << '\\n';
        continue;
      }
      int a = (c[0] + ((num[0] - _2[i][0]) % c[0])) % c[0];
      int ans = 1;
      while (a > 0) {
        a %= __builtin_popcount(a);
        ++ans;
      }
      cout << ans << '\\n';
    } else {
      int a = (num[1] + _2[i][1]) % c[1];
      int ans = 1;
      while (a > 0) {
        a %= __builtin_popcount(a);
        ++ans;
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

ll modpow(ll a, ll n, ll modd = 1e9 + 7) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % modd;
    a = a * a % modd;
    n >>= 1;
  }
  return res;
}

int popcnt(int num) {
  int ans = 0;
  while (num > 0) {
    num &= num - 1;
    ans++;
  }
  return ans;
}

int filler(int num) {
  if (num == 0) {
    return 0;
  }
  int res = num % popcnt(num);
  return filler(res) + 1;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  int basemod = 0;
  int onebase = 0;
  int zerobase = 0;
  rep(i, n) if (x[i] == '1') basemod++;

  rep(i, n) {
    onebase = (onebase * 2 + int(x[i] - '0')) % (basemod + 1);
    if (basemod == 1)
      continue;
    zerobase = (zerobase * 2 + int(x[i] - '0')) % (basemod - 1);
  }
  rep(i, n) {
    int modthis = basemod;
    if (x[i] == '1') {
      int num = zerobase;
      modthis--;
      if (modthis == 0) {
        cout << 0 << endl;
        continue;
      }
      if (modthis == 1) {
        cout << 1 << endl;
        continue;
      }
      num -= modpow(2, n - i - 1, modthis);
      if (num < 0)
        num += modthis;
      cout << filler(num) + 1 << endl;
    } else {
      int num = onebase;
      modthis++;
      if (modthis == 1) {
        cout << 1 << endl;
        continue;
      }
      num += modpow(2, n - i - 1, modthis);
      num %= modthis;
      cout << filler(num) + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

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

ll modpow(ll a, ll n, ll modd = 1e9 + 7) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % modd;
    a = a * a % modd;
    n >>= 1;
  }
  return res;
}

int popcnt(int num) {
  int ans = 0;
  while (num > 0) {
    num &= num - 1;
    ans++;
  }
  return ans;
}

int filler(int num) {
  if (num == 0) {
    return 0;
  }
  int res = num % popcnt(num);
  return filler(res) + 1;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  int basemod = 0;
  int onebase = 0;
  int zerobase = 0;
  rep(i, n) if (x[i] == '1') basemod++;
  rep(i, n) {
    onebase = (onebase * 2 + int(x[i] - '0')) % (basemod + 1);
    zerobase = (zerobase * 2 + int(x[i] - '0')) % (basemod - 1);
  }
  rep(i, n) {
    int modthis = basemod;
    if (x[i] == '1') {
      int num = zerobase;
      modthis--;
      if (modthis == 0) {
        cout << 0 << endl;
        continue;
      }
      if (modthis == 1) {
        cout << 1 << endl;
        continue;
      }
      num -= modpow(2, n - i - 1, modthis);
      if (num < 0)
        num += modthis;
      cout << filler(num) + 1 << endl;
    } else {
      int num = onebase;
      modthis++;
      if (modthis == 1) {
        cout << 1 << endl;
        continue;
      }
      num += modpow(2, n - i - 1, modthis);
      num %= modthis;
      cout << filler(num) + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define lp(i, n) for (int i = 0; i < (n); ++i)
#define sz(v) (ll)(v).size()
#define int long long

int fast_pow(int a, int p, int mod) {
  if (p == 0)
    return 1;
  if (p == 1)
    return a % mod;
  int ans = fast_pow(a, p >> 1, mod);
  ans = (ans * ans) % mod;
  if (p & 1)
    ans = a * ans;
  return ans % mod;
}

int get(int c) {
  int nm = 0;
  while (c) {
    c &= (c - 1);
    nm++;
  }
  return nm;
}

void solve() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  int f = 0, s = 0, z = 0;
  lp(i, str.size()) if (str[i] == '1') z++;
  f = z + 1;
  s = z - 1;
  int an1 = 0, an2 = 0, an = 0;
  lp(i, str.size()) {
    if (str[i] == '1') {
      an1 += fast_pow(2, n - (i + 1), f);
      if (s)
        an2 += fast_pow(2, n - (i + 1), s);
    }
  }
  map<int, int> mp;
  for (int i = 1; i <= 200001; i++) {
    int cnt = 0, k = i;
    while (k != 0) {
      cnt++;
      k %= get(k);
    }
    mp[i] = cnt;
  }

  for (int i = 0; i < n; i++) {
    if (str[i] == '0') {
      an = ((an1 + fast_pow(2, n - (i + 1), f) % f) + f) % f;
      cout << 1 + mp[an] << "\\n";
    } else {
      if (s == 0) {
        an = 0;
        cout << "0\\n";
      } else {
        an = ((an2 - fast_pow(2, n - (i + 1), s) % s) + s) % s;
        cout << 1 + mp[an] << "\\n";
      }
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define lp(i, n) for (int i = 0; i < (n); ++i)
#define sz(v) (ll)(v).size()
#define int long long

int fast_pow(int a, int p, int mod) {
  if (p == 0)
    return 1;
  if (p == 1)
    return a % mod;
  int ans = fast_pow(a, p >> 1, mod);
  ans = (ans * ans) % mod;
  if (p & 1)
    ans = a * ans;
  return ans % mod;
}

int get(int c) {
  int nm = 0;
  while (c) {
    c &= (c - 1);
    nm++;
  }
  return nm;
}

void solve() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  int f = 0, s = 0, z = 0;
  lp(i, str.size()) if (str[i] == '1') z++;
  f = z + 1;
  s = z - 1;
  int an1 = 0, an2 = 0, an = 0;
  lp(i, str.size()) {
    if (str[i] == '1') {
      an1 += fast_pow(2, n - (i + 1), f);
      an2 += fast_pow(2, n - (i + 1), s);
    }
  }
  map<int, int> mp;
  for (int i = 1; i <= 200001; i++) {
    int cnt = 0, k = i;
    while (k != 0) {
      cnt++;
      k %= get(k);
    }
    mp[i] = cnt;
  }

  for (int i = 0; i < n; i++) {
    if (str[i] == '0') {
      an = ((an1 + fast_pow(2, n - (i + 1), f) % f) + f) % f;
    } else {
      if (s == 0) {
        an = 0;
      } else
        an = ((an2 - fast_pow(2, n - (i + 1), s) % s) + s) % s;
    }
    cout << 1 + mp[an] << "\\n";
  }
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

int main(void) {
  int64_t N;
  string X;
  cin >> N >> X;
  int64_t cnt = count(X.begin(), X.end(), '1');

  int64_t m1 = 0, m2 = 0;
  for (int64_t i = 0; i < N; i++) {
    if (X[i] == '1') {
      m2 += mod_pow(2L, N - 1 - i, cnt + 1);
      m2 %= cnt + 1;
      if (cnt <= 1)
        continue;
      m1 += mod_pow(2L, N - 1 - i, cnt - 1);
      m1 %= cnt - 1;
    }
  }

  vector<int64_t> f(300000);
  for (int64_t i = 1; i < 300000; i++) {
    f[i] = f[i % __builtin_popcountll(i)] + 1;
  }

  for (int64_t i = 0; i < N; i++) {
    if (cnt == 1 && X[i] == '1') {
      cout << 0 << endl;
    } else if (X[i] == '1') {
      int64_t tmp = m1 - mod_pow(2L, N - 1 - i, cnt - 1);
      tmp %= cnt - 1;
      if (tmp < 0)
        tmp += cnt - 1;
      cout << f[tmp] + 1 << endl;
    } else {
      int64_t tmp = m2 + mod_pow(2L, N - 1 - i, cnt + 1);
      tmp %= cnt + 1;
      if (tmp < 0)
        tmp += cnt + 1;
      cout << f[tmp] + 1 << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

int main(void) {
  int64_t N;
  string X;
  cin >> N >> X;
  int64_t cnt = count(X.begin(), X.end(), '1');

  int64_t m1 = 0, m2 = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      m1 += mod_pow(2L, N - 1 - i, cnt - 1);
      m1 %= cnt - 1;
      m2 += mod_pow(2L, N - 1 - i, cnt + 1);
      m2 %= cnt + 1;
    }
  }

  vector<int64_t> f(300000);
  for (int64_t i = 1; i < 300000; i++) {
    f[i] = f[i % __builtin_popcountll(i)] + 1;
  }

  for (int64_t i = 0; i < N; i++) {
    if (X[i] == '1') {
      int64_t tmp = m1 - mod_pow(2L, N - 1 - i, cnt - 1);
      tmp %= cnt - 1;
      if (tmp < 0)
        tmp += cnt - 1;
      cout << f[tmp] + 1 << endl;
    } else {
      int64_t tmp = m2 + mod_pow(2L, N - 1 - i, cnt + 1);
      tmp %= cnt + 1;
      if (tmp < 0)
        tmp += cnt + 1;
      cout << f[tmp] + 1 << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, pwm1[200020], pw[200020], pwp1[200020], num = 0, resm1 = 0, res = 0,
                                              resp1 = 0;
string s;

int get(LL x) {
  int ret = 0, y;
  while (x > 0) {
    y = __builtin_popcount(x);
    x %= y;
    ret++;
  }
  return ret;
}

int main() {
  cin >> n >> s;
  rep(i, n) num += s[i] - '0';
  pwm1[0] = pw[0] = pwp1[0] = 1;
  repn(i, 200010) {
    if (num - 1 > 0)
      pwm1[i] = pwm1[i - 1] * 2 % (num - 1);
    if (num > 0)
      pw[i] = pw[i - 1] * 2 % num;
    pwp1[i] = pwp1[i - 1] * 2 % (num + 1);
  }
  rep(i, n) {
    if (num - 1 > 0)
      resm1 = (resm1 + (s[i] == '1') * pwm1[n - i - 1]) % (num - 1);
    if (num > 0)
      res = (res + (s[i] == '1') * pw[n - i - 1]) % num;
    resp1 = (resp1 + (s[i] == '1') * pwp1[n - i - 1]) % (num + 1);
  }
  rep(i, n) {
    if (s[i] == '0') {
      LL tmp = (resp1 + pwp1[n - i - 1]) % (num + 1);
      printf("%d\\n", get(tmp) + 1);
    } else {
      if (num == 1) {
        puts("0");
        continue;
      }
      LL tmp = (resm1 - pwm1[n - i - 1] + num - 1 + num - 1) % (num - 1);
      printf("%d\\n", get(tmp) + 1);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, pwm1[200020], pw[200020], pwp1[200020], num = 0, resm1 = 0, res = 0,
                                              resp1 = 0;
string s;

int get(LL x) {
  int ret = 0, y;
  while (x > 0) {
    y = __builtin_popcount(x);
    x %= y;
    ret++;
  }
  return ret;
}

int main() {
  cin >> n >> s;
  rep(i, n) num += s[i] - '0';
  pwm1[0] = pw[0] = pwp1[0] = 1;
  repn(i, 200010) {
    if (num - 1 > 0)
      pwm1[i] = pwm1[i - 1] * 2 % (num - 1);
    if (num > 0)
      pw[i] = pw[i - 1] * 2 % num;
    pwp1[i] = pwp1[i - 1] * 2 % (num + 1);
  }
  rep(i, n) {
    if (num - 1 > 0)
      resm1 = (resm1 + (s[i] == '1') * pwm1[n - i - 1]) % (num - 1);
    if (num > 0)
      res = (res + (s[i] == '1') * pw[n - i - 1]) % num;
    resp1 = (resp1 + (s[i] == '1') * pwp1[n - i - 1]) % (num + 1);
  }
  rep(i, n) {
    if (s[i] == '0') {
      LL tmp = (resp1 + pwp1[n - i - 1]) % (num + 1);
      printf("%d\\n", get(tmp) + 1);
    } else {
      LL tmp = (resm1 - pwm1[n - i - 1] + num - 1 + num - 1) % (num - 1);
      printf("%d\\n", get(tmp) + 1);
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int siz = 2e5;
const long inf = 1e9;
int calc(int x) {
  int res = 0;
  while (x > 0) {
    int k = 0;
    rep(i, 0, 20) if (x & (1 << i)) k++;
    x %= k;
    res++;
  }
  return res;
}
long modpow(long x, int n, long p) {
  int i = 0;
  long res = 1;
  while (n >= (1 << i)) {
    if (n & (1 << i))
      (res *= x) %= p;
    i++;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  int N;
  string s;
  cin >> N >> s;
  int cnt = 0;
  int A = 0, B = 0;
  rep(i, 0, N) cnt += s[i] - '0';
  rep(i, 0, N) {
    if (cnt > 1)
      ((A *= 2) += s[i] - '0') %= (cnt - 1);
    ((B *= 2) += s[i] - '0') %= (cnt + 1);
  }
  int t;
  rep(i, 0, N) {
    if (cnt == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    if (s[i] == '1') {
      t = (A - modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      (t += cnt - 1) %= (cnt - 1);
    } else {
      t = (B + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      (t += cnt + 1) %= (cnt + 1);
    }
    cout << calc(t) + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int siz = 2e5;
const long inf = 1e9;
int calc(int x) {
  int res = 0;
  while (x > 0) {
    int k = 0;
    rep(i, 0, 20) if (x & (1 << i)) k++;
    x %= k;
    res++;
  }
  return res;
}
long modpow(long x, int n, long p) {
  int i = 0;
  long res = 1;
  while (n >= (1 << i)) {
    if (n & (1 << i))
      (res *= x) %= p;
    i++;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  int N;
  string s;
  cin >> N >> s;
  int cnt = 0;
  int A = 0, B = 0;
  rep(i, 0, N) cnt += s[i] - '0';
  if (N == 1 && cnt == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, 0, N) {
    ((A *= 2) += s[i] - '0') %= (cnt - 1);
    ((B *= 2) += s[i] - '0') %= (cnt + 1);
  }
  int t;
  rep(i, 0, N) {
    if (s[i] == '1') {
      t = (A - modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      (t += cnt - 1) %= (cnt - 1);
    } else {
      t = (B + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      (t += cnt + 1) %= (cnt + 1);
    }
    cout << calc(t) + 1 << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_one(i, n) for (int i = 1; i < n; i++)
using namespace std;
typedef long long ll;

int solve(int x, int count) {
  if (x == -1)
    return 0;
  if (x == 0)
    return count;
  int y = x;
  int pc = 0;
  while (y != 0) {
    pc += y % 2;
    y /= 2;
  }
  return solve(x % pc, count + 1);
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  ll xp = 0, xm = 0;
  int pc = 0;
  int x_list[n];
  rep(i, n) {
    x_list[i] = (int)(x[i] - '0');
    pc += x_list[i];
  }
  rep(i, n) {
    xp = (xp * 2) % (pc + 1);
    xp += x_list[i];
    if (pc - 1 > 0) {
      xm = (xm * 2) % (pc - 1);
      xm += x_list[i];
    }
  }
  int xi[n];
  ll pm = 1, pp = 1;
  for (int i = n - 1; i > -1; i--) {
    if (x_list[i]) {
      if (pc - 1 < 1) {
        xi[i] = -1;
        continue;
      }
      xi[i] = (xm - pm + pc - 1) % (pc - 1);
    } else {
      xi[i] = (xp + pp) % (pc + 1);
    }
    pp = (2 * pp) % (pc + 1);
    if (pc - 1 > 0)
      pm = (2 * pm) % (pc - 1);
  }
  rep(i, n) { cout << solve(xi[i], 1) << endl; }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_one(i, n) for (int i = 1; i < n; i++)
using namespace std;
typedef long long ll;

int solve(int x, int count) {
  if (x == -1)
    return 0;
  if (x == 0)
    return count;
  int y = x;
  int pc = 0;
  while (y != 0) {
    pc += y % 2;
    y /= 2;
  }
  return solve(x % pc, count + 1);
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  ll xp = 0, xm = 0;
  int pc = 0;
  int x_list[n];
  rep(i, n) {
    x_list[i] = (int)(x[i] - '0');
    pc += x_list[i];
  }
  rep(i, n) {
    xp = (xp * 2) % (pc + 1);
    xp += x_list[i];
    if (pc - 1 > 0) {
      xm = (xm * 2) % (pc - 1);
      xm += x_list[i];
    }
  }
  int xi[n];
  ll pm = 1, pp = 1;
  for (int i = n - 1; i > -1; i--) {
    if (x_list[i]) {
      if (pc - 1 < 1) {
        xi[i] = -1;
        continue;
      }
      xi[i] = (xm - pm + pc - 1) % (pc - 1);
      pp = (2 * pp) % (pc + 1);
    } else {
      xi[i] = (xp + pp) % (pc + 1);
    }
    pm = (2 * pm) % (pc - 1);
  }
  rep(i, n) { cout << solve(xi[i], 1) << endl; }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  string s;
  cin >> n >> s;
  int cnt = count(s.begin(), s.end(), '1');
  int x0 = 0, x1 = 0;
  auto modpow = [](auto a, auto e, int m) {
    long long r = 1, x = a % m;
    for (; e > 0; e >>= 1, x = x * x % m)
      if (e & 1)
        r = r * x % m;
    return r;
  };
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      x0 = (x0 + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        x1 = (x1 + modpow(2, n - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  map<int, int> d;
  function<int(int)> f = [&](int x) {
    if (!x)
      return 1;
    if (d.count(x))
      return d[x];
    return d[x] = f(x % __builtin_popcount(x)) + 1;
  };
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      cout << f((x0 + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1)) << '\\n';
    } else if (cnt == 1) {
      cout << 0 << '\\n';
    } else {
      cout << f((x1 - modpow(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1))
           << '\\n';
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  string s;
  cin >> n >> s;
  int cnt = count(s.begin(), s.end(), '1');
  int x0 = 0, x1 = 0;
  auto modpow = [](auto a, auto e, int m) {
    long long r = 1, x = a % m;
    for (; e > 0; e >>= 1, x = x * x % m)
      if (e & 1)
        r = r * x % m;
    return r;
  };
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      x0 = (x0 + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
      x1 = (x1 + modpow(2, n - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  map<int, int> d;
  function<int(int)> f = [&](int x) {
    if (!x)
      return 1;
    if (d.count(x))
      return d[x];
    return d[x] = f(x % __builtin_popcount(x)) + 1;
  };
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      cout << f((x0 + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1)) << '\\n';
    } else {
      cout << f((x1 - modpow(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1))
           << '\\n';
    }
  }
}