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

#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin >> n;
  int we[200001];
  int an[200001];
  an[0] = 0;
  we[0] = 0;
  we[1] = 1;
  an[1] = 1;
  for (int i = 2; i < n; i++) {
    if (i % 2) {
      we[i] = we[i / 2] + 1;
    } else {
      we[i] = we[i / 2];
    }
    an[i] = an[i % we[i]] + 1;
  }
  string s;
  cin >> s;
  long long int z = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      z++;
  }
  long long int a = 0, b = 0;
  long long int x = z - 1, y = z + 1;
  if (x == 0)
    x = 1;
  for (int i = 0; i < n; i++) {
    a = (((long long int)(s[i] - '0')) + a * 2) % x;
    b = (((long long int)(s[i] - '0')) + b * 2) % y;
  }
  long long int wa[200001];
  long long int wb[200001];
  long long int oa = 1, ob = 1;
  for (int i = 0; i < n; i++) {
    wa[n - 1 - i] = oa % x;
    wb[n - 1 - i] = ob % y;
    oa = oa * 2 % x;
    ob = ob * 2 % y;
  }
  for (int i = 0; i < n; i++) {
    int o = (int)(s[i] - '0');
    if (o) {
      if (z > 1)
        cout << an[(a - wa[i] + x) % x] + 1 << endl;
      else
        cout << 0 << endl;
    } else {
      cout << an[(b + wb[i]) % y] + 1 << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin >> n;
  int we[200001];
  int an[200001];
  an[0] = 0;
  we[0] = 0;
  we[1] = 1;
  an[1] = 1;
  for (int i = 2; i < n; i++) {
    if (i % 2) {
      we[i] = we[i / 2] + 1;
    } else {
      we[i] = we[i / 2];
    }
    an[i] = an[i % we[i]] + 1;
  }
  string s;
  cin >> s;
  long long int z = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      z++;
  }
  long long int a = 0, b = 0;
  long long int x = z - 1, y = z + 1;
  for (int i = 0; i < n; i++) {
    if (x)
      a = (((long long int)(s[i] - '0')) + a * 2) % x;
    b = (((long long int)(s[i] - '0')) + b * 2) % y;
  }
  long long int wa[200001];
  long long int wb[200001];
  long long int oa = 1, ob = 1;
  for (int i = 0; i < n; i++) {
    wa[n - 1 - i] = oa % x;
    wb[n - 1 - i] = ob % y;
    oa = oa * 2 % x;
    ob = ob * 2 % y;
  }
  for (int i = 0; i < n; i++) {
    int o = (int)(s[i] - '0');
    if (o) {
      if (z > 1)
        cout << an[(a - wa[i] + x) % x] + 1 << endl;
      else
        cout << 0 << endl;
    } else {
      cout << an[(b + wb[i]) % y] + 1 << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
ll cntone(ll n) {
  ll cnt = 0, i;
  for (i = 60; i >= 0; i--)
    if ((n >> i) & 1)
      cnt++;
  return cnt;
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll i, j, ans = 0, cnt = 0, s = 0, s2 = 0;
  for (i = n - 1; i >= 0; i--)
    if (x[i] == '1')
      cnt++;
  if (n == 1) {
    if (x[0] == '1')
      cout << 0;
    else
      cout << 1;
    return 0;
  }
  if (cnt == 0) {
    for (i = 0; i < n; i++)
      cout << 1 << "\\n";
    return 0;
  }
  if (cnt == 1) {
    for (i = 0; i < n; i++) {
      if (x[i] == '1')
        cout << 0 << "\\n";
      else {
        if (x[n - 1] == '1')
          cout << 2 << "\\n";
        else {
          if (i == n - 1)
            cout << 2 << "\\n";
          else
            cout << 1 << "\\n";
        }
      }
    }
    return 0;
  }
  for (j = 0, i = n - 1; i >= 0; i--, j++) {
    if (x[i] == '1') {
      s = (s + power(2, j, cnt + 1)) % (cnt + 1);
      if (cnt - 1 != 0)
        s2 = (s2 + power(2, j, cnt - 1)) % (cnt - 1);
    }
    // cout<<s<<" "<<s2<<"\\n";
  }
  for (j = 0, i = n - 1; i >= 0; i--, j++) {
    ll w = 0;
    ans = 1;
    if (x[j] == '1') {
      if (cnt - 1 != 0)
        w = (s2 - power(2, i, cnt - 1) + cnt - 1) % (cnt - 1);
      else {
        w = 0;
        ans = 0;
      }
    } else
      w = (s + power(2, i, cnt + 1)) % (cnt + 1);
    // cout<<w<<" ";
    while (w != 0) {
      w = w % cntone(w);
      ans++;
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
ll cntone(ll n) {
  ll cnt = 0, i;
  for (i = 30; i >= 0; i--)
    if ((n >> i) & 1)
      cnt++;
  return cnt;
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll i, j, ans = 0, cnt = 0, s = 0, s2 = 0;
  for (i = n - 1; i >= 0; i--)
    if (x[i] == '1')
      cnt++;
  for (j = 0, i = n - 1; i >= 0; i--, j++) {
    if (x[i] == '1') {
      s = (s + power(2, j, cnt + 1)) % (cnt + 1);
      s2 = (s2 + power(2, j, cnt - 1)) % (cnt - 1);
    }
    // cout<<s<<" "<<s2<<"\\n";
  }
  for (j = 0, i = n - 1; i >= 0; i--, j++) {
    ll w;
    ans = 1;
    if (x[j] == '1')
      w = (s2 - power(2, i, cnt - 1) + cnt - 1) % (cnt - 1);
    else
      w = (s + power(2, i, cnt + 1)) % (cnt + 1);
    // cout<<w<<" ";
    while (w != 0) {
      w = w % cntone(w);
      ans++;
    }
    cout << ans << "\\n";
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  vector<int> b(n);
  rep(i, n) b[i] = s[i] - '0';
  rep(i, n) { cnt += b[i]; }
  vector<int> ans(n, 0);
  rep(i, 2) { // 1のとき0のとき
    int p = cnt;
    if (i == 0)
      p++;
    else
      p--;
    if (p <= 0)
      continue;
    int r0 = 0;
    rep(j, n) { // もとの10進数を求める
      r0 = (r0 * 2) % p;
      r0 += b[j];
    }

    int k = 1;
    for (int j = n - 1; j >= 0; --j) {
      if (b[j] == i) {
        int r = r0;
        if (i == 0)
          r = (r + k) % p; // 0->1のぶんたす
        else
          r = (r - k + p) % p; // r-kが負になるかも
        ans[j] = f(r) + 1;
      }
      k = (k * 2) % p;
    }
  }
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  int temp = 0;
  reverse(all(x));
  rep(i, n) {
    if (x[i] == '1')
      temp += pow(2.0, i);
  }

  for (ll i = n - 1; i >= 0; --i) {
    int y = temp;
    y ^= (1 << i);
    ll res = 0;
    while (1) {
      ll a = __builtin_popcountll(y);
      if (a == 0)
        break;
      y = y % a;
      res++;
      if (y == 0)
        break;
    }
    cout << res << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

int f(int k) {
  int cnt = 0;
  while (k > 0) {
    k %= __builtin_popcount(k);
    ++cnt;
  }
  return cnt;
}

ll modpow(ll k, ll n, ll m) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * k % m;
    k = k * k % m;
    n /= 2;
  }
  return res;
}

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

  int pc = 0;
  rep(i, n) pc += s[i] - '0';

  int a = 0, b = 0, mula = 1, mulb = 1;
  rep(i, n) {
    a += (s[n - i - 1] - '0') * mula;
    a %= pc + 1;
    b += (s[n - i - 1] - '0') * mulb;
    if (pc - 1 != 0)
      b %= pc - 1;
    mula *= 2;
    mula %= pc + 1;
    mulb *= 2;
    if (pc - 1 != 0)
      mulb %= pc - 1;
  }

  rep(i, n) {
    int d = s[i] - '0';
    int res = 0;
    if (d) {
      if (pc - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      res = modpow(2, n - i - 1, pc - 1);
      res = (b - res + pc - 1) % (pc - 1);
    } else {
      res = modpow(2, n - i - 1, pc + 1);
      res = (a + res) % (pc + 1);
    }
    cout << f(res) + 1 << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

int f(int k) {
  int cnt = 0;
  while (k > 0) {
    k %= __builtin_popcount(k);
    ++cnt;
  }
  return cnt;
}

ll modpow(ll k, ll n, ll m) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * k % m;
    k = k * k % m;
    n /= 2;
  }
  return res;
}

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

  int pc = 0;
  rep(i, n) pc += s[i] - '0';

  int a = 0, b = 0, mula = 1, mulb = 1;
  rep(i, n) {
    a += (s[n - i - 1] - '0') * mula;
    a %= pc + 1;
    b += (s[n - i - 1] - '0') * mulb;
    b %= pc - 1;
    mula *= 2;
    mula %= pc + 1;
    mulb *= 2;
    mulb %= pc - 1;
  }

  rep(i, n) {
    int d = s[i] - '0';
    int res = 0;
    if (d) {
      res = modpow(2, n - i - 1, pc - 1);
      res = (b - res + pc - 1) % (pc - 1);
    } else {
      res = modpow(2, n - i - 1, pc + 1);
      res = (a + res) % (pc + 1);
    }
    cout << f(res) + 1 << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int popcount(int x) {
  int n = 0;
  for (int i = 0; i < 32; i++) {
    if (x == 0)
      break;
    if ((x & 1) != 0)
      n++;
    x >>= 1;
  }
  return n;
}

int f(int x) { return x == 0 ? 0 : f(x % popcount(x)) + 1; }

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  valarray<int> v(n);

  for (int i = 0; i < n; i++) {
    v[i] = s[i] == '0' ? 0 : 1;
  }

  int popcnt = v.sum();
  int r1 = 0;
  int r2 = 0;

  if (popcnt != 0) {
    for (int i = 0; i < n; i++) {
      r1 *= 2;
      r1 += v[i];
      r1 %= (popcnt + 1);
    }
  }
  if (popcnt > 1) {
    for (int i = 0; i < n; i++) {
      r2 *= 2;
      r2 += v[i];
      r2 %= (popcnt - 1);
    }
  }

  vector<int> ans(n);

  for (int i = n - 1, x = 1, y = 1; i >= 0; i--) {
    if (v[i] == 0) {
      ans[i] = f((r1 + x) % (popcnt + 1)) + 1;
    } else {
      ans[i] = popcnt > 1 ? f((r2 - y + popcnt - 1) % (popcnt - 1)) + 1 : 0;
    }
    x = x * 2 % (popcnt + 1);
    if (popcnt - 1 != 0) {
      y = y * 2 % (popcnt - 1);
    }
  }

  for (auto &&e : ans) {
    cout << e << endl;
  }
}


Error: 0

Fixed submission:

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

int popcount(int x) {
  int n = 0;
  for (int i = 0; i < 32; i++) {
    if (x == 0)
      break;
    if (x & 1 != 0)
      n++;
    x >>= 1;
  }
  return n;
}

int f(int x) { return x == 0 ? 0 : f(x % popcount(x)) + 1; }

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  valarray<int> v(n);

  for (int i = 0; i < n; i++) {
    v[i] = s[i] == '0' ? 0 : 1;
  }

  int popcnt = v.sum();
  int r1 = 0;
  int r2 = 0;

  if (popcnt != 0) {
    for (int i = 0; i < n; i++) {
      r1 *= 2;
      r1 += v[i];
      r1 %= (popcnt + 1);
    }
  }
  if (popcnt > 1) {
    for (int i = 0; i < n; i++) {
      r2 *= 2;
      r2 += v[i];
      r2 %= (popcnt - 1);
    }
  }

  vector<int> ans(n);

  for (int i = n - 1, x = 1, y = 1; i >= 0; i--) {
    if (v[i] == 0) {
      ans[i] = f((r1 + x) % (popcnt + 1)) + 1;
    } else {
      int t = r2 - y;
      if (t <= 0)
        t += popcnt - 1;
      ans[i] = popcnt > 1 ? f(t % (popcnt - 1)) + 1 : 1;
    }
    x = x * 2 % (popcnt + 1);
    y = y * 2 % (popcnt - 1);
  }

  for (auto &&e : ans) {
    cout << e << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int N, F[MAXN], Pow0[MAXN], Pow1[MAXN];
char s[MAXN];
int main() {
  int c = 0;
  for (int i = 1; i < MAXN; ++i) {
    int v = i;
    int cnt = 0;
    while (v)
      cnt += (v & 1), v >>= 1;
    F[i] = F[i % cnt] + 1;
  }
  cin >> N;
  scanf("%s", s + 1);
  for (int i = 1; i <= N; ++i)
    if (s[i] == '1')
      ++c;
  int v0 = 0, v1 = 0;
  for (int i = 1; i <= N; ++i) {
    if (c - 1 > 0)
      v0 = (v0 * 2 + (s[i] - 48)) % (c - 1);
    v1 = (v1 * 2 + (s[i] - 48)) % (c + 1);
  }
  if (c - 1 > 0)
    Pow0[0] = 1 % (c - 1);
  Pow1[0] = 1 % (c + 1);
  for (int i = 1; i <= N; ++i) {
    if (c - 1 > 0)
      Pow0[i] = (Pow0[i - 1] * 2) % (c - 1);
    Pow1[i] = (Pow1[i - 1] * 2) % (c + 1);
  }
  for (int i = 1; i <= N; ++i)
    if (s[i] == '1') {
      if (c - 1 == 0) {
        printf("0\\n");
      } else {
        int v = v0;
        v -= Pow0[N - i];
        if (v < 0)
          v += (c - 1);
        printf("%d\\n", F[v] + 1);
      }
    } else {
      int v = v1;
      v += Pow1[N - i];
      if (v >= c + 1)
        v -= (c + 1);
      printf("%d\\n", F[v] + 1);
    }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int N, F[MAXN], Pow0[MAXN], Pow1[MAXN];
char s[MAXN];
int main() {
  int c = 0;
  for (int i = 1; i < MAXN; ++i) {
    int v = i;
    int cnt = 0;
    while (v)
      cnt += (v & 1), v >>= 1;
    F[i] = F[i % cnt] + 1;
  }
  cin >> N;
  scanf("%s", s + 1);
  for (int i = 1; i <= N; ++i)
    if (s[i] == '1')
      ++c;
  int v0 = 0, v1 = 0;
  for (int i = 1; i <= N; ++i) {
    v0 = (v0 * 2 + (s[i] - 48)) % (c - 1);
    v1 = (v1 * 2 + (s[i] - 48)) % (c + 1);
  }
  if (c - 1)
    Pow0[0] = 1 % (c - 1);
  Pow1[0] = 1 % (c + 1);
  for (int i = 1; i <= N; ++i) {
    if (c - 1)
      Pow0[i] = (Pow0[i - 1] * 2) % (c - 1);
    Pow1[i] = (Pow1[i - 1] * 2) % (c + 1);
  }
  for (int i = 1; i <= N; ++i)
    if (s[i] == '1') {
      if (c - 1 == 0) {
        printf("0\\n");
      } else {
        int v = v0;
        v -= Pow0[N - i];
        if (v < 0)
          v += (c - 1);
        printf("%d\\n", F[v] + 1);
      }
    } else {
      int v = v1;
      v += Pow1[N - i];
      if (v >= c + 1)
        v -= (c + 1);
      printf("%d\\n", F[v] + 1);
    }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

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

  int popx = 0;
  rep(i, n) {
    if (s[i] == '1')
      popx++;
  }
  int px1 = popx + 1, px2 = popx - 1;

  vector<ll> x1(n + 1, 0);
  vector<ll> x2(n + 1, 0);
  ll x1sum = 0;
  ll x2sum = 0;
  ll tmp1 = 1;
  ll tmp2 = 1;
  rep(i, n) {
    if (popx <= 1)
      break;
    x1[n - i - 1] = tmp1;
    x2[n - i - 1] = tmp2;

    tmp1 *= 2;
    tmp1 %= px1;
    tmp2 *= 2;
    tmp2 %= px2;
  }
  rep(i, n) {
    if (popx <= 1)
      break;
    if (s[i] == '1') {
      x1sum += x1[i];
      x2sum += x2[i];
    }
  }

  rep(i, n) {
    if (popx == 0) {
      cout << 1 << endl;
      continue;
    } else if (popx == 1) {
      if (s[i] == '1')
        cout << 0 << endl;
      else if (i == n - 1)
        cout << 2 << endl;
      else
        cout << 1 << endl;
      continue;
    }

    ll res = 1;
    ll sum = 0;
    if (s[i] == '0') {
      sum += (x1sum + x1[i]) % px1;
    } else {
      sum += (x2sum - x2[i]) % px2;
    }

    while (sum > 0) {
      res++;
      sum %= __builtin_popcount(sum);
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

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

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

  int popx = 0;
  rep(i, n) {
    if (s[i] == '1')
      popx++;
  }
  int px1 = popx + 1, px2 = popx - 1;

  vector<ll> x1(n + 1, 0);
  vector<ll> x2(n + 1, 0);
  ll x1sum = 0;
  ll x2sum = 0;
  ll tmp1 = 1;
  ll tmp2 = 1;
  rep(i, n) {
    x1[n - i - 1] = tmp1;
    x2[n - i - 1] = tmp2;

    tmp1 *= 2;
    tmp1 %= px1;
    tmp2 *= 2;
    tmp2 %= px2;
  }
  rep(i, n) {
    if (s[i] == '1') {
      x1sum += x1[i];
      x2sum += x2[i];
    }
  }

  rep(i, n) {
    if (popx == 0) {
      cout << 1 << endl;
      continue;
    } else if (popx == 1) {
      if (s[i] == '1')
        cout << 0 << endl;
      else if (i == n - 1)
        cout << 2 << endl;
      else
        cout << 1 << endl;
    }

    ll res = 1;
    ll sum = 0;
    if (s[i] == '0') {
      sum += (x1sum + x1[i]) % px1;
    } else {
      sum += (x2sum - x2[i]) % px2;
    }

    while (sum > 0) {
      res++;
      sum %= __builtin_popcount(sum);
    }
    cout << res << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

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

ll mod(ll val, ll m) {
  ll res = val % m;
  if (res < 0)
    res += m;
  return res;
}

ll popcnt(ll n) {
  ll c;
  c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555);
  c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333);
  c = (c & 0x0f0f0f0f0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f0f0f0f0f);
  c = (c & 0x00ff00ff00ff00ff) + ((c >> 8) & 0x00ff00ff00ff00ff);
  c = (c & 0x0000ffff0000ffff) + ((c >> 16) & 0x0000ffff0000ffff);
  c = (c & 0x00000000ffffffff) + ((c >> 32) & 0x00000000ffffffff);
  return c;
}

int main() {
  ll n;
  string X;
  cin >> n >> X;
  vector<int> x(n);
  int s = 0;
  rep(i, 0, n) {
    x[i] = stoi(X.substr(i, 1));
    if (x[i] == 1)
      s++;
  }
  ll a = 0, b = 0;
  rep(i, 0, n) {
    if (x[i] == 0)
      continue;
    a += modpow(2, n - i - 1, s + 1);
    a %= s + 1;
    if (s == 1)
      continue;
    b += modpow(2, n - i - 1, s - 1);
    b %= s - 1;
  }
  rep(i, 0, n) {
    int cnt = 1;
    int p = 0;
    if (x[i] == 0) {
      p = (a + modpow(2, n - i - 1, s + 1)) % (s + 1);
    } else {
      if (s == 1) {
        cout << 0 << endl;
        continue;
      }
      p = mod(b - modpow(2, n - i - 1, s - 1), s - 1);
    }
    while (p > 0) {
      p %= popcnt(p);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

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

ll mod(ll val, ll m) {
  ll res = val % m;
  if (res < 0)
    res += m;
  return res;
}

ll popcnt(ll n) {
  ll c;
  c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555);
  c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333);
  c = (c & 0x0f0f0f0f0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f0f0f0f0f);
  c = (c & 0x00ff00ff00ff00ff) + ((c >> 8) & 0x00ff00ff00ff00ff);
  c = (c & 0x0000ffff0000ffff) + ((c >> 16) & 0x0000ffff0000ffff);
  c = (c & 0x00000000ffffffff) + ((c >> 32) & 0x00000000ffffffff);
  return c;
}

int main() {
  ll n;
  string X;
  cin >> n >> X;
  vector<int> x(n);
  int s = 0;
  rep(i, 0, n) {
    x[i] = stoi(X.substr(i, 1));
    if (x[i] == 1)
      s++;
  }
  ll a = 0, b = 0;
  rep(i, 0, n) {
    if (x[i] == 0)
      continue;
    a += modpow(2, n - i - 1, s + 1);
    a %= s + 1;
    b += modpow(2, n - i - 1, s - 1);
    b %= s - 1;
  }
  rep(i, 0, n) {
    int cnt = 1;
    int p = 0;
    if (x[i] == 0) {
      p = (a + modpow(2, n - i - 1, s + 1)) % (s + 1);
    } else {
      p = mod(b - modpow(2, n - i - 1, s - 1), s - 1);
    }
    while (p > 0) {
      p %= popcnt(p);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int popc(string s) {
  int ret = 0;
  for (auto c : s) {
    ret += (c == '1');
  }
  return ret;
}

int f(int n) {
  if (n == 0) {
    return 0;
  }

  string s = "";
  int t = n;
  while (t > 0) {
    s += (t % 2 ? '1' : '0');
    t /= 2;
  }
  reverse(s.begin(), s.end());
  return 1 + f(n % popc(s));
}

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

  int cnt = popc(X);

  vector<vector<int>> memo(N + 1, vector<int>(2, 0));
  int b0 = 1, b1 = 1;
  for (int i = N - 1; i >= 0; --i) {
    memo[i][0] = b0 % (cnt + 1);
    (b0 *= 2) %= (cnt + 1);
    if (cnt > 1) {
      memo[i][1] = b1 % (cnt - 1);
      (b1 *= 2) %= (cnt - 1);
    }
  }

  int sum0 = 0, sum1 = 0;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      (sum0 += memo[i][0]) %= (cnt + 1);
      if (cnt > 1) {
        (sum1 += memo[i][1]) %= (cnt - 1);
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    if (cnt == 1 && X[i] == '1') {
      cout << 0 << endl;
      continue;
    }

    if (X[i] == '0') {
      int sum = (sum0 + memo[i][0]) % (cnt + 1);
      cout << 1 + f(sum) << endl;
    } else {
      int sum = (sum1 - memo[i][1] + (cnt - 1)) % (cnt - 1);
      cout << 1 + f(sum) << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int popc(string s) {
  int ret = 0;
  for (auto c : s) {
    ret += (c == '1');
  }
  return ret;
}

int f(int n) {
  if (n == 0) {
    return 0;
  }

  string s = "";
  int t = n;
  while (t > 0) {
    s += (t % 2 ? '1' : '0');
    t /= 2;
  }
  reverse(s.begin(), s.end());
  return 1 + f(n % popc(s));
}

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

  int cnt = popc(X);

  vector<vector<int>> memo(N + 1, vector<int>(2, 0));
  int b0 = 1, b1 = 1;
  for (int i = N - 1; i >= 0; --i) {
    memo[i][0] = b0 % (cnt + 1);
    memo[i][1] = b1 % (cnt - 1);
    (b0 *= 2) %= (cnt + 1);
    (b1 *= 2) %= (cnt - 1);
  }

  int sum0 = 0, sum1 = 0;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      (sum0 += memo[i][0]) %= (cnt + 1);
      (sum1 += memo[i][1]) %= (cnt - 1);
    }
  }

  for (int i = 0; i < N; ++i) {
    if (X[i] == '0') {
      int sum = (sum0 + memo[i][0]) % (cnt + 1);
      cout << 1 + f(sum) << endl;
    } else {
      int sum = (sum1 - memo[i][1] + (cnt - 1)) % (cnt - 1);
      cout << 1 + f(sum) << endl;
    }
  }

  return 0;
}