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++)
typedef long long ll;
using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;

  int p = count(s.begin(), s.end(), '1');
  int rp = 0, rn = 0;
  for (char c : s) {
    (rp *= 2) %= (p + 1);
    if (p > 1)
      (rn *= 2) %= (p - 1);
    if (c == '1') {
      (rp += 1) %= (p + 1);
      if (p > 1)
        (rn += 1) %= (p - 1);
    }
  }

  reverse(s.begin(), s.end());
  vector<int> ans;
  int twop = 1, twon = 1;

  rep(i, n) {
    int r;
    if (s[i] == '0')
      r = (rp + twop) % (p + 1);
    else {
      if (p == 1) {
        ans.push_back(0);
        continue;
      }
      r = (rn - twon + p - 1) % (p - 1);
    }

    int cnt = 1;
    while (r > 0) {
      r %= __builtin_popcount(r);
      cnt++;
    }
    ans.push_back(cnt);

    (twop *= 2) %= (p + 1);
    if (p > 1)
      (twon *= 2) %= (p - 1);
  }

  reverse(ans.begin(), ans.end());
  for (auto x : ans)
    cout << x << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  bitset<200005> x;
  cin >> x;

  for (int i = n - 1; i >= 0; i--) {
    bitset<200005> y = x;
    y.flip(i);

    int cnt = 0;
    while (y != 0) {
      if (y.count() == 0)
        break;
      y = y.to_ullong() % y.count();
      cnt++;
    }
    cout << cnt << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 13;
char s[maxn];
int f[maxn];
int f2[maxn];
int solve(int x) {
  int cnt = 1;
  while (x != 0) {
    cnt++;
    int a = x;
    int ans = 0;
    while (a) {
      if (a % 2 == 1)
        ans++;
      a /= 2;
    }
    x = x % ans;
  }
  return cnt;
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int cnt = 0;
  for (int i = n; i >= 1; i--) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  int mod1 = cnt - 1;
  int mod2 = cnt + 1;
  int ans1 = 0, ans2 = 0;
  if (cnt != 1 && cnt != 0)
    f[1] = 1 % mod1;
  f2[1] = 1 % mod2;
  for (int i = 2; i <= n; i++) {
    if (cnt != 1 && cnt != 0)
      f[i] = (f[i - 1] * 2) % mod1;
  }
  for (int i = 2; i <= n; i++) {
    f2[i] = (f2[i - 1] * 2) % mod2;
  }
  for (int i = n; i >= 1; i--) {
    if (s[i] == '1') {
      if (cnt != 1 && cnt != 0)
        ans1 = (ans1 + f[n - i + 1]) % mod1;
      ans2 = (ans2 + f2[n - i + 1]) % mod2;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (cnt == 1) {
        printf("0\\n");
        continue;
      }
      int now = ans1;
      now = (now - f[n - i + 1] + mod1) % mod1;
      printf("%d\\n", solve(now));
    } else {
      int now = ans2;
      now = (now + f2[n - i + 1]) % mod2;
      printf("%d\\n", solve(now));
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 13;
char s[maxn];
int solve(int x) {
  int cnt = 1;
  while (x != 0) {
    cnt++;
    int a = x;
    int ans = 0;
    while (a) {
      if (a % 2 == 1)
        ans++;
      a /= 2;
    }
    x = x % ans;
  }
  return cnt;
}
int qpow(int p, int mod) {
  int a = 2;
  int ans = 1;
  while (p != 0) {
    if (p & 1) {
      ans = ans * a % mod;
    }
    a = a * a % mod;
    p = p >> 1;
  }
  return ans;
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  int cnt = 0;
  for (int i = len; i >= 1; i--) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  int mod1 = cnt - 1;
  int mod2 = cnt + 1;
  int ans1 = 0, ans2 = 0;
  for (int i = len; i >= 1; i--) {
    if (s[i] == '1') {
      ans1 = (ans1 + qpow(len - i, mod1)) % mod1;
      if (cnt != 1)
        ans2 = (ans2 + qpow(len - i, mod2)) % mod2;
    }
  }
  for (int i = 1; i <= len; i++) {
    if (s[i] == '1') {
      if (cnt == 1) {
        printf("0\\n");
        continue;
      }
      int now = ans1;
      now = (now - qpow(len - i, mod1) + mod1) % mod1;
      printf("%d\\n", solve(now));
    } else {
      int now = ans2;
      now = (now + qpow(len - i, mod2)) % mod2;
      printf("%d\\n", solve(now));
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_(i, n, m) for (int i = n; i < (int)(m); i++)
#define all(v) v.begin(), v.end()
#define int long long
#define stoi stoll
// #define _GLIBCXX_DEBUG

int f(int X, int C, int a) {
  if (X == 0)
    return a;
  int x = X % C;
  bitset<20> b = x;
  int c = b.count();
  return f(x, c, a + 1);
}

signed main() {
  int N;
  cin >> N;
  bitset<200000> B;
  cin >> B;

  int C = B.count();

  int x = 0;
  int k = 1;
  rep(i, N) {
    if (B.test(i))
      x += k;
    k *= 2;
    k %= C + 1;
  }
  x %= C + 1;

  int y = 0;
  k = 1;
  if (C - 1 > 0) {
    rep(i, N) {
      if (B.test(i))
        y += k;
      k *= 2;
      k %= C - 1;
    }
    y %= C - 1;
  }

  vector<int> ans(N);

  int kx = 1, ky = 1;
  rep(i, N) {
    if (B.test(i)) {
      if (C - 1 == 0)
        ans[i] = 0;
      else {
        int z = (y - ky + C - 1) % (C - 1);
        if (z == 0)
          ans[i] = 1;
        else
          ans[i] = f(z, C - 1, 0);
      }
    } else {
      int z = (x + kx) % (C + 1);
      if (z == 0)
        ans[i] = 1;
      else
        ans[i] = f(z, C + 1, 0);
    }

    kx *= 2;
    kx %= C + 1;
    if (C - 1 != 0) {
      ky *= 2;
      ky %= C - 1;
    }
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_(i, n, m) for (int i = n; i < (int)(m); i++)
#define all(v) v.begin(), v.end()
#define int long long
#define stoi stoll
// #define _GLIBCXX_DEBUG

int f(int X, int C, int a) {
  if (X == 0)
    return a;
  int x = X % C;
  bitset<20> b = x;
  int c = b.count();
  return f(x, c, a + 1);
}

signed main() {
  int N;
  cin >> N;
  bitset<200000> B;
  cin >> B;

  int C = B.count();

  int x = 0;
  int k = 1;
  rep(i, N) {
    if (B.test(i))
      x += k;
    k *= 2;
    k %= C + 1;
  }
  x %= C + 1;

  int y = 0;
  k = 1;
  if (C - 1 != 0) {
    rep(i, N) {
      if (B.test(i))
        y += k;
      k *= 2;
      k %= C - 1;
    }
    y %= C - 1;
  }

  vector<int> ans(N);

  int kx = 1, ky = 1;
  rep(i, N) {
    if (B.test(i)) {
      if (C - 1 == 0)
        ans[i] = 0;
      else {
        int z = (y - ky + C - 1) % (C - 1);
        if (z == 0)
          ans[i] = 1;
        else
          ans[i] = f(z, C - 1, 0);
      }
    } else {
      int z = (x + kx) % (C + 1);
      if (z == 0)
        ans[i] = 1;
      else
        ans[i] = f(z, C + 1, 0);
    }

    kx *= 2;
    kx %= C + 1;
    ky *= 2;
    ky %= C - 1;
  }

  reverse(all(ans));
  rep(i, N) cout << ans[i] << endl;
}
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;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int dp[200000] = {};

void init() { rep(i, 200000) dp[i] = mod; }

int rec(int N) {
  if (N == 0)
    return dp[0] = 0;
  if (dp[N] != mod)
    return dp[N];
  int stand = __builtin_popcount(N);
  return dp[N] = 1 + rec(N % stand);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  init();
  int N;
  cin >> N;
  string S;
  cin >> S;
  rep(i, 200000) { rec(i); }
  int stand = 0;
  rep(i, N) if (S[i] == '1') stand++;
  int md[] = {stand - 1, stand + 1};
  int pow2[2][200000];
  pow2[0][0] = pow2[1][0] = 1;
  for (int i = 1; i < 200000; i++) {
    if (stand >= 2)
      pow2[0][i] = pow2[0][i - 1] * 2 % md[0];
    pow2[1][i] = pow2[1][i - 1] * 2 % md[1];
  }
  int s1 = 0, s2 = 0;
  reverse(S.begin(), S.end());
  rep(i, N) {
    if (stand >= 2)
      s1 = (s1 + pow2[0][i] * (S[i] - '0')) % md[0];
    s2 = (s2 + pow2[1][i] * (S[i] - '0')) % md[1];
  }

  vector<int> ans;
  rep(i, N) {
    int temp;
    if (S[i] == '0') {
      temp = (s2 + pow2[1][i]) % md[1];
    } else if (stand >= 2) {
      temp = (s1 - pow2[0][i]) % md[0];
      if (temp < 0)
        temp += md[0];
    } else {
      temp = -1;
    }
    ans.push_back(temp);
    // cout << temp << endl;
  }
  reverse(ans.begin(), ans.end());
  rep(i, N) {
    if (ans[i] != -1)
      cout << dp[ans[i]] + 1 << endl;
    else
      cout << 0 << 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;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int dp[200000] = {};

void init() { rep(i, 200000) dp[i] = mod; }

int rec(int N) {
  if (N == 0)
    return dp[0] = 0;
  if (dp[N] != mod)
    return dp[N];
  int stand = __builtin_popcount(N);
  return dp[N] = 1 + rec(N % stand);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  init();
  int N;
  cin >> N;
  string S;
  cin >> S;
  rep(i, 200000) { rec(i); }
  int stand = 0;
  rep(i, N) if (S[i] == '1') stand++;
  int md[] = {stand - 1, stand + 1};
  int pow2[2][200000];
  pow2[0][0] = pow2[1][0] = 1;
  for (int i = 1; i < 200000; i++) {
    pow2[0][i] = pow2[0][i - 1] * 2 % md[0];
    pow2[1][i] = pow2[1][i - 1] * 2 % md[1];
  }
  int s1 = 0, s2 = 0;
  reverse(S.begin(), S.end());
  rep(i, N) {
    s1 = (s1 + pow2[0][i] * (S[i] - '0')) % md[0];
    s2 = (s2 + pow2[1][i] * (S[i] - '0')) % md[1];
  }

  vector<int> ans;
  rep(i, N) {
    int temp;
    if (S[i] == '0') {
      temp = (s2 + pow2[1][i]) % md[1];
    } else {
      temp = (s1 - pow2[0][i]) % md[0];
      if (temp < 0)
        temp += md[0];
    }
    ans.push_back(temp);
    // cout << temp << endl;
  }
  reverse(ans.begin(), ans.end());
  rep(i, N) cout << dp[ans[i]] + 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++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int dp[200001] = {};

int dfs(int n) {
  if (n == 0)
    return dp[0] = 0;
  else if (dp[n] != 0)
    return dp[n];
  int p = __builtin_popcount(n);
  return dp[n] = dfs(n % p) + 1;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string X;
  cin >> X;
  for (int i = 1; i <= 200000; i++) {
    dfs(i);
  }
  int pow2[2][N + 1];
  int k = 0;
  rep(i, N) {
    if (X[i] == '1')
      k++;
  }
  pow2[0][0] = 1;
  pow2[1][0] = 1;
  if (k > 1)
    for (int i = 1; i <= N; i++)
      pow2[0][i] = pow2[0][i - 1] * 2 % (k - 1);
  for (int i = 1; i <= N; i++)
    pow2[1][i] = pow2[1][i - 1] * 2 % (k + 1);
  vector<int> after(N);
  int md1 = 0, md2 = 0;
  reverse(X.begin(), X.end());
  if (k > 1)
    rep(i, N) md1 = (md1 + pow2[0][i] * (X[i] == '1')) % (k - 1);
  rep(i, N) md2 = (md2 + pow2[1][i] * (X[i] == '1')) % (k + 1);
  rep(i, N) {
    if (X[i] == '0') {
      after[N - 1 - i] = (md2 + pow2[1][i]) % (k + 1);
    } else if (k > 1) {
      int temp = (md1 - pow2[0][i]) % (k - 1);
      if (temp < 0)
        temp += (k - 1);
      after[N - 1 - i] = temp;
    } else
      after[N - 1 - i] = -1;
  }
  rep(i, N) {
    if (after[i] != -1)
      cout << dp[after[i]] + 1 << endl;
    else
      cout << 0 << 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;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int dp[200001] = {};

int dfs(int n) {
  if (n == 0)
    return dp[0] = 0;
  else if (dp[n] != 0)
    return dp[n];
  int p = __builtin_popcount(n);
  return dp[n] = dfs(n % p) + 1;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string X;
  cin >> X;
  for (int i = 1; i <= 200000; i++) {
    dfs(i);
  }
  int pow2[2][N + 1];
  int k = 0;
  rep(i, N) {
    if (X[i] == '1')
      k++;
  }
  pow2[0][0] = 1;
  pow2[1][0] = 1;
  for (int i = 1; i <= N; i++)
    pow2[0][i] = pow2[0][i - 1] * 2 % (k - 1);
  for (int i = 1; i <= N; i++)
    pow2[1][i] = pow2[1][i - 1] * 2 % (k + 1);
  vector<int> after(N);
  int md1 = 0, md2 = 0;
  reverse(X.begin(), X.end());
  rep(i, N) md1 = (md1 + pow2[0][i] * (X[i] == '1')) % (k - 1);
  rep(i, N) md2 = (md2 + pow2[1][i] * (X[i] == '1')) % (k + 1);
  rep(i, N) {
    if (X[i] == '0') {
      after[N - 1 - i] = (md2 + pow2[1][i]) % (k + 1);
    } else {
      int temp = (md1 - pow2[0][i]) % (k - 1);
      if (temp < 0)
        temp += (k - 1);
      after[N - 1 - i] = temp;
    }
  }
  rep(i, N) cout << dp[after[i]] + 1 << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> b(n, 0);
  for (int i = 0; i < n; i++) {
    b[i] = (s[i] - '0');
  }

  int popcnt = 0;
  for (int i = 0; i < n; i++) {
    if (b[i])
      popcnt++;
  }

  vector<int> mod = {popcnt - 1, popcnt + 1};
  vector<int> a(2, 0);
  auto f = [&](int a, int b) {
    if (b == 0)
      return -1;
    return a % b;
  };
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < 2; k++) {
      a[k] = f((a[k] * 2) + b[i], mod[k]);
    }
  }

  vector<int> t(2, 1);
  vector<int> cur(n, 0);
  for (int i = n - 1; i >= 0; i--) {
    int p = b[i] ^ 1;
    int sgn = 1;
    if (b[i])
      sgn = -sgn;
    if (b[i] && popcnt - 1 == 0) {
      cur[i] = -1;
    } else {
      cur[i] = f(a[p] + t[p] * sgn + mod[p], mod[p]);
    }
    for (int k = 0; k < 2; k++)
      t[k] = f(t[k] * 2, mod[k]);
  }

  for (int i = 0; i < n; i++) {
    if (cur[i] < 0) {
      cout << 0 << endl;
      continue;
    }
    int ans = 1;
    while (cur[i] > 0) {
      ans++;
      cur[i] %= __builtin_popcount(cur[i]);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> b(n, 0);
  for (int i = 0; i < n; i++) {
    b[i] = (s[i] - '0');
  }

  int popcnt = 0;
  for (int i = 0; i < n; i++) {
    if (b[i])
      popcnt++;
  }

  vector<int> mod = {popcnt - 1, popcnt + 1};
  vector<int> a(2, 0);
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < 2; k++) {
      a[k] = ((a[k] * 2) + b[i]) % mod[k];
    }
  }

  vector<int> t(2, 1);
  vector<int> cur(n, 0);
  for (int i = n - 1; i >= 0; i--) {
    int p = b[i] ^ 1;
    int sgn = 1;
    if (b[i])
      sgn = -sgn;
    if (b[i] && popcnt - 1 == 0) {
      cur[i] = -1;
    } else {
      cur[i] = (a[p] + t[p] * sgn + mod[p]) % mod[p];
    }
    for (int k = 0; k < 2; k++)
      t[k] = (t[k] * 2) % mod[k];
  }

  for (int i = 0; i < n; i++) {
    if (cur[i] < 0) {
      cout << 0 << endl;
      continue;
    }
    int ans = 1;
    while (cur[i] > 0) {
      ans++;
      cur[i] %= __builtin_popcount(cur[i]);
    }
    cout << ans << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll a[200050];
char c[200050];
ll u;
ll powmi(ll a, ll n, ll mod) {
  ll ans = 1;
  a %= mod;
  while (n) {
    if (n & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    n = n >> 1;
  }
  return ans;
}
void chuli(ll k) {
  u++;
  ll ci = 0;
  ll h = k;
  while (h != 0) {
    if (h & 1)
      ci++;
    h >>= 1;
  }
  k = k % ci;
  if (k != 0)
    chuli(k);
}
int main() {
  ll i, k, n, ci = 0;
  ll cs = 0, ct = 0;
  scanf("%lld", &n);
  scanf("%s", c);
  for (i = n - 1; i >= 0; i--) {
    if (c[n - 1 - i] == '1')
      a[i] = 1;
    else
      a[i] = 0;
    if (a[i] == 1)
      ci++;
  }

  if (ci == 0) {
    for (i = n - 1; i >= 0; i--)
      printf("1\\n");
    return 0;
  }

  for (i = n - 1; i >= 0; i--) {
    if (a[i] == 1) {
      cs = (cs + powmi(2, i, ci + 1)) % (ci + 1);
      if (ci != 1)
        ct = (ct + powmi(2, i, ci - 1)) % (ci - 1);
    }
  }

  for (i = n - 1; i >= 0; i--) {
    u = 1;
    if (a[i] == 1 && ci == 1)
      u = 0;
    else if (a[i] == 1) {
      k = (ct + (ci - 1)) - powmi(2, i, ci - 1);
      k = k % (ci - 1);
      if (k != 0)
        chuli(k);
    } else {
      k = cs + powmi(2, i, ci + 1);
      k = k % (ci + 1);
      if (k != 0)
        chuli(k);
    }
    printf("%lld\\n", u);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll a[200050];
char c[200050];
ll u;
ll powmi(ll a, ll n, ll mod) {
  ll ans = 1;
  a %= mod;
  while (n) {
    if (n & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    n = n >> 1;
  }
  return ans;
}
void chuli(ll k) {
  u++;
  ll ci = 0;
  ll h = k;
  while (h != 0) {
    if (h & 1)
      ci++;
    h >>= 1;
  }
  k = k % ci;
  if (k != 0)
    chuli(k);
}
int main() {
  ll i, k, n, ci = 0;
  ll cs = 0, ct = 0;
  scanf("%lld", &n);
  scanf("%s", c);
  for (i = n - 1; i >= 0; i--) {
    if (c[n - 1 - i] == '1')
      a[i] = 1;
    else
      a[i] = 0;
    if (a[i] == 1)
      ci++;
  }
  for (i = n - 1; i >= 0; i--) {
    if (a[i] == 1) {
      cs = (cs + powmi(2, i, ci + 1)) % (ci + 1);
      ct = (ct + powmi(2, i, ci - 1)) % (ci - 1);
    }
  }

  for (i = n - 1; i >= 0; i--) {
    u = 1;
    if (a[i] == 1 && ci == 1)
      u = 0;
    else if (a[i] == 1) {
      k = (ct + (ci - 1)) - powmi(2, i, ci - 1);
      k = k % (ci - 1);
      if (k != 0)
        chuli(k);
    } else {
      k = cs + powmi(2, i, ci + 1);
      k = k % (ci + 1);
      if (k != 0)
        chuli(k);
    }
    printf("%lld\\n", u);
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  long long N;
  string sX;

  cin >> N;
  cin >> sX;

  long long origin_count = 0;

  for (auto s : sX) {
    if (s == '1')
      origin_count += 1;
  }

  long long one_r_count = origin_count - 1;
  long long zero_r_count = origin_count + 1;

  long long one_mod = 0;
  long long zero_mod = 0;

  for (auto s : sX) {
    if (one_r_count != 0)
      one_mod = ((long long)(one_mod * 2) + (long long)(s - '0')) % one_r_count;
    zero_mod =
        ((long long)(zero_mod * 2) + (long long)(s - '0')) % zero_r_count;
  }

  long long f[220000] = {0};
  long long pop[220000] = {0};
  for (int i = 1; i < 220000; ++i) {
    pop[i] = pop[i / 2] + i % 2;
    f[i] = f[i % pop[i]] + 1;
  }

  long long onepow[220000] = {1};
  long long zeropow[220000] = {1};
  for (int i = 1; i < 220000; ++i) {
    if (one_r_count != 0)
      onepow[i] = (long long)(onepow[i - 1] * 2) % one_r_count;
    zeropow[i] = (long long)(zeropow[i - 1] * 2) % zero_r_count;
  }

  for (int i = 1; i <= N; ++i) {
    long long Xi_n;
    if (sX[i - 1] == '1') {
      if (one_r_count != 0) {
        Xi_n = (one_mod - onepow[N - i]) % one_r_count;
        if (Xi_n < 0)
          Xi_n += one_r_count;
        Xi_n %= one_r_count;
        long long ans = (long long)(f[Xi_n] + 1);
        cout << ans << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      Xi_n = (zero_mod + zeropow[N - i] % zero_r_count);
      Xi_n %= zero_r_count;
      long long ans = (long long)(f[Xi_n] + 1);
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

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

int main(void) {
  int N;
  string sX;

  cin >> N;
  cin >> sX;

  int origin_count = 0;

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

  int one_r_count = origin_count - 1;
  int zero_r_count = origin_count + 1;

  int one_mod = 0;
  int zero_mod = 0;

  for (int i = 0; i < N; ++i) {
    if (one_r_count != 0)
      one_mod = (((int)one_mod << 1) + (int)(sX[i] - '0')) % one_r_count;
    zero_mod = (((int)zero_mod << 1) + (int)(sX[i]) - '0') % zero_r_count;
  }

  int f[220000] = {};
  int pop[220000] = {};
  for (int i = 1; i < 220000; ++i) {
    pop[i] = pop[i / 2] + i % 2;
    f[i] = f[i % pop[i]] + 1;
  }

  for (int i = 1; i <= N; ++i) {
    long long Xi_n;
    if (sX[i - 1] == '1') {
      Xi_n = abs(one_mod - (long long)pow(2.0, N - i) % one_r_count);
      Xi_n %= one_r_count;
      cout << f[Xi_n] + 1 << endl;
    } else {
      Xi_n = (zero_mod + (long long)pow(2.0, N - i) % zero_r_count);
      Xi_n %= zero_r_count;
      cout << f[Xi_n] + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\\n"

int_fast64_t mpow(int_fast64_t m, int_fast64_t x, int_fast64_t n) {
  using i64 = int_fast64_t;
  if (n == 0)
    return 1;
  if (n % 2)
    return ((x % m) * (mpow(m, x, n - 1) % m)) % m;
  else {
    i64 half = mpow(m, x, n / 2) % m;
    return (half * half) % m;
  }
}

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

  int N;
  string X;
  cin >> N >> X;
  reverse(rng(X));

  ll bits = 0;
  for (int i = 0; i < N; i++)
    if (X[i] == '1')
      bits++;
  ll Bm = 0, Bp = 0;
  for (int i = 0; i < N; i++)
    if (X[i] == '1') {
      if (bits - 1 != 0)
        Bm = (Bm + mpow(bits - 1, 2, i)) % (bits - 1);
      else
        Bm = 0;
      Bp = (Bp + mpow(bits + 1, 2, i)) % (bits + 1);
    }
  reverse(rng(X));
  vector<ll> dp(200001, 0);
  for (int i = 1; i <= 200000; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }
  for (int i = 0; i < N; i++) {
    ll ans = 0;
    if (X[i] == '1') {
      ll m = bits - 1;
      if (m != 0) {
        ans = Bm;
        ans = ((ans - mpow(m, 2, N - i - 1)) % m + m) % m;
      } else {
        cout << 0 << endl;
        continue;
      }
    } else {
      ll m = bits + 1;
      ans = Bp;
      ans = (ans + mpow(m, 2, N - i - 1)) % m;
    }
    cout << dp[ans] + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\\n"

int_fast64_t mpow(int_fast64_t m, int_fast64_t x, int_fast64_t n) {
  using i64 = int_fast64_t;
  if (n == 0)
    return 1;
  if (n % 2)
    return ((x % m) * (mpow(m, x, n - 1) % m)) % m;
  else {
    i64 half = mpow(m, x, n / 2) % m;
    return (half * half) % m;
  }
}

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

  int N;
  string X;
  cin >> N >> X;
  reverse(rng(X));

  ll bits = 0;
  for (int i = 0; i < N; i++)
    if (X[i] == '1')
      bits++;
  ll Bm = 0, Bp = 0;
  for (int i = 0; i < N; i++)
    if (X[i] == '1') {
      Bm = (Bm + mpow(bits - 1, 2, i)) % (bits - 1);
      Bp = (Bp + mpow(bits + 1, 2, i)) % (bits + 1);
    }
  reverse(rng(X));
  vector<ll> dp(200001, 0);
  for (int i = 1; i <= 200000; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }
  for (int i = 0; i < N; i++) {
    ll ans = 0;
    if (X[i] == '1') {
      ll m = bits - 1;
      if (m != 0) {
        ans = Bm;
        ans = ((ans - mpow(m, 2, N - i - 1)) % m + m) % m;
      } else {
        ans = 0;
      }
    } else {
      ll m = bits + 1;
      ans = Bp;
      ans = (ans + mpow(m, 2, N - i - 1)) % m;
    }
    cout << dp[ans] + 1 << endl;
  }

  return 0;
}