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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> ans;
  string x;
  cin >> x;
  reverse(x.begin(), x.end());
  ll cnt = 0;
  rep(i, n) cnt += (x[i] == '1');
  if (cnt == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }
  vector<ll> mod2_up, mod2_down;
  mod2_up.push_back(1 % (cnt + 1));
  if (cnt != 1)
    mod2_down.push_back(1 % (cnt - 1));
  rep(i, n - 1) {
    mod2_up.push_back((mod2_up[i] * 2) % (cnt + 1));
    if (cnt != 1)
      mod2_down.push_back((mod2_down[i] * 2) % (cnt - 1));
  }
  ll modup = 0, moddown = 0;
  rep(i, n) {
    if (x[i] == '1') {
      modup += mod2_up[i];
      if (cnt != 1)
        moddown += mod2_down[i];
      modup %= (cnt + 1);
      if (cnt != 1)
        moddown %= (cnt - 1);
    }
  }
  rep(i, n) {
    int ansi = 1, newx;
    if (x[i] == '1') {
      if (cnt == 1) {
        ans.push_back(0);
        continue;
      }
      newx = moddown;
      newx = (newx - mod2_down[i] + (cnt - 1)) % (cnt - 1);
    } else {
      newx = modup;
      newx = (newx + mod2_up[i]) % (cnt + 1);
    }
    while (newx > 0) {
      int popcnt = 0;
      for (int i = newx; i > 0; i >>= 1)
        popcnt += (i & 1);
      newx %= popcnt;
      ansi++;
    }
    ans.push_back(ansi);
  }
  rep(i, n) { cout << ans[n - i - 1] << endl; }
}

Error: 0

Fixed submission:

#include <bit>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> ans;
  string x;
  cin >> x;
  reverse(x.begin(), x.end());
  ll cnt = 0;
  rep(i, n) cnt += (x[i] == '1');
  vector<ll> mod2_up, mod2_down;
  mod2_up.push_back(1 % (cnt + 1));
  mod2_down.push_back(1 % (cnt - 1));
  rep(i, n - 1) {
    mod2_up.push_back((mod2_up[i] * 2) % (cnt + 1));
    mod2_down.push_back((mod2_down[i] * 2) % (cnt - 1));
  }
  ll modup = 0, moddown = 0;
  rep(i, n) {
    if (x[i] == '1') {
      modup += mod2_up[i];
      moddown += mod2_down[i];
      modup %= (cnt + 1);
      moddown %= (cnt - 1);
    }
  }
  rep(i, n) {
    int ansi = 1, newx;
    if (x[i] == '1') {
      newx = moddown;
      newx = (newx - mod2_down[i] + (cnt - 1)) % (cnt - 1);
    } else {
      newx = modup;
      newx = (newx + mod2_up[i]) % (cnt + 1);
    }
    while (newx > 0) {
      int popcnt = 0;
      for (int i = newx; i > 0; i >>= 1)
        popcnt += (i & 1);
      newx %= popcnt;
      ansi++;
    }
    ans.push_back(ansi);
  }
  rep(i, n) { cout << ans[n - i - 1] << endl; }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 200000;

vector<int> f(MAXN, 0);

inline void precalc() {
  for (int i = 1; i < MAXN; i++) {
    f[i] = 1 + f[i % __builtin_popcount(i)];
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  precalc();
  int n;
  string s;
  cin >> n >> s;
  int cnt = count(s.begin(), s.end(), '1');
  array<int, 2> mods = {cnt + 1, cnt - 1};
  for (auto &el : mods) {
    el = (el == 0 ? 1 : el);
  }
  vector<array<int, 2>> p2(n, {1, 1});
  for (int i = 1; i < n; i++) {
    p2[i][0] = (p2[i - 1][0] * 2) % mods[0];
    p2[i][1] = (p2[i - 1][1] * 2) % mods[1];
  }
  array<int, 2> res = {0, 0};
  for (int i = 0; i < n; i++) {
    res[0] += (p2[n - 1 - i][0] * (s[i] - '0')) % mods[0];
    res[0] %= mods[0];
    res[1] += (p2[n - 1 - i][1] * (s[i] - '0')) % mods[1];
    res[1] %= mods[1];
  }
  for (int i = 0; i < n; i++) {
    if (cnt == 1 && s[i] == '1') {
      cout << "0\\n";
      continue;
    }
    cout << f[(res[s[i] - '0'] -
               (p2[n - 1 - i][s[i] - '0'] * (s[i] - '0')) % mods[s[i] - '0'] +
               ((p2[n - 1 - i][s[i] - '0'] * ((s[i] - '0') ^ 1))) %
                   mods[s[i] - '0'] +
               mods[s[i] - '0']) %
              mods[s[i] - '0']] +
                1
         << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 200000;

vector<int> f(MAXN, 0);

inline void precalc() {
  for (int i = 1; i < MAXN; i++) {
    f[i] = 1 + f[i % __builtin_popcount(i)];
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  precalc();
  int n;
  string s;
  cin >> n >> s;
  int cnt = count(s.begin(), s.end(), '1');
  array<int, 2> mods = {cnt + 1, cnt - 1};
  for (auto &el : mods) {
    el = (el == 0 ? 1 : el);
  }
  vector<array<int, 2>> p2(n + 1, {1, 1});
  for (int i = 1; i <= n; i++) {
    p2[i][0] = (p2[i - 1][0] * 2) % mods[0];
    p2[i][1] = (p2[i - 1][1] * 2) % mods[1];
  }
  array<int, 2> res = {1, 1};
  for (int i = 0; i < n; i++) {
    res[0] += (p2[n - 1 - i][0] * (s[i] - '1')) % mods[0];
    res[0] %= mods[0];
    res[1] += (p2[n - 1 - i][1] * (s[i] - '1')) % mods[1];
    res[1] %= mods[1];
  }
  for (int i = 0; i < n; i++) {
    cout << f[(res[s[i] - '0'] -
               (p2[n - 1 - i][s[i] - '0'] * ((s[i] - '0') ^ 1)) %
                   mods[s[i] - '0'] +
               mods[s[i] - '0']) %
              mods[s[i] - '0']] +
                1
         << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

const int MAXN = 2e5 + 19;

inline int f(int x) {
  int res = 0;
  while (x)
    ++res, x %= __builtin_popcount(unsigned(x));
  return res;
}

int n, pop;
int low[MAXN], val_low;
int hig[MAXN], val_hig;
char s[MAXN];

int main() {
  std::scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; ++i)
    if (s[i] == '1')
      ++pop;
  low[1] = 1, hig[1] = 1;
  for (int i = 2; i <= n; ++i) {
    if (pop - 1)
      low[i] = low[i - 1] * 2 % (pop - 1);
    hig[i] = hig[i - 1] * 2 % (pop + 1);
  }
  for (int i = 1; i <= n; ++i)
    if (s[n - i + 1] == '1') {
      if (pop - 1)
        val_low = (val_low + low[i]) % (pop - 1);
      val_hig = (val_hig + hig[i]) % (pop + 1);
    }
  for (int i = 1, val; i <= n; ++i)
    if (s[i] == '1') {
      //	if(check(i))
      //		continue;
      if (pop == 1) {
        std::puts("0");
        continue;
      }
      val = (val_low - low[n - i + 1]) % (pop - 1);
      val += pop - 1;
      val %= pop - 1;
      std::printf("%d\\n", f(val) + 1);
    } else {
      //	if(check(i))
      //		continue;
      val = (val_hig + hig[n - i + 1]) % (pop + 1);
      val += pop + 1;
      val %= pop + 1;
      std::printf("%d\\n", f(val) + 1);
    }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

const int MAXN = 2e5 + 19;

inline int f(int x) {
  int res = 0;
  while (x)
    ++res, x %= __builtin_popcount(unsigned(x));
  return res;
}

int n, pop;
int low[MAXN], val_low;
int hig[MAXN], val_hig;
char s[MAXN];

int main() {
  std::scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; ++i)
    if (s[i] == '1')
      ++pop;
  low[1] = 1, hig[1] = 1;
  for (int i = 2; i <= n; ++i)
    low[i] = low[i - 1] * 2 % (pop - 1), hig[i] = hig[i - 1] * 2 % (pop + 1);
  for (int i = 1; i <= n; ++i)
    if (s[n - i + 1] == '1')
      val_low = (val_low + low[i]) % (pop - 1),
      val_hig = (val_hig + hig[i]) % (pop + 1);
  for (int i = 1, val; i <= n; ++i)
    if (s[i] == '1') {
      //	if(check(i))
      //		continue;
      val = (val_low - low[n - i + 1]) % (pop - 1);
      val += pop - 1;
      val %= pop - 1;
      std::printf("%d\\n", f(val) + 1);
    } else {
      //	if(check(i))
      //		continue;
      val = (val_hig + hig[n - i + 1]) % (pop + 1);
      val += pop + 1;
      val %= pop + 1;
      std::printf("%d\\n", f(val) + 1);
    }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <iostream>
using namespace std;
using ll = long long;
int MOD = 1e9 + 7;
int mod(int a, int b) { return (a % b + b) % b; }
int pangkat(int num, int derajat) {
  if (derajat == 1)
    return num;
  if (derajat == 0)
    return 1;
  int x = pangkat(num, derajat / 2);
  x = (ll)x * x % MOD;
  if (derajat & 1)
    x = (ll)x * num % MOD;
  return x;
}
int main() {
  cin.tie(0);
  int pl = 0, mn = 0;
  int N;
  cin >> N;
  string s;
  cin >> s;
  bitset<200000> b(s);

  int cnt = b.count();
  MOD = cnt + 1;
  for (int i = 0; i < N; i++) {
    if (b[i])
      pl = (pl + pangkat(2, i)) % MOD;
  }
  if (cnt != 1) {
    MOD = cnt - 1;
    for (int i = 0; i < N; i++) {
      if (b[i])
        mn = (mn + pangkat(2, i)) % MOD;
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (!b[i]) { // giliran plus
      MOD = cnt + 1;
      int now = (pl + pangkat(2, i)) % MOD, ans = 1;
      while (now > 0)
        ++ans, now %= __builtin_popcount(now);
      cout << ans;
    } else {
      MOD = cnt - 1;
      if (MOD == 0)
        cout << "0\\n";
      else {
        int now = mod(mn - pangkat(2, i), MOD), ans = 1;
        while (now > 0)
          ++ans, now %= __builtin_popcount(now);
        cout << ans;
      }
    }
    cout << '\\n';
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
using namespace std;
using ll = long long;
int MOD = 1e9 + 7;
int mod(int a, int b) { return (a % b + b) % b; }
int pangkat(int num, int derajat) {
  if (derajat == 1)
    return num;
  if (derajat == 0)
    return 1;
  int x = pangkat(num, derajat / 2);
  x = (ll)x * x % MOD;
  if (derajat & 1)
    x = (ll)x * num % MOD;
  return x;
}
int main() {
  cin.tie(0);
  int pl = 0, mn = 0;
  int N;
  cin >> N;
  string s;
  cin >> s;
  bitset<200000> b(s);

  int cnt = b.count();
  MOD = cnt + 1;
  for (int i = 0; i < N; i++) {
    if (b[i])
      pl = (pl + pangkat(2, i)) % MOD;
  }
  MOD = cnt - 1;
  for (int i = 0; i < N; i++) {
    if (b[i])
      mn = (mn + pangkat(2, i)) % MOD;
  }

  for (int i = N - 1; i >= 0; i--) {
    if (!b[i]) { // giliran plus
      MOD = cnt + 1;
      int now = (pl + pangkat(2, i)) % MOD, ans = 1;
      while (now > 0)
        ++ans, now %= __builtin_popcount(now);
      cout << ans;
    } else {
      MOD = cnt - 1;
      if (MOD == 0)
        cout << "0\\n";
      else {
        int now = mod(mn - pangkat(2, i), MOD), ans = 1;
        while (now > 0)
          ++ans, now %= __builtin_popcount(now);
        cout << ans;
      }
    }
    cout << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define pb push_back
#define mp make_pair
#define ms memset
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define lower_bound lb
#define upper_bound ub

int main() {
  int n;
  cin >> n;
  string S;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (S[i] == '1')
      cnt++;
  }
  int Ap[n + 1], Bp[n + 1];
  Ap[0] = 1;
  Bp[0] = 1;
  for (int i = 1; i < n; i++) {
    Ap[i] = (2 * Ap[i - 1]) % (cnt + 1);
    if (cnt == 1)
      continue;
    Bp[i] = (2 * Bp[i - 1]) % (cnt - 1);
  }
  int A = 0;
  int B = 0;
  for (int i = 0; i < n; i++) {
    if (S[i] == '1') {
      A += Ap[n - i - 1];
      A %= (cnt + 1);
      if (cnt == 1)
        continue;
      B += Bp[n - i - 1];
      B %= (cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    int c = 1;
    if (S[i] == '0') {
      int a = (A + Ap[n - i - 1]) % (cnt + 1);
      while (a > 0) {
        int j = a;
        int count = 0;
        while (j > 0) {
          if (j % 2 == 1)
            count++;
          j /= 2;
        }
        a %= count;
        c++;
      }
      cout << c << '\\n';
    } else {
      if (cnt == 1) {
        cout << 0 << '\\n';
        continue;
      }
      int b = (B - Bp[n - i - 1] + cnt - 1) % (cnt - 1);
      while (b > 0) {
        int j = b;
        int count = 0;
        while (j > 0) {
          if (j % 2 == 1)
            count++;
          j /= 2;
        }
        b %= count;
        c++;
      }
      cout << c << '\\n';
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define pb push_back
#define mp make_pair
#define ms memset
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define lower_bound lb
#define upper_bound ub

int main() {
  int n;
  cin >> n;
  string S;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (S[i] == '1')
      cnt++;
  }
  int Ap[n + 1], Bp[n + 1];
  Ap[0] = 1;
  Bp[0] = 1;
  for (int i = 1; i < n; i++) {
    Ap[i] = (2 * Ap[i - 1]) % (cnt + 1);
    Bp[i] = (2 * Bp[i - 1]) % (cnt - 1);
  }
  int A = 0;
  int B = 0;
  for (int i = 0; i < n; i++) {
    if (S[i] == '1') {
      A += Ap[n - i - 1];
      A %= (cnt + 1);
      B += Bp[n - i - 1];
      B %= (cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    int c = 1;
    if (S[i] == '0') {
      int a = (A + Ap[n - i - 1]) % (cnt + 1);
      while (a > 0) {
        int j = a;
        int count = 0;
        while (j > 0) {
          if (j % 2 == 1)
            count++;
          j /= 2;
        }
        a %= count;
        c++;
      }
      cout << c << '\\n';
    } else {
      int b = (B - Bp[n - i - 1] + cnt - 1) % (cnt - 1);
      while (b > 0) {
        int j = b;
        int count = 0;
        while (j > 0) {
          if (j % 2 == 1)
            count++;
          j /= 2;
        }
        b %= count;
        c++;
      }
      cout << c << '\\n';
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
int Mi(int X, int Mod) {
  if (X == 0)
    return 1;
  int res = Mi(X / 2, Mod);
  res *= res;
  if (X % 2)
    res *= 2;
  return res % Mod;
}
int Ex(int X) {
  if (X == 0)
    return 0;
  return Ex(X % __builtin_popcount(X)) + 1;
}
signed main() {
  int mod1 = 0, mod2 = 0;
  int N;
  cin >> N;
  string S;
  cin >> S;
  for (int i = 0; i < N; i++)
    if (S[i] == '1')
      mod1++, mod2++;
  mod1++;
  mod2--;
  int X1 = 0, X2 = 0;
  for (int i = 0; i < N; i++) {
    X1 = (X1 + X1) % mod1;
    if (S[i] == '1')
      X1++;
    X1 %= mod1;
    if (!mod2)
      continue;
    X2 = (X2 + X2) % mod2;
    if (S[i] == '1')
      X2++;
    X2 %= mod2;
  }
  for (int i = 0; i < N; i++)
    if (S[i] == '1') {
      if (!mod2) {
        cout << 0 << endl;
        continue;
      }
      int Tmp = X2;
      Tmp -= Mi(N - i - 1, mod2);
      Tmp %= mod2;
      Tmp += mod2;
      Tmp %= mod2;
      cout << Ex(Tmp) + 1 << endl;
    } else {
      int Tmp = X1;
      Tmp += Mi(N - i - 1, mod1);
      Tmp %= mod1;
      Tmp += mod1;
      Tmp %= mod1;
      cout << Ex(Tmp) + 1 << endl;
    }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int Mi(int X, int Mod) {
  if (X == 0)
    return 1;
  int res = Mi(X / 2, Mod);
  res *= res;
  if (X % 2)
    res *= 2;
  return res % Mod;
}
int Ex(int X) {
  if (X == 0)
    return 0;
  return Ex(X % __builtin_popcount(X)) + 1;
}
signed main() {
  int mod1 = 0, mod2 = 0;
  int N;
  cin >> N;
  string S;
  cin >> S;
  for (int i = 0; i < N; i++)
    if (S[i] == '1')
      mod1++, mod2++;
  mod1++;
  mod2--;
  int X1 = 0, X2 = 0;
  for (int i = 0; i < N; i++) {
    X1 = (X1 + X1) % mod1;
    if (S[i] == '1')
      X1++;
    X1 %= mod1;
    X2 = (X2 + X2) % mod2;
    if (S[i] == '1')
      X2++;
    X2 %= mod2;
  }
  for (int i = 0; i < N; i++)
    if (S[i] == '1') {
      int Tmp = X2;
      Tmp -= Mi(N - i - 1, mod2);
      Tmp %= mod2;
      Tmp += mod2;
      Tmp %= mod2;
      cout << Ex(Tmp) + 1 << endl;
    } else {
      int Tmp = X1;
      Tmp += Mi(N - i - 1, mod1);
      Tmp %= mod1;
      Tmp += mod1;
      Tmp %= mod1;
      cout << Ex(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 chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

vector<int> memo(1 << 21, -1);
int f(int x) {
  if (memo[x] >= 0)
    return memo[x];
  int bc = __builtin_popcount(x);
  return memo[x] = f(x % bc) + 1;
}

int main() {
  memo[0] = 0;
  int n;
  string x;
  cin >> n >> x;
  int pc = 0;
  rep(i, n) pc += x[i] == '1';
  vi pn(n), pp(n);
  int now1 = 1;
  int now2 = 1;
  rep(i, n) {
    pn[n - 1 - i] = (pc == 1 ? 0 : now1);
    pp[n - 1 - i] = now2;
    if (pc > 1)
      now1 = (now1 * 2) % (pc - 1);
    now2 = (now2 * 2) % (pc + 1);
  }
  int tot1 = 0, tot2 = 0;
  rep(i, n) if (x[i] == '1') {
    if (pc > 1)
      tot1 = (tot1 + pn[i]) % (pc - 1);
    tot2 = (tot2 + pp[i]) % (pc + 1);
  }
  rep(i, n) {
    if (x[i] == '0') {
      int t = (tot2 + pp[i]) % (pc + 1);
      cout << f(t) + 1 << endl;
    } else {
      if (pc == 1)
        cout << 0 << endl;
      else {
        int t = (tot1 - pn[i] + pc - 1) % (pc - 1);
        cout << f(t) + 1 << endl;
      }
    }
  }
  return 0;
  int xo = 0;

  for (int i = 0; i < n; i++) {
    xo |= (x[n - 1 - i] == '1') << i;
  }
  // cout << xo << ' ' << (1 << 21 << endl;
  for (int i = 0; i < n; i++) {
    int xi = xo ^ (1 << (n - i - 1));
    cout << f(xi) << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

vector<int> memo(1 << 24, -1);
int f(int x) {
  if (memo[x] >= 0)
    return memo[x];
  int bc = __builtin_popcount(x);
  return memo[x] = f(x % bc) + 1;
}

int main() {
  memo[0] = 0;
  int n;
  string x;
  cin >> n >> x;
  int xo = 0;
  for (int i = 0; i < n; i++) {
    xo |= (x[n - 1 - i] == '1') << i;
  }
  for (int i = 0; i < n; i++) {
    int xi = xo ^ (1 << (n - i - 1));
    cout << f(xi) << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int N;

int pcnt(int x) { return __builtin_popcount(x); }

int f(int a) {
  if (a == 0)
    return 0;
  return f(a % pcnt(a)) + 1;
}

int main() {
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N);
  rep(i, N) X[i] = s[i] - '0';
  int opc = 0;
  rep(i, N) opc += X[i];

  vector<int> ans(N);
  // b=0は0→1の反転をする場合
  rep(b, 2) {
    int pc = opc;
    if (b == 0)
      pc++;
    else
      pc--;
    if (pc <= 0)
      continue;

    // x(mod pc)を求める
    int y = 0;
    for (int i = 0; i < N; i++) {
      y = (2 * y) % pc;
      y += X[i];
    }

    // ビット反転させていく
    int bit = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (b == X[i]) {
        int z = y;
        if (b == 0) {
          z = (z + bit) % pc;
        } else {
          z = (z - bit + pc) % pc;
        }
        ans[i] = f(z) + 1;
      }
      bit *= 2;
      bit %= pc;
    }
  }

  rep(i, N) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int N;

int pcnt(int x) { return __builtin_popcount(x); }

int f(int a) {
  if (a == 0)
    return 0;
  return f(a % pcnt(a)) + 1;
}

int main() {
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N);
  rep(i, N) X[i] = s[i] - '0';
  int opc = 0;
  rep(i, N) opc += X[i];

  vector<int> ans(N);
  // b=0は0→1の反転をする場合
  rep(b, 2) {
    int pc = opc;
    if (b == 0)
      pc++;
    else
      pc--;

    // x(mod pc)を求める
    int y = 0;
    for (int i = 0; i < N; i++) {
      y = (2 * y) % pc;
      y += X[i];
    }

    // ビット反転させていく
    int bit = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (b == X[i]) {
        int z = y;
        if (b == 0) {
          z = (z + bit) % pc;
        } else {
          z = (z - bit + pc) % pc;
        }
        ans[i] = f(z) + 1;
      }
      bit *= 2;
      bit %= pc;
    }
  }

  rep(i, N) cout << ans[i] << endl;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MAX 200007

using namespace std;

int pw[MAX][3];
int sum[3];
int n;
string x;

void calc(int cnt, int idx) {
  if (cnt <= 0)
    return;
  pw[0][idx] = 1;
  for (int i = 1; i < MAX; i++) {
    pw[i][idx] = 2 * pw[i - 1][idx];
    pw[i][idx] %= cnt;
  }
  for (int i = 0; i < x.length(); i++) {
    if (x[i] == '1') {
      sum[idx] += pw[n - i - 1][idx];
      sum[idx] %= cnt;
    }
  }
}

int f(int num) {
  // std::popc
  if (num == 0)
    return 0;
  int p = __builtin_popcount(num);
  return 1 + f(num % p);
}

int main() {

  int cnt = 0;

  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cnt++;
  }
  calc(cnt - 1, 0);
  calc(cnt, 1);
  calc(cnt + 1, 2);
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int rem = sum[2];
      rem += pw[(n - i - 1)][2];
      rem %= (cnt + 1);
      // cout << rem << endl;
      cout << (1 + f(rem)) << endl;
    } else {
      if (cnt == 1)
        cout << 0 << endl;
      else {
        int rem = sum[0];
        rem -= pw[(n - i - 1)][0];
        rem %= (cnt - 1);
        if (rem < 0)
          rem += (cnt - 1);
        cout << (1 + f(rem)) << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MAX 100007

using namespace std;

int pw[MAX][3];
int sum[3];
int n;
string x;

void calc(int cnt, int idx) {
  if (cnt <= 0)
    return;
  pw[0][idx] = 1;
  for (int i = 1; i < MAX; i++) {
    pw[i][idx] = 2 * pw[i - 1][idx];
    pw[i][idx] %= cnt;
  }
  for (int i = 0; i < x.length(); i++) {
    if (x[i] == '1') {
      sum[idx] += pw[n - i - 1][idx];
      sum[idx] %= cnt;
    }
  }
}

int f(int num) {
  // std::popc
  if (num == 0)
    return 0;
  int p = __builtin_popcount(num);
  return 1 + f(num % p);
}

int main() {

  int cnt = 0;

  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cnt++;
  }
  calc(cnt - 1, 0);
  calc(cnt, 1);
  calc(cnt + 1, 2);
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int rem = sum[2];
      rem += pw[(n - i - 1)][2];
      rem %= (cnt + 1);
      // cout << rem << endl;
      cout << (1 + f(rem)) << endl;
    } else {
      if (cnt == 1)
        cout << 0 << endl;
      else {
        int rem = sum[0];
        rem -= pw[(n - i - 1)][0];
        rem %= (cnt - 1);
        if (rem < 0)
          rem += (cnt - 1);
        cout << (1 + f(rem)) << endl;
      }
    }
  }
  return 0;
}