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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int cnt[200010], total, resi[200010][2], xremain[2];
int main() {
  int n;
  char x[200010];
  cin >> n >> x;
  for (int i = 0; i <= n; ++i)
    cnt[i] = cnt[i >> 1] + (i & 1);
  for (int i = 0; i < n; ++i)
    if (x[i] == '1')
      ++total;

  if (total != 1)
    resi[0][0] = 1 % (total - 1);
  resi[0][1] = 1 % (total + 1);
  for (int i = 1; i < n; ++i) {
    if (total != 1)
      resi[i][0] = (resi[i - 1][0] * 2) % (total - 1);
    resi[i][1] = (resi[i - 1][1] * 2) % (total + 1);
  }
  for (int i = 0; i < n; ++i) {
    if (total != 1)
      xremain[0] = (xremain[0] * 2 + x[i] - '0') % (total - 1),
      xremain[1] = (xremain[1] * 2 + x[i] - '0') % (total + 1);
  }

  for (int i = 0; i < n; ++i) {
    int t, rem;
    if (x[i] == '0') {
      t = total + 1;
      rem = (xremain[1] + resi[n - 1 - i][1]) % t;
    } else {
      t = total - 1;
      if (t == 0) {
        printf("0\\n");
        continue;
      }
      rem = (t + xremain[0] - resi[n - 1 - i][0]) % t;
    }

    int ans = 1;
    while (rem) {
      rem %= cnt[rem];
      ++ans;
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int cnt[200010], total, resi[200010][2], xremain[2];
int main() {
  int n;
  char x[200010];
  cin >> n >> x;
  for (int i = 0; i <= n; ++i)
    cnt[i] = cnt[i >> 1] + (i & 1);
  for (int i = 0; i < n; ++i)
    if (x[i] == '1')
      ++total;

  resi[0][0] = 1 % (total - 1);
  resi[0][1] = 1 % (total + 1);
  for (int i = 1; i < n; ++i)
    resi[i][0] = (resi[i - 1][0] * 2) % (total - 1),
    resi[i][1] = (resi[i - 1][1] * 2) % (total + 1);
  for (int i = 0; i < n; ++i)
    xremain[0] = (xremain[0] * 2 + x[i] - '0') % (total - 1),
    xremain[1] = (xremain[1] * 2 + x[i] - '0') % (total + 1);

  for (int i = 0; i < n; ++i) {
    int t, rem;
    if (x[i] == '0') {
      t = total + 1;
      rem = (xremain[1] + resi[n - 1 - i][1]) % t;
    } else {
      t = total - 1;
      rem = (t + xremain[0] - resi[n - 1 - i][0]) % t;
    }
    //		printf("i = %d, rem = %d\\n", i, rem);
    int ans = 1;
    while (rem) {
      rem %= cnt[rem];
      ++ans;
    }
    printf("%d\\n", ans);
  }
  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 n;
string x;

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

ll f(int v) {
  if (v == 0)
    return 0;
  int ppp = __builtin_popcount(v);
  int v2 = v % ppp;
  if (v2 == 0)
    return 1;
  else
    return f(v2) + 1;
}

signed main() {
  cin >> n >> x;
  int pp = 0;
  rep(i, n) if (x[i] == '1') pp++;

  ll mpp = 0, mpm = 0;

  rep(i, n) {
    if (x[i] == '0')
      continue;
    mpp += modpow(2, n - 1 - i, pp + 1);
    mpp %= pp + 1;
  }
  if (pp == 1) {
  } else {
    rep(i, n) {
      if (x[i] == '0')
        continue;
      mpm += modpow(2, n - 1 - i, pp - 1);
      mpm %= pp - 1;
    }
  }

  rep(pos, n) {
    char cp = x[pos];
    ll mp;

    if (x[pos] == '1') {
      if (pp == 1) {
        cout << 0 << endl;
        continue;
      } else {
        mp = mpm - modpow(2, n - 1 - pos, pp - 1);
        if (mp < 0)
          mp += pp - 1;
      }
    } else {
      mp = mpp + modpow(2, n - 1 - pos, pp + 1);
      mp %= pp + 1;
    }
    cout << f(mp) + 1 << endl;
  }
}


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 n;
string x;

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

ll f(int v) {
  if (v == 0)
    return 0;
  int ppp = __builtin_popcount(v);
  int v2 = v % ppp;
  if (v2 == 0)
    return 1;
  else
    return f(v2) + 1;
}

signed main() {
  cin >> n >> x;
  int pp = 0;
  rep(i, n) if (x[i] == '1') pp++;

  ll mpp = 0, mpm = 0;

  rep(i, n) {
    if (x[i] == '0')
      continue;
    mpp += modpow(2, n - 1 - i, pp + 1);
    mpp %= pp + 1;
  }
  rep(i, n) {
    if (x[i] == '0')
      continue;
    mpm += modpow(2, n - 1 - i, pp - 1);
    mpm %= pp - 1;
  }

  rep(pos, n) {
    char cp = x[pos];
    ll mp;

    if (x[pos] == '1') {
      mp = mpm - modpow(2, n - 1 - pos, pp - 1);
      if (mp < 0)
        mp += pp - 1;
    } else {
      mp = mpp + modpow(2, n - 1 - pos, pp + 1);
      mp %= pp + 1;
    }
    cout << f(mp) + 1 << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int n, res[3], dp[200005][3], cnt, sum, ans, a[200005], b[200005], f[200005];

int main() {
  // freopen("rand.in", "r", stdin);
  // freopen("zhengjie.out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x = i;
    while (x) {
      b[i] += x & 1;
      x >>= 1;
    }
  }
  for (int i = 1; i <= n; i++)
    scanf("%1d", &a[i]), cnt += a[i];
  // cout << cnt << endl;
  for (int i = 1; i <= n; i++) {
    res[1] = (res[1] * 2 + a[i]) % (cnt + 1);
    if (cnt != 1)
      res[2] = (res[2] * 2 + a[i]) % (cnt - 1);
    else
      res[2] = 0;
  }
  dp[n][1] = dp[n][2] = 1;
  for (int i = n - 1; i >= 1; i--) {
    dp[i][1] = (dp[i + 1][1] * 2) % (cnt + 1);
    if (cnt != 1)
      dp[i][2] = (dp[i + 1][2] * 2) % (cnt - 1);
    else
      dp[i][2] = 0;
  }
  for (int i = 1; i <= n; i++) {
    // cout << cnt << endl;
    int mo = cnt + (a[i] == 0 ? 1 : -1);
    if (mo == 0) {
      cout << 0 << endl;
      continue;
    }
    sum = (res[a[i] == 0 ? 1 : 2] +
           (a[i] == 0 ? 1 : -1) * dp[i][a[i] == 0 ? 1 : 2] + mo) %
          mo;
    // cout << res[a[i] == 0? 1: 2] << ' ' << sum << ' ' << (a[i] == 0? 1: -1) *
    // dp[i][a[i] == 0? 1: 2] << ' ' << mo << ' ';
    ans = 1;
    while (sum) {
      sum = sum % b[sum];
      ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

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

int fast(int a, int b, int p) {
  int sum = 1;
  while (b) {
    if (b & 1)
      sum = sum * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return sum;
}

int n, res[3], cnt, sum, ans, a[200005], b[200005], f[200005];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x = i;
    while (x) {
      b[i] += x & 1;
      x >>= 1;
    }
  }
  for (int i = 1; i <= n; i++)
    scanf("%1d", &a[i]), cnt += a[i];
  // cout << cnt << endl;
  for (int i = 1; i <= n; i++)
    res[1] = (res[1] * 2 + a[i]) % (cnt + 1),
    res[2] = (res[2] * 2 + a[i]) % (cnt - 1);
  for (int i = 1; i <= n; i++) {
    // cout << cnt << endl;
    int mo = cnt + (a[i] == 0 ? 1 : -1);
    sum = (res[a[i] == 0 ? 1 : 2] + (a[i] == 0 ? 1 : -1) * fast(2, n - i, mo) +
           mo) %
          mo;
    // cout << res[a[i] == 0? 1: 2] << ' ' << sum << ' ' << f[i] << ' ' << mo <<
    // endl;
    ans = 1;
    while (sum) {
      sum = sum % b[sum];
      ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long
int pw(int n, int k, int MOD) {
  assert(k >= 0);
  int res = 1;
  while (k) {
    if (k & 1)
      (res *= n) %= MOD;
    (n *= n) %= MOD;
    k >>= 1;
  }
  return res;
}
int f(string s, int a) {
  if (a == 0)
    return -1;
  int res = 0, t = 1;
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++) {
    (res += t * (s[i] == '1')) %= a;
    (t *= 2) %= a;
  }
  return res;
}
int p(int a) {
  int res = 0;
  while (a) {
    if (a & 1)
      res++;
    a >>= 1;
  }
  return res;
}
int F(int a) {
  int res = 0;
  while (a) {
    res++;
    a %= p(a);
  }
  return res;
}

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sum = 0;
  for (char p : s)
    sum += p == '1';
  int A = f(s, sum + 1);
  int B = f(s, sum - 1);
  for (int i = 0; i < n; i++) {
    if (sum == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    if (s[i] == '0') {
      int res = A;
      res += pw(2, n - i - 1, sum + 1);
      res %= sum + 1;
      cout << F(res) + 1 << endl;
    } else {
      int res = B;
      res -= pw(2, n - i - 1, sum - 1);
      res += sum - 1;
      res %= sum - 1;
      cout << F(res) + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

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

#define int long long
int pw(int n, int k, int MOD) {
  assert(k >= 0);
  int res = 1;
  while (k) {
    if (k & 1)
      (res *= n) %= MOD;
    (n *= n) %= MOD;
    k >>= 1;
  }
  return res;
}
int f(string s, int a) {
  int res = 0, t = 1;
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++) {
    (res += t * (s[i] == '1')) %= a;
    (t *= 2) %= a;
  }
  return res;
}
int p(int a) {
  int res = 0;
  while (a) {
    if (a & 1)
      res++;
    a >>= 1;
  }
  return res;
}
int F(int a) {
  int res = 0;
  while (a) {
    res++;
    a %= p(a);
  }
  return res;
}

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sum = 0;
  for (char p : s)
    sum += p == '1';
  int A = f(s, sum + 1);
  int B = f(s, sum - 1);
  for (int i = 0; i < n; i++) {
    if (sum == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    if (s[i] == '0') {
      int res = A;
      res += pw(2, n - i - 1, sum + 1);
      res %= sum + 1;
      cout << F(res) + 1 << endl;
    } else {
      int res = B;
      res -= pw(2, n - i - 1, sum - 1);
      res += sum - 1;
      res %= sum - 1;
      cout << F(res) + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

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

using namespace std;

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

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

  int popcount_orig = 0;
  rep(i, N) {
    if (S[i] == '1')
      popcount_orig++;
  }

  auto pc0 = popcount_orig + 1;
  auto pc1 = popcount_orig - 1;

  int r0 = 0;
  int r1 = 0;
  rep(i, N) {
    int b = S[i] - '0';
    r0 = (r0 * 2) % pc0;
    if (pc1 != 0)
      r1 = (r1 * 2) % pc1;
    r0 += b;
    r1 += b;
  }
  vector<int> pow0(N);
  vector<int> pow1(N);
  int p0 = 1;
  int p1 = 1;
  for (int i = N - 1; i >= 0; --i) {
    pow0[i] = p0;
    pow1[i] = p1;
    p0 *= 2;
    p0 %= pc0;
    p1 *= 2;
    if (pc1 != 0)
      p1 %= pc1;
  }

  rep(i, N) {
    int b = S[i] - '0';
    if (b == 1) {
      if (pc1 > 0)
        cout << f((r1 - pow1[i] + pc1) % pc1) + 1 << endl;
      else
        cout << 0 << endl;
    } else {
      cout << f((r0 + pow0[i]) % pc0) + 1 << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

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

using namespace std;

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

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

  int popcount_orig = 0;
  rep(i, N) {
    if (S[i] == '1')
      popcount_orig++;
  }

  auto pc0 = popcount_orig + 1;
  auto pc1 = popcount_orig - 1;

  int r0 = 0;
  int r1 = 0;
  rep(i, N) {
    int b = S[i] - '0';
    r0 = (r0 * 2) % pc0;
    r1 = (r1 * 2) % pc1;
    r0 += b;
    r1 += b;
  }
  vector<int> pow0(N);
  vector<int> pow1(N);
  int p0 = 1;
  int p1 = 1;
  for (int i = N - 1; i >= 0; --i) {
    pow0[i] = p0;
    pow1[i] = p1;
    p0 *= 2;
    p0 %= pc0;
    p1 *= 2;
    p1 %= pc1;
  }

  rep(i, N) {
    int b = S[i] - '0';
    if (b == 1) {
      cout << f((r1 - pow1[i] + pc1) % pc1) + 1 << endl;
    } else {
      cout << f((r0 + pow0[i]) % pc0) + 1 << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

int f(int x) {
  int ret = 0;
  while (x) {
    x %= __builtin_popcount(x);
    ++ret;
  }
  return ret;
}

int binpow(long long b, long long e, int mod) {
  if (!e)
    return 1;
  if (e % 2)
    return b * binpow(b, e - 1, mod) % mod;
  return binpow(b * b % mod, e / 2, mod);
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      ++cnt;
  }
  int x1 = 0, x0 = 0;
  for (int i = 0; i < n; ++i) {
    (x0 *= 2) %= (cnt + 1);
    if (cnt > 1)
      (x1 *= 2) %= (cnt - 1);
    if (s[i] == '0')
      continue;
    (x0 += 1) %= (cnt + 1);
    if (cnt > 1)
      (x1 += 1) %= (cnt - 1);
  }
  for (int p, x, i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (cnt == 1) {
        cout << "0\\n";
        continue;
      }
      p = binpow(2, n - i - 1, cnt - 1);
      x = (x1 - p + cnt - 1) % (cnt - 1);
    } else {
      p = binpow(2, n - i - 1, cnt + 1);
      x = (x0 + p) % (cnt + 1);
    }
    cout << 1 + f(x) << "\\n";
  }
}

int main() {
  init();
  int t = 1; // scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

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

int cnt = 0;

int binpow(int b, int e, int mod) {
  if (!e)
    return 1;
  if (e % 2)
    return b * binpow(b, e - 1, mod) % mod;
  return binpow(b * b % mod, e / 2, mod);
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      ++cnt;
  }
  int x1 = 0, x0 = 0;
  for (int i = 0; i < n; ++i) {
    (x0 *= 2) %= (cnt + 1);
    (x1 *= 2) %= (cnt - 1);
    if (s[i] == '0')
      continue;
    (x0 += 1) %= (cnt + 1);
    (x1 += 1) %= (cnt - 1);
  }
  for (int p, x, i = 0; i < n; ++i) {
    if (s[i] == '1') {
      p = binpow(2, n - i - 1, cnt - 1);
      x = (x1 - p) % (cnt - 1);
      while (x < 0)
        x += cnt - 1;
    } else {
      p = binpow(2, n - i - 1, cnt + 1);
      x = (x0 + p) % (cnt + 1);
    }
    cout << 1 + f(x) << "\\n";
  }
}

int main() {
  init();
  int t = 1; // scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

int func(long long a) {
  int ret = 0;
  for (int i = 0; i < 22; i++) {
    if ((a >> i & 1) == 1) {
      ret += 1;
    }
  }
  return ret;
}

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

  long long num = 0;
  for (int i = 0; i < n; i++) {
    num += (s[i] - '0');
  }

  long long a[2][200010] = {};
  a[0][n - 1] = a[1][n - 1] = 1;
  long long now[2];
  now[0] = now[1] = (s[n - 1] == '1' ? 1 : 0);

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

      if (s[i] == '1') {
        now[1] += a[1][i];
        now[1] %= num + 1;
      }
    }
  } else {
    for (int i = n - 2; i >= 0; i--) {
      a[0][i] = a[0][i + 1] * 2;
      a[1][i] = a[1][i + 1] * 2;
      a[0][i] %= num - 1;
      a[1][i] %= num + 1;

      if (s[i] == '1') {
        now[0] += a[0][i];
        now[1] += a[1][i];
        now[0] %= num - 1;
        now[1] %= num + 1;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    int mod;
    long long c;
    if (s[i] == '0') {
      c = now[1] + a[1][i];
      mod = num + 1;
    } else {
      c = now[0] - a[0][i];
      mod = num - 1;
    }

    if (mod == 0) {
      cout << 0 << endl;
      continue;
    }
    c %= mod;
    c = (c + mod) % mod;

    int ans = 1;
    while (c != 0) {
      mod = func(c);

      c %= mod;
      ans++;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int func(long long a) {
  int ret = 0;
  for (int i = 0; i < 22; i++) {
    if ((a >> i & 1) == 1) {
      ret += 1;
    }
  }
  return ret;
}

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

  long long num = 0;
  for (int i = 0; i < n; i++) {
    num += (s[i] - '0');
  }
  if (num == 0) {
    for (int i = 0; i < n; i++) {
      cout << 0 << endl;
    }
    return 0;
  }

  long long a[2][200010] = {};
  a[0][n - 1] = a[1][n - 1] = 1;
  long long now[2];
  now[0] = now[1] = (s[n - 1] == '1' ? 1 : 0);
  for (int i = n - 2; i >= 0; i--) {
    a[0][i] = a[0][i + 1] * 2;
    a[1][i] = a[1][i + 1] * 2;
    a[0][i] %= num - 1;
    a[1][i] %= num + 1;

    if (s[i] == '1') {
      now[0] += a[0][i];
      now[1] += a[1][i];
      now[0] %= num - 1;
      now[1] %= num + 1;
    }
  }

  for (int i = 0; i < n; i++) {
    int mod;
    long long c;
    if (s[i] == '0') {
      c = now[1] + a[1][i];
      mod = num + 1;
    } else {
      c = now[0] - a[0][i];
      mod = num - 1;
    }

    if (mod == 0) {
      cout << 0 << endl;
      continue;
    }
    c %= mod;
    c = (c + mod) % mod;

    int ans = 1;
    while (c != 0) {
      mod = func(c);

      c %= mod;
      ans++;
    }

    cout << ans << endl;
  }

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

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

#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _Rep(i, a, b) for (int i = a; i >= b; i--)
#define RepG(i, u) for (int i = head[u]; ~i; i = e[i].next)

typedef long long ll;

const int N = 2e5 + 5;

template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  x *= f;
}

int n;
int a[N], cnt;
int sum0, sum1;

int Qpow(int base, int ind, int p) {
  int res = 1;
  while (ind) {
    if (ind & 1)
      res = 1ll * res * base % p;
    base = 1ll * base * base % p;
    ind >>= 1;
  }
  return res;
}

int solve(int x) {
  if (!x)
    return 1;
  int t = x, count = 0;
  while (t) {
    if (t & 1)
      count++;
    t /= 2;
  }
  return solve(x % count) + 1;
}

int main() {
  read(n);
  Rep(i, 1, n) scanf("%1d", &a[i]), cnt += a[i];
  if (cnt == 1) {
    Rep(i, 1, n) sum0 += a[i] * Qpow(2, n - i, cnt + 1), sum0 %= (cnt + 1);
    Rep(i, 1, n) if (!a[i]) printf(
        "%d\\n", solve((sum0 + Qpow(2, n - i, cnt + 1) + cnt + 1) % (cnt + 1)));
    else puts("0");
    return 0;
  }
  Rep(i, 1, n) {
    sum0 += a[i] * Qpow(2, n - i, cnt + 1), sum0 %= (cnt + 1);
    sum1 += a[i] * Qpow(2, n - i, cnt - 1), sum1 %= (cnt - 1);
  }
  Rep(i, 1, n) if (a[i]) printf(
      "%d\\n", solve((sum1 - Qpow(2, n - i, cnt - 1) + (cnt - 1)) % (cnt - 1)));
  else printf("%d\\n",
              solve((sum0 + Qpow(2, n - i, cnt + 1) + cnt + 1) % (cnt + 1)));
  return 0;
}

Error: 0

Fixed submission:

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

#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _Rep(i, a, b) for (int i = a; i >= b; i--)
#define RepG(i, u) for (int i = head[u]; ~i; i = e[i].next)

typedef long long ll;

const int N = 2e5 + 5;

template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  x *= f;
}

int n;
int a[N], cnt;
int sum0, sum1;

int Qpow(int base, int ind, int p) {
  int res = 1;
  while (ind) {
    if (ind & 1)
      res = 1ll * res * base % p;
    base = 1ll * base * base % p;
    ind >>= 1;
  }
  return res;
}

int solve(int x) {
  if (!x)
    return 1;
  int t = x, count = 0;
  while (t) {
    if (t & 1)
      count++;
    t /= 2;
  }
  return solve(x % count) + 1;
}

int main() {
  read(n);
  Rep(i, 1, n) scanf("%1d", &a[i]), cnt += a[i];
  Rep(i, 1, n) {
    sum0 += a[i] * Qpow(2, n - i, cnt + 1), sum0 %= (cnt + 1);
    sum1 += a[i] * Qpow(2, n - i, cnt - 1), sum1 %= (cnt - 1);
  }
  Rep(i, 1, n) if (a[i]) printf(
      "%d\\n", solve((sum1 - Qpow(2, n - i, cnt - 1) + (cnt - 1)) % (cnt - 1)));
  else printf("%d\\n",
              solve((sum0 + Qpow(2, n - i, cnt + 1) + cnt + 1) % (cnt + 1)));
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long LL;
typedef pair<int, int> PII;
#define X first
#define Y second
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, a[200010], cnt, ans1, ans2, f1[200010], f2[200010];
char str[200010];
int f(int x) {
  if (x == 0)
    return 0;
  int tmp = x, count = 0;
  while (tmp) {
    if (tmp & 1)
      count++;
    tmp >>= 1;
  }
  return 1 + f(x % count);
}
int main() {
  n = read();
  scanf("%s", str);
  for (int i = 0; i < n; i++)
    a[i] = str[i] - '0', cnt += (a[i] == 1);
  if (cnt != 1) {
    for (int i = 0; i < n; i++)
      ans1 = (ans1 * 2 + a[i]) % (cnt - 1);
  }
  for (int i = 0; i < n; i++)
    ans2 = (ans2 * 2 + a[i]) % (cnt + 1);
  f1[n - 1] = 1;
  f2[n - 1] = 1;
  if (cnt != 1) {
    for (int i = n - 2; i >= 0; i--)
      f1[i] = (f1[i + 1] * 2) % (cnt - 1);
  }
  for (int i = n - 2; i >= 0; i--)
    f2[i] = (f2[i + 1] * 2) % (cnt + 1);
  for (int i = 0; i < n; i++) {
    int temp;
    if (a[i] == 1) {
      if (cnt == 1) {
        printf("0\\n");
        continue;
      }
      temp = (ans1 + cnt - 1 - f1[i]) % (cnt - 1);
    } else
      temp = (ans2 + f2[i]) % (cnt + 1);
    printf("%d\\n", 1 + f(temp));
  }
  return 0;
}
/*
1110111001011011001110
*/


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long LL;
typedef pair<int, int> PII;
#define X first
#define Y second
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, a[100010], cnt, ans1, ans2, f1[100010], f2[100010];
char str[100010];
int f(int x) {
  if (x == 0)
    return 0;
  int tmp = x, count = 0;
  while (tmp) {
    if (tmp & 1)
      count++;
    tmp >>= 1;
  }
  return 1 + f(x % count);
}
int main() {
  n = read();
  scanf("%s", str);
  for (int i = 0; i < n; i++)
    a[i] = str[i] - '0', cnt += (a[i] == 1);
  for (int i = 0; i < n; i++)
    ans1 = (ans1 * 2 + a[i]) % (cnt - 1), ans2 = (ans2 * 2 + a[i]) % (cnt + 1);
  f1[n - 1] = 1;
  f2[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--)
    f1[i] = (f1[i + 1] * 2) % (cnt - 1), f2[i] = (f2[i + 1] * 2) % (cnt + 1);
  for (int i = 0; i < n; i++) {
    int temp;
    if (a[i] == 1)
      temp = (ans1 + cnt - 1 - f1[i]) % (cnt - 1);
    else
      temp = (ans2 + f2[i]) % (cnt + 1);
    printf("%d\\n", 1 + f(temp));
  }
  return 0;
}
/*
1110111001011011001110
*/