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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll modpow(ll a, ll N, ll mod) {
  ll ans = 1;
  ll tmp = a;
  while (N > 0) {
    if (N % 2 == 1)
      (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    N /= 2;
  }
  return ans;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  string x;
  cin >> x;
  int mx = 300005;
  vector<int> memo(mx);
  rep(i, mx) {
    int cur = i;
    int cnt = 0;
    while (cur) {
      int p = __builtin_popcount(cur);
      cur %= p;
      cnt++;
    }
    memo[i] = cnt;
  }
  int one = 0;
  rep(i, n) if (x[i] == '1') one++;
  map<int, int> mp;
  for (int j = max(one - 1, 1); j <= one + 1; j++) {
    rep(i, n) {
      if (x[i] == '1')
        (mp[j] += modpow(2, n - 1 - i, j)) %= j;
    }
  }
  rep(i, n) {
    if (x[i] == '0') {
      int cur = (mp[one + 1] + modpow(2, n - 1 - i, one + 1)) % (one + 1);
      cout << memo[cur] + 1 << "\\n";
    } else {
      if (one - 1 == 0) {
        cout << 0 << "\\n";
      } else {
        int cur = (mp[one - 1] + (one - 1) - modpow(2, n - 1 - i, one - 1)) %
                  (one - 1);
        cout << memo[cur] + 1 << "\\n";
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll modpow(ll a, ll N, ll mod) {
  ll ans = 1;
  ll tmp = a;
  while (N > 0) {
    if (N % 2 == 1)
      (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    N /= 2;
  }
  return ans;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  string x;
  cin >> x;
  int mx = 200005;
  vector<int> memo(mx);
  rep(i, mx) {
    int cur = i;
    int cnt = 0;
    while (cur) {
      int p = __builtin_popcount(cur);
      cur %= p;
      cnt++;
    }
    memo[i] = cnt;
  }
  int one = 0;
  rep(i, n) if (x[i] == '1') one++;
  map<int, int> mp;
  for (int j = one - 1; j <= one + 1; j++) {
    rep(i, n) {
      if (x[i] == '1')
        (mp[j] += modpow(2, n - 1 - i, j)) %= j;
    }
  }
  rep(i, n) {
    string y = x;
    if (x[i] == '0') {
      int cur = (mp[one + 1] + modpow(2, n - 1 - i, one + 1)) % (one + 1);
      cout << memo[cur] + 1 << "\\n";
    } else {
      int cur =
          (mp[one - 1] + (one - 1) - modpow(2, n - 1 - i, one - 1)) % (one - 1);
      cout << memo[cur] + 1 << "\\n";
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define maxn 200086

using namespace std;

int n;
char s[maxn];

int a[maxn];

int cal(int x) {
  if (!x)
    return 0;
  if (a[x])
    return a[x];
  int y = x, cnt = 0;
  while (y) {
    if (y & 1)
      cnt++;
    y >>= 1;
  }
  return a[x] = cal(x % cnt) + 1;
}

int cnt;
int b[maxn];

inline int fpow(int x, int y, int p) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = 1ll * ans * x % p;
    y >>= 1, x = 1ll * x * x % p;
  }
  return ans % p;
}

int main() {
  scanf("%d%s", &n, s + 1);
  // n = 2e5;
  // for(int i = 1;i <= n;i++) s[i] = (i & 1) + '0';
  for (int i = 1; i <= n; i++)
    cnt += s[i] - '0';
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (cnt - 1 > 0)
        b[0] = (b[0] + fpow(2, n - i, cnt - 1)) % (cnt - 1);
      b[1] = (b[1] + fpow(2, n - i, cnt + 1)) % (cnt + 1);
    }
  }
  // printf("%d %d %d--\\n", b[0], b[1], cnt);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (cnt - 1 > 0)
        printf("%d\\n",
               cal(((b[0] - fpow(2, n - i, cnt - 1)) % (cnt - 1) + cnt - 1) %
                   (cnt - 1)) +
                   1);
      else
        puts("0");
    } else {
      // printf("%d--\\n", fpow(2, n - i, cnt + 1));
      printf("%d\\n", cal((b[1] + fpow(2, n - i, cnt + 1)) % (cnt + 1)) + 1);
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define maxn 200086

using namespace std;

int n;
char s[maxn];

int a[maxn];

int cal(int x) {
  if (!x)
    return 0;
  if (a[x])
    return a[x];
  int y = x, cnt = 0;
  while (y) {
    if (y & 1)
      cnt++;
    y >>= 1;
  }
  return a[x] = cal(x % cnt) + 1;
}

int cnt;
int b[2];

inline int fpow(int x, int y, int p) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = ans * x % p;
    y >>= 1, x = x * x % p;
  }
  return ans % p;
}

int main() {
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++)
    cnt += s[i] - '0';
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (cnt - 1 > 0)
        b[0] = (b[0] + fpow(2, n - i, cnt - 1)) % (cnt - 1);
      b[1] = (b[1] + fpow(2, n - i, cnt + 1)) % (cnt + 1);
    }
  }
  // printf("%d %d %d--\\n", b[0], b[1], cnt);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (cnt - 1 > 0)
        printf("%d\\n",
               cal((b[0] - fpow(2, n - i, cnt - 1) + cnt - 1) % (cnt - 1)) + 1);
      else
        puts("0");
    } else {
      // printf("%d--\\n", fpow(2, n - i, cnt + 1));
      printf("%d\\n", cal((b[1] + fpow(2, n - i, cnt + 1)) % (cnt + 1)) + 1);
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main() {
  ll n;
  string x;
  cin >> n >> x;

  ll bit = 0, y;
  rep(i, n) {
    if (x[i] == '1') {
      bit++;
      y = i;
    }
  }

  if (bit == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  vector<ll> A(200001), B(200001);
  ll a = bit - 1, b = bit + 1;
  A[0] = 1, B[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (a > 0)
      A[i] = A[i - 1] * 2 % a;
    B[i] = B[i - 1] * 2 % b;
  }

  ll ansa = 0, ansb = 0;
  rep(i, n) {
    if (x[n - 1 - i] == '1') {
      if (a > 0)
        ansa = (ansa + A[i]) % a;
      ansb = (ansb + B[i]) % b;
    }
  }

  for (int i = 0; i < n; i++) {
    if (bit == 1 && i == y) {
      cout << 0 << endl;
      continue;
    }

    ll ans, cnt = 1;
    if (x[i] == '0') {
      ans = (ansb + B[n - 1 - i]) % b;
    } else {
      if (a > 0)
        ans = (ansa - A[n - 1 - i] + a) % a;
    }

    while (ans > 0) {
      cnt++;
      int m = __builtin_popcount(ans);
      ans = ans % m;
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main() {
  ll n;
  string x;
  cin >> n >> x;

  ll bit = 0;
  rep(i, n) {
    if (x[i] == '1')
      bit++;
  }

  vector<ll> A(200001), B(200001);
  ll a = bit - 1, b = bit + 1;
  ll c = 1;
  rep(i, n) {
    if (a > 0)
      A[i] = c % a;
    B[i] = c % b;
    c *= 2;
  }

  ll ansa = 0, ansb = 0;
  rep(i, n) {
    if (x[n - 1 - i] == '1') {
      if (a > 0)
        ansa = (ansa + A[i]) % a;
      ansb = (ansb + B[i]) % b;
    }
  }

  for (int i = 0; i < n; i++) {
    ll ans, cnt = 1;
    if (x[i] == '0') {
      ans = (ansb + B[n - 1 - i]) % b;
    } else {
      ans = (ansa - A[n - 1 - i] + a) % a;
    }

    while (ans > 0) {
      cnt++;
      int m = bitset<200001>(ans).count();
      ans = ans % m;
    }
    cout << cnt << endl;
  }

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

#include <bits/stdc++.h>

std::vector<int> map(500000, 0);

int func(int val) {
  if (val == 0) {
    map[val] = 0;
    return map[val];
  }
  if (map[val] != 0) {
    return map[val];
  }
  std::bitset<20> bs(val);
  int count = bs.count();
  map[val] = func(val % count) + 1;
  return map[val];
}

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

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

  int c_m = std::max(count - 1, 1);
  int c_p = count + 1;
  std::vector<int> mod_cm(N, 1 % c_m), mod_cp(N, 1 % c_p);

  map[1] = 1;
  map[2] = 1;

  int sum_m = 0, sum_p = 0;
  if (X[N - 1] == '1') {
    sum_m = mod_cm[N - 1];
    sum_p = mod_cp[N - 1];
  }
  for (int i = N - 2; i >= 0; i--) {
    mod_cm[i] = mod_cm[i + 1] * 2 % c_m;
    mod_cp[i] = mod_cp[i + 1] * 2 % c_p;
    if (X[i] == '1') {
      sum_m = (sum_m + mod_cm[i]) % c_m;
      sum_p = (sum_p + mod_cp[i]) % c_p;
    }
  }

  for (int i = 0; i < N; i++) {
    int val;
    if (X[i] == '1') {
      if (count == 1) {
        std::cout << 0 << std::endl;
        continue;
      }
      val = (sum_m - mod_cm[i] + c_m) % c_m;
    } else {
      val = (sum_p + mod_cp[i]) % c_p;
    }
    std::cout << func(val) + 1 << std::endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

std::vector<int> map(500000, 0);

int func(int val) {
  if (val == 0) {
    map[val] = 0;
    return map[val];
  }
  if (map[val] != 0) {
    return map[val];
  }
  std::bitset<20> bs(val);
  int count = bs.count();
  map[val] = func(val % count) + 1;
  return map[val];
}

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

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

  int c_m = count - 1;
  int c_p = count + 1;
  std::vector<int> mod_cm(N, 1 % c_m), mod_cp(N, 1 % c_p);

  map[1] = 1;
  map[2] = 1;

  int sum_m = 0, sum_p = 0;
  if (X[N - 1] == '1') {
    sum_m = mod_cm[N - 1];
    sum_p = mod_cp[N - 1];
  }
  for (int i = N - 2; i >= 0; i--) {
    mod_cm[i] = mod_cm[i + 1] * 2 % c_m;
    mod_cp[i] = mod_cp[i + 1] * 2 % c_p;
    if (X[i] == '1') {
      sum_m = (sum_m + mod_cm[i]) % c_m;
      sum_p = (sum_p + mod_cp[i]) % c_p;
    }
  }

  for (int i = 0; i < N; i++) {
    int val;
    if (X[i] == '1') {
      val = (sum_m - mod_cm[i] + c_m) % c_m;
    } else {
      val = (sum_p + mod_cp[i]) % c_p;
    }
    std::cout << func(val) + 1 << std::endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;

int f(int i) {
  if (i == 0)
    return 0;
  return f(i % __builtin_popcount(i)) + 1;
}

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() {
  int n;
  cin >> n;
  string x;
  cin >> x;

  int bc = 0;
  for (int i = 0; i < n; i++)
    bc += (x[i] == '1');

  if (bc == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << endl;
    return 0;
  }
  if (bc == 1) {
    int id;
    for (int i = 0; i < n; i++)
      if (x[i] == '1')
        id = i;
    for (int i = 0; i < n; i++) {
      if (i == id)
        cout << 0 << endl;
      else {
        ll mpid = mod_pow(2LL, (ll)(n - 1 - id), 2LL);
        ll mpi = mod_pow(2LL, (ll)(n - 1 - i), 2LL);
        cout << f((mpid + mpi) % 2) + 1 << endl;
      }
    }
    return 0;
  }

  ll sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) {
    sum1 = sum1 * 2 % (bc + 1);
    sum1 = (sum1 + (x[i] - '0')) % (bc + 1);
  }
  for (int i = 0; i < n; i++) {
    sum2 = sum2 * 2 % (bc - 1);
    sum2 = (sum2 + (x[i] - '0')) % (bc - 1);
  }

  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      ll sum = sum2;
      ll mp = mod_pow(2LL, (ll)(n - 1 - i), (ll)(bc - 1));
      sum = (sum - mp + (bc - 1)) % (bc - 1);
      cout << f(sum) + 1 << endl;
    } else {
      ll sum = sum1;
      ll mp = mod_pow(2LL, (ll)(n - 1 - i), (ll)(bc + 1));
      sum = (sum + mp + (bc + 1)) % (bc + 1);
      cout << f(sum) + 1 << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

using ll = long long;

int f(int i) {
  if (i == 0)
    return 0;
  return f(i % __builtin_popcount(i)) + 1;
}

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() {
  int n;
  cin >> n;
  string x;
  cin >> x;

  int bc = 0;
  for (int i = 0; i < n; i++)
    bc += (x[i] == '1');

  ll sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) {
    sum1 = sum1 * 2 % (bc + 1);
    sum1 = (sum1 + (x[i] - '0')) % (bc + 1);
  }
  for (int i = 0; i < n; i++) {
    sum2 = sum2 * 2 % (bc - 1);
    sum2 = (sum2 + (x[i] - '0')) % (bc - 1);
  }

  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {

      if (bc == 1) {
        cout << 0 << endl;
        continue;
      }

      ll sum = sum2;
      ll mp = mod_pow(2LL, (ll)(n - 1 - i), (ll)(bc - 1));
      sum = (sum - mp + (bc - 1)) % (bc - 1);
      cout << f(sum) + 1 << endl;
    } else {
      ll sum = sum1;
      ll mp = mod_pow(2LL, (ll)(n - 1 - i), (ll)(bc + 1));
      sum = (sum + mp + (bc + 1)) % (bc + 1);
      cout << f(sum) + 1 << endl;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

uint32_t popcnt(uint32_t val) {
  uint32_t cnt = 0;
  while (val) {
    cnt += val % 2;
    val /= 2;
  }
  return cnt;
}

int main() {
  uint32_t N = 0;
  std::cin >> N;
  std::string X;
  std::cin >> X;

  uint32_t X_cnt = 0;
  for (size_t i = 0; i < N; i++)
    if (X[i] == '1')
      X_cnt++;

  std::vector<std::vector<uint32_t>> digit_mod(2,
                                               std::vector<uint32_t>(N + 1, 0));
  for (size_t f = 0; f < 2; f++) {
    if (X_cnt <= 1 && f == 1)
      continue;
    uint32_t mod = X_cnt + (f ? -1 : 1), X_mod = 0;

    for (size_t i = 0, val = 1; i < N; val *= 2, i++) {
      digit_mod[f][i] = val % mod;
      val = digit_mod[f][i];
      if (X[N - 1 - i] == '1')
        X_mod = (X_mod + val) % mod;
    }
    digit_mod[f][N] = X_mod;
  }

  for (size_t i = 0; i < N; i++) {
    bool flag = (X[i] == '1');
    uint32_t mod = X_cnt + (flag ? -1 : 1), Xi_mod = digit_mod[flag][N];

    if (mod == 0) {
      std::cout << 0 << std::endl;
      continue;
    }

    if (flag) {
      if (Xi_mod >= digit_mod[flag][N - 1 - i]) {
        Xi_mod = (Xi_mod - digit_mod[flag][N - 1 - i]) % mod;
      } else {
        Xi_mod = (mod + Xi_mod - digit_mod[flag][N - 1 - i]) % mod;
      }
    } else {
      Xi_mod = (Xi_mod + digit_mod[flag][N - 1 - i]) % mod;
    }

    uint32_t func = 1;
    while (Xi_mod > 0) {
      mod = popcnt(Xi_mod);
      Xi_mod %= mod;
      func++;
    }

    std::cout << func << std::endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

uint32_t popcnt(uint32_t val) {
  uint32_t cnt = 0;
  while (val) {
    cnt += val % 2;
    val /= 2;
  }
  return cnt;
}

int main() {
  uint32_t N = 0;
  std::cin >> N;
  std::string X;
  std::cin >> X;

  uint32_t X_cnt = 0;
  for (size_t i = 0; i < N; i++)
    if (X[i] == '1')
      X_cnt++;

  std::vector<std::vector<uint32_t>> digit_mod(2,
                                               std::vector<uint32_t>(N + 1, 0));

  for (size_t f = 0; f < 2; f++) {
    uint32_t mod = X_cnt + (f ? -1 : 1);
    uint32_t X_mod = 0;
    for (size_t i = 0, val = 1; i < N; val *= 2, i++) {
      digit_mod[f][i] = val % mod;
      if (X[N - 1 - i] == '1')
        X_mod = (X_mod + digit_mod[f][i]) % mod;
    }
    digit_mod[f][N] = X_mod;
  }

  for (size_t i = 0; i < N; i++) {
    bool flag = (X[i] == '1');
    uint32_t mod = X_cnt + (flag ? -1 : 1), Xi_mod = digit_mod[flag][N];
    if (flag) {
      if (Xi_mod >= digit_mod[flag][N - 1 - i]) {
        Xi_mod = (Xi_mod - digit_mod[flag][N - 1 - i]) % mod;
      } else {
        Xi_mod = (mod + Xi_mod - digit_mod[flag][N - 1 - i]) % mod;
      }
    } else {
      Xi_mod = (Xi_mod + digit_mod[flag][N - 1 - i]) % mod;
    }

    uint32_t func = 1;
    while (Xi_mod > 0) {
      mod = popcnt(Xi_mod);
      Xi_mod %= mod;
      func++;
    }

    std::cout << func << std::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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int max_size = 200005;
int n;
bitset<max_size> bs;

int popcount(int n) {
  int count = 0;
  int i = 0;
  for (int j = 1; j <= n; j *= 2) {
    if (n >> i & 1)
      count++;
    ++i;
  }
  return count;
}

int residue(int mod, vector<int> &res) {
  int ri = 1, r = 0;
  rep(i, n) {
    res.at(i) = ri;
    if (bs.test(i)) {
      r += ri;
      r %= mod;
    }
    ri <<= 1;
    ri %= mod;
  }
  return r;
}

int main() {
  cin >> n;
  string s;
  cin >> s;
  bs = bitset<max_size>(s);
  int mod = bs.count();
  vector<int> ans(n);
  vector<int> resn1(n), resp1(n);
  int rn1 = 0;
  if (0 < mod - 1)
    rn1 = residue(mod - 1, resn1);
  int rp1 = residue(mod + 1, resp1);

  rep(i, n) {
    int m, r;
    if (bs.test(i)) {
      m = mod - 1;
      if (m == 0) {
        ans.at(i) = 0;
        continue;
      }
      r = rn1;
      r -= resn1.at(i);
    } else {
      m = mod + 1;
      r = rp1;
      r += resp1.at(i);
    }
    r %= m;
    if (r < 0)
      r += m;
    int count = 1;
    while (r != 0) {
      r %= popcount(r);
      count++;
    }
    ans.at(i) = count;
  }

  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans.at(i) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

const int max_size = 200005;
int n;
bitset<max_size> bs;

int popcount(int n) {
  int count = 0;
  int i = 0;
  for (int j = 1; j <= n; j *= 2) {
    if (n >> i & 1)
      count++;
    ++i;
  }
  return count;
}

int residue(int mod, vector<int> &res) {
  int ri = 1, r = 0;
  rep(i, n) {
    res.at(i) = ri;
    if (bs.test(i)) {
      r += ri;
      r %= mod;
    }
    ri <<= 1;
    ri %= mod;
  }
  return r;
}

int main() {
  cin >> n;
  string s;
  cin >> s;
  bs = bitset<max_size>(s);
  int mod = bs.count();
  vector<int> ans(n);
  vector<int> resn1(n), resp1(n);
  int rn1 = residue(mod - 1, resn1);
  int rp1 = residue(mod + 1, resp1);

  rep(i, n) {
    int m, r;
    if (bs.test(i)) {
      m = mod - 1;
      r = rn1;
      r -= resn1.at(i);
    } else {
      m = mod + 1;
      r = rp1;
      r += resp1.at(i);
    }
    r %= m;
    if (r < 0)
      r += m;
    int count = 1;
    while (r != 0) {
      r %= popcount(r);
      count++;
    }
    ans.at(i) = count;
  }

  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans.at(i) << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define int long long
using namespace std;

#define deb(x) cout << #x << " = " << x << endl
template <typename T> vector<T> readVector(int n) {
  vector<T> res(n);
  for (int i = 0; i < n; i++)
    cin >> res[i];
  return res;
}

int power(int a, int b, int mod) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b = b >> 1;
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;

  vector<int> dp(2e6 + 5);
  dp[0] = 0;
  for (int i = 1; i <= 2e6; i++) {
    dp[i] = (1 + dp[i % __builtin_popcount(i)]);
  }

  int ans1 = 0; /// when s[i] == '1'
  int ans2 = 0; /// when s[i] == '0'

  int c = 0;
  for (int i = 0; i < n; i++)
    c += (s[i] - '0');

  if (c > 0) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '0')
        continue;
      if (c > 1)
        ans1 = (ans1 + power(2, n - i - 1, c - 1)) % (c - 1);
      ans2 = (ans2 + power(2, n - i - 1, c + 1)) % (c + 1);
    }
  }
  // deb(ans1);
  // deb(ans2);

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (c == 1)
        cout << 0 << endl;
      else {
        cout << (1 +
                 dp[((ans1 - power(2, n - i - 1, c - 1)) % (c - 1) + c - 1) %
                    (c - 1)])
             << endl;
      }
    } else {
      // deb(dp[ans2 + power(2, n-i+1, c+1)%(c+1)]);
      cout << (1 + dp[(ans2 + power(2, n - i - 1, c + 1)) % (c + 1)]) << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

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

template <typename T> vector<T> readVector(int n) {
  vector<T> res(n);
  for (int i = 0; i < n; i++)
    cin >> res[i];
  return res;
}

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

  int n;
  cin >> n;
  string s;
  cin >> s;

  vector<int> sum(n + 1);
  sum[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    sum[i] = (s[i] - '0');
    if (i < n - 1)
      sum[i] += sum[i + 1];
  }

  int mx = sum[0];

  for (int i = 0; i < n; i++) {
    int total = mx;
    if (s[i] == '1')
      total--;
    else
      total++;

    int val = (int)log2(total);
    if (total % 2 == 0)
      val--;
    int res = sum[n - val];
    if (i >= (n - val)) {
      if (s[i] == '1')
        res--;
      else
        res++;
    }
    cout << res + 1 << endl;
  }

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

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

int mpow(int a, int b, int m) {
  int res = 1;
  do {
    if (b & 1)
      res = 1LL * res * a % m;
    a = 1LL * a * a % m;
  } while (b >>= 1);
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> f(n + 1);
  for (int i = 1; i <= n; ++i) {
    f[i] = 1 + f[i % __builtin_popcount(i)];
  }
  int on = 0;
  for (char c : s)
    if (c == '1')
      on += 1;

  int neg = 0, plus = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (on > 1)
        neg += mpow(2, n - 1 - i, on - 1);
      plus += mpow(2, n - 1 - i, on + 1);
      if (on > 1)
        neg %= on - 1;
      plus %= on + 1;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      int ans = plus;
      ans += mpow(2, n - 1 - i, on + 1);
      ans %= on + 1;
      cout << 1 + f[ans] << '\\n';
    } else {
      if (on == 1) {
        cout << 0 << '\\n';
        continue;
      }
      int ans = neg;
      ans -= mpow(2, n - 1 - i, on - 1);
      ans %= on - 1;
      if (ans < 0)
        ans += on - 1;
      cout << 1 + f[ans] << '\\n';
    }
  }
}

Error: 0

Fixed submission:

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

int mpow(int a, int b, int m) {
  int res = 1;
  do {
    if (b & 1)
      res = 1LL * res * a % m;
    a = 1LL * a * a % m;
  } while (b >>= 1);
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> f(n + 1);
  for (int i = 1; i <= n; ++i) {
    f[i] = 1 + f[i % __builtin_popcount(i)];
  }
  int on = 0;
  for (char c : s)
    if (c == '1')
      on += 1;

  int neg = 0, plus = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      neg += mpow(2, n - 1 - i, on - 1);
      plus += mpow(2, n - 1 - i, on + 1);
      neg %= on - 1;
      plus %= on + 1;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      int ans = plus;
      ans += mpow(2, n - 1 - i, on + 1);
      ans %= on + 1;
      cout << 1 + f[ans] << '\\n';
    } else {
      int ans = neg;
      ans -= mpow(2, n - 1 - i, on - 1);
      ans %= on - 1;
      if (ans < 0)
        ans += on - 1;
      cout << 1 + f[ans] << '\\n';
    }
  }
}