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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define ll long long

char s[200001];
int a[200001][2];
int p[200002];
int ans[200002];

int main(void) {
  int n, sum, temp1, temp2, k;
  scanf("%d", &n);
  scanf("%s", s);

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

  temp1 = temp2 = 1;
  for (int i = 1; i <= n; i++) {
    a[n - i][0] = temp1;
    a[n - i][1] = temp2;
    temp1 *= 2;
    if (sum != 1)
      temp1 %= sum - 1;

    temp2 *= 2;
    temp2 %= sum + 1;
  }

  temp1 = temp2 = 0;
  for (int i = 0; i < n; i++) {
    temp1 *= 2;
    temp1 += s[i] - '0';
    if (sum != 1)
      temp1 %= sum - 1;

    temp2 *= 2;
    temp2 += s[i] - '0';
    temp2 %= sum + 1;
  }

  for (int i = 1; i <= n + 1; i++) {
    k = i;
    while (k) {
      p[i] += k % 2;
      k /= 2;
    }
  }

  for (int i = 1; i <= n + 1; i++) {
    k = i;
    while (k) {
      k %= p[k];
      ans[i]++;
    }
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      printf("%d\\n", ans[(temp2 + a[i][1]) % (sum + 1)] + 1);
    else {
      if (sum == 1)
        printf("0\\n");
      else
        printf("%d\\n", ans[(temp1 - a[i][0] + sum - 1) % (sum - 1)] + 1);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define ll long long

char s[200001];
int a[200001][2];
int p[200002];
int ans[200002];

int main(void) {
  int n, sum, temp1, temp2, k;
  scanf("%d", &n);
  scanf("%s", s);

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

  temp1 = temp2 = 1;
  for (int i = 1; i <= n; i++) {
    a[n - i][0] = temp1;
    a[n - i][1] = temp2;
    temp1 *= 2;
    if (sum > 1)
      temp1 %= sum - 1;

    temp2 *= 2;
    temp2 %= sum + 1;
  }

  temp1 = temp2 = 0;
  for (int i = 0; i < n; i++) {
    temp1 *= 2;
    temp1 += s[i] - '0';
    if (sum > 1)
      temp1 %= sum - 1;

    temp2 *= 2;
    temp2 += s[i] - '0';
    temp2 %= sum + 1;
  }

  for (int i = 1; i <= n + 1; i++) {
    k = i;
    while (k) {
      p[i] += k % 2;
      k /= 2;
    }
  }

  for (int i = 1; i <= n + 1; i++) {
    k = i;
    while (k) {
      k %= p[k];
      ans[i]++;
    }
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      printf("%d\\n", ans[(temp2 + a[i][1]) % (sum + 1)] + 1);
    else
      printf("%d\\n", ans[(temp1 - a[i][0] + sum - 1) % (sum - 1)] + 1);
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;
long long mp(long long b, long long e, long long m) {
  if (e == 0)
    return 1;
  long long a = mp(b, e / 2, m);
  a = a * a % m;
  if (e & 1)
    a = a * b % m;
  return a;
}
int get_ans(int X) {
  if (X == 0)
    return 0;
  return get_ans(X % __builtin_popcount(X)) + 1;
}
int main() {
  int N, pc = 0;
  scanf("%d", &N);
  char X[N];
  for (int i = 0; i < N; ++i)
    scanf(" %c", &X[i]);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1')
      ++pc;
  }
  int tz = 0, to = 0;
  for (int i = 0; i < N; ++i) {
    if (pc > 1)
      tz = ((tz << 1) + (X[i] == '1')) % (pc - 1);
    to = ((to << 1) + (X[i] == '1')) % (pc + 1);
  }
  for (int i = 0; i < N; ++i) {
    if (X[i] == '0')
      printf("%d\\n", get_ans((to + mp(2, N - 1 - i, pc + 1)) % (pc + 1)) + 1);
    else if (pc == 1)
      printf("0\\n");
    else
      printf("%d\\n",
             get_ans((tz - mp(2, N - 1 - i, pc - 1) + pc - 1) % (pc - 1)) + 1);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
long long mp(long long b, long long e, long long m) {
  if (e == 0)
    return 1;
  long long a = mp(b, e / 2, m);
  a = a * a % m;
  if (e & 1)
    a = a * b % m;
  return a;
}
int get_ans(int X) {
  if (X == 0)
    return 0;
  return get_ans(X % __builtin_popcount(X)) + 1;
}
int main() {
  int N, pc = 0;
  scanf("%d", &N);
  char X[N];
  for (int i = 0; i < N; ++i)
    scanf(" %c", &X[i]);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1')
      ++pc;
  }
  int tz = 0, to = 0;
  for (int i = 0; i < N; ++i) {
    if (pc != 1)
      tz = ((tz << 1) + (X[i] == '1')) % (pc - 1);
    to = ((to << 1) + (X[i] == '1')) % (pc + 1);
  }
  for (int i = 0; i < N; ++i) {
    if (X[i] == '0')
      printf("%d\\n", get_ans((to + mp(2, N - 1 - i, pc + 1)) % (pc + 1)) + 1);
    else
      printf("%d\\n",
             get_ans((tz - mp(2, N - 1 - i, pc - 1) + pc - 1) % (pc - 1)) + 1);
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll powmod(ll a, ll x, ll m) {
  ll res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % m;
    }
    a = (a * a) % m;
    x /= 2;
  }
  return res;
}

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

  int n;
  string x;
  cin >> n >> x;

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

  ll p1 = 0;
  ll p2 = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1' && b != 1) {
      p1 += powmod(2, n - i - 1, b + 1);
      p2 += powmod(2, n - i - 1, b - 1);
    }
  }

  for (int i = 0; i < n; i++) {
    ll y;
    if (x[i] == '0') {
      y = p1 + powmod(2, n - i - 1, b + 1);
      y %= b + 1;
    } else {
      if (b == 1) {
        cout << 0 << endl;
        continue;
      }
      y = p2 - powmod(2, n - i - 1, b - 1);
      y %= b - 1;
      if (y < 0)
        y += b - 1;
    }
    int ans = 1;
    while (y > 0) {
      y %= __builtin_popcount(y);
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll powmod(ll a, ll x, ll m) {
  ll res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % m;
    }
    a = (a * a) % m;
    x /= 2;
  }
  return res;
}

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

  int n;
  string x;
  cin >> n >> x;

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

  ll p1 = 0;
  ll p2 = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      p1 += powmod(2, n - i - 1, b + 1);
      p2 += powmod(2, n - i - 1, b - 1);
    }
  }

  for (int i = 0; i < n; i++) {
    ll y;
    if (x[i] == '0') {
      y = p1 + powmod(2, n - i - 1, b + 1);
      y %= b + 1;
    } else {
      if (b == 1) {
        cout << 0 << endl;
        break;
      }
      y = p2 - powmod(2, n - i - 1, b - 1);
      y %= b - 1;
      if (y < 0)
        y += b - 1;
    }
    int ans = 1;
    while (y > 0) {
      y %= __builtin_popcount(y);
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}
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++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF 1001001001

int popcount(int n) {
  int res = 0;
  while (n) {
    res += n & 1;
    n >>= 1;
  }
  return res;
}
int f(int n) {
  int res = 0;
  while (n) {
    n = n % popcount(n);
    res++;
  }
  return res;
}
int main(int, char **) {
  int n;
  cin >> n;

  string x;
  cin >> x;

  int onecnt = 0;
  rep(i, n) if (x[i] == '1') onecnt++;
  reverse(x.begin(), x.end());
  int mod1 = onecnt + 1;
  int mod2 = onecnt - 1;
  int msk1 = 1, msk2 = 1;
  int base1 = 0, base2 = 0;
  rep(i, n) {
    if (x[i] == '1') {
      base1 = (base1 + msk1) % mod1;
      if (mod2 > 0)
        base2 = (base2 + msk2) % mod2;
    }
    msk1 = (msk1 << 1) % mod1;
    if (mod2 > 0)
      msk2 = (msk2 << 1) % mod2;
  }
  msk1 = msk2 = 1;
  vector<int> ans;
  rep(i, n) {
    if (x[i] == '0') {
      ans.push_back(1 + f((base1 + msk1) % mod1));
    } else {
      if (mod2 > 0)
        ans.push_back(1 + f((base2 - msk2 + mod2) % mod2));
      else
        ans.push_back(0);
    }
    msk1 = (msk1 << 1) % mod1;
    if (mod2 > 0)
      msk2 = (msk2 << 1) % mod2;
  }
  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF 1001001001

int popcount(int n) {
  int res = 0;
  while (n) {
    res += n & 1;
    n >>= 1;
  }
  return res;
}
int f(int n) {
  int res = 0;
  while (n) {
    n = n % popcount(n);
    res++;
  }
  return res;
}
int main(int, char **) {
  int n;
  cin >> n;

  string x;
  cin >> x;

  int onecnt = 0;
  rep(i, n) if (x[i] == '1') onecnt++;
  reverse(x.begin(), x.end());
  int mod1 = onecnt + 1;
  int mod2 = onecnt - 1;
  int msk1 = 1, msk2 = 1;
  int base1 = 0, base2 = 0;
  rep(i, n) {
    if (x[i] == '1') {
      base1 = (base1 + msk1) % mod1;
      base2 = (base2 + msk2) % mod2;
    }
    msk1 = (msk1 << 1) % mod1;
    msk2 = (msk2 << 1) % mod2;
  }
  msk1 = msk2 = 1;
  vector<int> ans;
  rep(i, n) {
    if (x[i] == '0') {
      ans.push_back(1 + f((base1 + msk1) % mod1));
    } else {
      ans.push_back(1 + f((base2 - msk2 + mod2) % mod2));
    }
    msk1 = (msk1 << 1) % mod1;
    msk2 = (msk2 << 1) % mod2;
  }
  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans[i] << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 2e5 + 5;
int times[maxn];
int count(int a) {
  int cnt = 0;
  for (int lg = 0; (1 << lg) <= a; lg++)
    if (a & (1 << lg))
      cnt++;
  return cnt;
}
int mod_up[maxn], mod_down[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string ss;
  cin >> ss;
  for (int i = 1; i <= n; i++)
    times[i] = times[i % count(i)] + 1;
  int ones = 0;
  for (int i = 0; i < (int)ss.length(); i++)
    ones += (ss[i] == '1');
  int up = ones + 1, down = ones - 1;
  if (up) {
    mod_up[(int)ss.length() - 1] = 1 % up;
    for (int i = (int)ss.length() - 2; i >= 0; i--)
      mod_up[i] = (2 * mod_up[i + 1]) % up;
  }
  if (down) {
    mod_down[(int)ss.length() - 1] = 1 % down;
    for (int i = (int)ss.length() - 2; i >= 0; i--)
      mod_down[i] = (2 * mod_down[i + 1]) % down;
  }
  int bas_up = 0, bas_down = 0;
  for (int i = 0; i < (int)ss.length(); i++) {
    bas_up = (bas_up + (ss[i] == '1') * mod_up[i]) % up;
    if (down)
      bas_down = (bas_down + (ss[i] == '1') * mod_down[i]) % down;
  }
  for (int i = 0; i < (int)ss.length(); i++) {
    int res = 0;
    if (ss[i] == '0') {
      int val = (bas_up + mod_up[i]) % up;
      res = times[val] + 1;
    } else {
      if (down == 0)
        res = 0;
      else {
        int val = (bas_down + down - mod_down[i]) % down;
        res = times[val] + 1;
      }
    }
    cout << res << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 2e5 + 5;
int times[maxn];
int count(int a) {
  int cnt = 0;
  for (int lg = 0; (1 << lg) <= a; lg++)
    if (a & (1 << lg))
      cnt++;
  return cnt;
}
int mod_up[maxn], mod_down[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string ss;
  cin >> ss;
  for (int i = 1; i <= n; i++)
    times[i] = times[i % count(i)] + 1;
  int ones = 0;
  for (int i = 0; i < (int)ss.length(); i++)
    ones += (ss[i] == '1');
  int up = ones + 1, down = ones - 1;
  if (up) {
    mod_up[(int)ss.length() - 1] = 1 % up;
    for (int i = (int)ss.length() - 2; i >= 0; i--)
      mod_up[i] = (2 * mod_up[i + 1]) % up;
  }
  if (down) {
    mod_down[(int)ss.length() - 1] = 1 % down;
    for (int i = (int)ss.length() - 2; i >= 0; i--)
      mod_down[i] = (2 * mod_down[i + 1]) % down;
  }
  int bas_up = 0, bas_down = 0;
  for (int i = 0; i < (int)ss.length(); i++) {
    bas_up = (bas_up + (ss[i] == '1') * mod_up[i]) % up;
    bas_down = (bas_down + (ss[i] == '1') * mod_down[i]) % down;
  }
  for (int i = 0; i < (int)ss.length(); i++) {
    int res = 0;
    if (ss[i] == '0') {
      int val = (bas_up + mod_up[i]) % up;
      res = times[val] + 1;
    } else {
      int val = (bas_down + down - mod_down[i]) % down;
      res = times[val] + 1;
    }
    cout << res << '\\n';
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 500;
int a[N];
int lis[N];
int falg1[N], falg2[N];
ll quick(ll a, ll b, ll mod) {
  int ans = 1 % mod;
  a %= mod;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return ans % mod;
}
int main() {
  int n;
  scanf("%d", &n);
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%1d", &a[i]);
    sum += a[i];
  }
  if (sum > 1) {
    ll cut0 = 0, cut2 = 0;
    for (int i = 1; i <= n; ++i) {
      if (a[i] == 1) {
        cut0 = (cut0 + quick(2, n - i, sum - 1)) % (sum - 1);
        cut2 = (cut2 + quick(2, n - i, sum + 1)) % (sum + 1);
      }
    }
    for (int i = 1; i <= n; ++i) {
      ll ans = 0;
      int cut = 1;
      if (a[i] == 0) {
        ans += cut2;
        ans = (ans + quick(2, n - i, sum + 1)) % (sum + 1);
      } else {
        ans += cut0;
        ans = (ans - quick(2, n - i, sum - 1) + (sum - 1)) % (sum - 1);
      }
      while (ans > 0) {
        ans = ans % __builtin_popcount(ans);
        cut++;
      }
      printf("%d\\n", cut);
    }
  } else if (sum == 0) {
    for (int i = 1; i <= n; ++i) {
      printf("1\\n");
    }
  } else {
    for (int i = 1; i <= n; i++) {
      if (a[i] == 1) {
        printf("0\\n");
      } else {
        if (a[n] == 1 || i == n)
          printf("2\\n");
        else
          printf("1\\n");
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 500;
int a[N];
int lis[N];
int quick(int a, int b, int mod) {
  int ans = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return ans % mod;
}
int jude(int x) {
  int cnt = 0;
  while (x) {
    if (x & 1)
      cnt++;
    x /= 2;
  }
  return cnt;
}
int cal(int x) {
  int cnt = 0;
  while (x) {
    cnt++;
    x %= lis[x];
  }
  return cnt;
}
int main() {
  for (int i = 0; i < N; i++) {
    lis[i] = jude(i);
  }
  int n;
  scanf("%d", &n);
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%1d", &a[i]);
    sum += a[i];
  }
  if (sum > 1) {
    int cut0 = 0, cut2 = 0;
    for (int i = 1; i <= n; ++i) {
      if (a[i] == 1) {
        cut0 = (cut0 + quick(2, n - i, sum - 1)) % (sum - 1);
        cut2 = (cut2 + quick(2, n - i, sum + 1)) % (sum + 1);
      }
    }
    for (int i = 1; i <= n; ++i) {
      int ans = 0;
      int cut = 1;
      if (a[i] == 0) {
        ans += cut2;
        ans = (ans + quick(2, n - i, sum + 1)) % (sum + 1);
      } else {
        ans += cut0;
        ans = (ans - quick(2, n - i, sum - 1) + (sum - 1)) % (sum - 1);
      }
      cut += cal(ans);
      printf("%d\\n", cut);
    }
  } else if (sum == 0) {
    for (int i = 1; i <= n; ++i) {
      printf("1\\n");
    }
  } else {
    for (int i = 1; i <= n; i++) {
      if (a[i] == 1) {
        printf("0\\n");
      } else {
        if (a[n] == 1 || i == n)
          printf("2\\n");
        else
          printf("1\\n");
      }
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

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

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

int main() {
  int n, pc = 0;
  string s;
  cin >> n >> s;
  vector<int> x(n);
  vector<int> ans(n);
  rep(i, 0, n) {
    x[i] = s[i] - '0';
    if (s[i] == '1')
      pc++;
  }
  rep(b, 0, 2) {
    int npc = pc, r0 = 0;
    if (b == 0)
      npc++;
    else
      npc--;
    if (npc <= 0)
      continue;
    rep(i, 0, n) {
      r0 = (2 * r0) % npc;
      r0 += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; i--) {
      int r = r0;
      if (x[i] == b) {
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[i] = f(r) + 1;
      }
      k = (2 * k) % npc;
    }
  }
  rep(i, 0, n) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

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

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

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

int main() {
  int n, pc = 0;
  string s;
  cin >> n >> s;
  vector<int> x(n);
  vector<int> ans(n);
  rep(i, 0, n) {
    x[i] = s[i] - '0';
    if (s[i] == '1')
      pc++;
  }
  rep(b, 0, 2) {
    int npc = pc, r0 = 0;
    if (b == 0)
      npc++;
    else
      npc--;
    rep(i, 0, n) {
      r0 = (2 * r0) % npc;
      r0 += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; i--) {
      int r = r0;
      if (x[i] == b) {
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[i] = f(r) + 1;
      }
      k = (2 * k) % npc;
    }
  }
  rep(i, 0, n) cout << ans[i] << endl;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int count_bits(int n) {
  n = (n & 0x55555555) + (n >> 1 & 0x55555555);
  n = (n & 0x33333333) + (n >> 2 & 0x33333333);
  n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
  n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
  n = (n & 0x0000ffff) + (n >> 16 & 0x0000ffff);
  return n;
}

ll modpow(ll a, ll n, ll mod) {
  if (mod <= 0)
    return 0;
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int solve(int n) {
  if (n == 0)
    return 0;
  return solve(n % count_bits(n)) + 1;
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  ll p = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      p++;
  }
  ll a1 = 0;
  ll a2 = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (p == 1) {
        a1 = 0;
      } else {
        a1 = (a1 + modpow(2, N - i - 1, p - 1)) % (p - 1);
      }
      a2 = (a2 + modpow(2, N - i - 1, p + 1)) % (p + 1);
    }
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (p == 1) {
        cout << 0 << "\\n";
      } else {
        cout << 1 + solve((a1 - modpow(2, N - i - 1, p - 1) + p - 1) % (p - 1))
             << "\\n";
      }

    } else {
      cout << 1 + solve((a2 + modpow(2, N - i - 1, p + 1)) % (p + 1)) << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

int count_bits(int n) {
  n = (n & 0x55555555) + (n >> 1 & 0x55555555);
  n = (n & 0x33333333) + (n >> 2 & 0x33333333);
  n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
  n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
  n = (n & 0x0000ffff) + (n >> 16 & 0x0000ffff);
  return n;
}

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

int solve(int n) {
  if (n == 0)
    return 0;
  return solve(n % count_bits(n)) + 1;
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  ll p = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      p++;
  }
  ll a1 = 0;
  ll a2 = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      a1 = (a1 + modpow(2, N - i - 1, p - 1)) % (p - 1);
      a2 = (a2 + modpow(2, N - i - 1, p + 1)) % (p + 1);
    }
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      cout << 1 + solve((a1 - modpow(2, N - i - 1, p - 1) + p - 1) % (p - 1))
           << "\\n";
    } else {
      cout << 1 + solve((a2 + modpow(2, N - i - 1, p + 1)) % (p + 1)) << "\\n";
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;

int n;
char a[200005];
int f[200005], num[200005];
int a1, a2;
int ksm(long long x, int y, int mod) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int main() {
  int i, j, k, cnt = 0;
  scanf("%d", &n);
  scanf("%s", a);
  for (i = 1; i <= 200000; i++)
    num[i] = num[i >> 1] + (i & 1);
  for (i = 1; i <= 200000; i++)
    f[i] = f[i % num[i]] + 1;
  for (i = 0; i < n; i++)
    if (a[i] == '1')
      cnt++;
  for (i = 0, j = n - 1; i < j; i++, j--)
    swap(a[i], a[j]);
  j = 1;
  k = 1;
  for (i = 0; i < n; i++) {
    if (cnt > 1) {
      a1 = (a1 + (a[i] - '0') * j) % (cnt - 1);
      j = j * 2 % (cnt - 1);
    }
    a2 = (a2 + (a[i] - '0') * k) % (cnt + 1);
    k = k * 2 % (cnt + 1);
  }
  for (i = n - 1; i >= 0; i--) {
    if (a[i] == '0') {
      j = (a2 + ksm(2, i, cnt + 1)) % (cnt + 1);
      printf("%d\\n", f[j] + 1);
    } else if (a[i] == '1') {
      if (cnt == 1)
        printf("0\\n");
      else {
        j = (a1 - ksm(2, i, cnt - 1) + cnt - 1) % (cnt - 1);
        printf("%d\\n", f[j] + 1);
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int n;
char a[200005];
int f[200005], num[200005];
int a1, a2;
int ksm(int x, int y, int mod) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int main() {
  int i, j, k, cnt = 0;
  scanf("%d", &n);
  scanf("%s", a);
  for (i = 1; i <= 200000; i++)
    num[i] = num[i >> 1] + (i & 1);
  for (i = 1; i <= 200000; i++)
    f[i] = f[i % num[i]] + 1;
  for (i = 0; i < n; i++)
    if (a[i] == '1')
      cnt++;
  for (i = 0, j = n - 1; i < j; i++, j--)
    swap(a[i], a[j]);
  j = 1;
  k = 1;
  for (i = 0; i < n; i++) {
    a1 = (a1 + (a[i] - '0') * j) % (cnt - 1);
    a2 = (a2 + (a[i] - '0') * k) % (cnt + 1);
    j = j * 2 % (cnt - 1);
    k = k * 2 % (cnt + 1);
  }
  for (i = n - 1; i >= 0; i--) {
    if (a[i] == '0') {
      j = (a2 + ksm(2, i, cnt + 1)) % (cnt + 1);
      printf("%d\\n", f[j] + 1);
    } else if (a[i] == '1') {
      if (cnt == 1)
        printf("0\\n");
      else {
        j = (a1 - ksm(2, i, cnt - 1) + cnt - 1) % (cnt - 1);
        printf("%d\\n", f[j] + 1);
      }
    }
  }
  return 0;
}