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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define pf(x) cout << (x) << endl

ll f(ll n) {
  if (n == 0)
    return 1;
  return f(n % bitset<20>(n).count()) + 1;
}

// modの値の確認をすること
int main() {
  ll n, c = 0;
  string s;
  cin >> n >> s;
  rep0(i, n) if (s[i] == '1')++ c;
  ll k1 = 0, k2 = 0, r1 = 1, r2 = 1;
  rrep0(i, n) {
    if (s[i] == '1') {
      if (c > 1) {
        k1 += r1;
        k1 %= (c - 1);
      }
      k2 += r2;
      k2 %= (c + 1);
    }
    if (c > 1)
      r1 = 2 * r1 % (c - 1);
    r2 = 2 * r2 % (c + 1);
  }
  r1 = 1;
  r2 = 1;
  vector<ll> ans(n);
  rrep0(i, n) {
    if (s[i] == '1') {
      if (c > 1) {
        k1 -= r1;
        k1 += (c - 1) * 2;
        k1 %= (c - 1);

        ans[i] = f(k1);
        k1 += r1;
      } else
        ans[i] == 0;
    } else {
      k2 += r2;
      k2 %= (c + 1);

      ans[i] = f(k2);

      k2 -= r2;
      k2 += (c + 1) * 2;
      k2 %= (c + 1);
    }
    if (c > 1)
      r1 = 2 * r1 % (c - 1);
    r2 = 2 * r2 % (c + 1);
  }
  rep0(i, n) pf(ans[i]);
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define pf(x) cout << (x) << endl

ll f(ll n) {
  ll k = 10 / 0;
  if (n == 0)
    return 1;
  return f(n % bitset<20>(n).count()) + 1;
}

// modの値の確認をすること
int main() {
  ll n, c = 0;
  string s;
  cin >> n >> s;
  rep0(i, n) if (s[i] == '1')++ c;
  ll k1 = 0, k2 = 0, r1 = 1, r2 = 1;
  rrep0(i, n) {
    if (s[i] == '1') {
      k1 += r1;
      k1 %= (c - 1);
      k2 += r2;
      k2 %= (c + 1);
    }
    r1 = 2 * r1 % (c - 1);
    r2 = 2 * r2 % (c + 1);
  }
  r1 = 1;
  r2 = 1;
  vector<ll> ans(n);
  rrep0(i, n) {
    if (s[i] == '1') {
      k1 -= r1;
      k1 += (c - 1) * 2;
      k1 %= (c - 1);

      ans[i] = f(k1);

      k1 += r1;
    } else {
      k2 += r2;
      k2 %= (c + 1);

      ans[i] = f(k2);

      k2 -= r2;
      k2 += (c + 1) * 2;
      k2 %= (c + 1);
    }
    r1 = 2 * r1 % (c - 1);
    r2 = 2 * r2 % (c + 1);
  }
  rep0(i, n) pf(ans[i]);
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

#define mx 200005
#define ll long long

char ch[mx];
ll besi[mx], kom[mx];
ll csum[mx], csum1[mx];
int n, m, k, ii;
ll mod;

ll bigmod(ll e, ll x) {
  if (!x)
    return 1;
  ll p = bigmod(e, x / 2);
  p = (p * p) % mod;
  if (x % 2)
    p = (p * e) % mod;
  return p;
}

void func(ll val) {
  int re = 1;
  while (val) {
    int cnt = __builtin_popcount(val);
    re++;
    val %= cnt;
  }
  printf("%d\\n", re);
}

void solve() {
  scanf("%d%s", &n, ch);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (ch[i] == '1')
      cnt++;
  }
  for (int i = 0; i < n; i++) {
    if (cnt > 1) {
      mod = cnt - 1;
      if (ch[i] == '1')
        kom[i] = bigmod(2, n - i - 1);
    }

    mod = cnt + 1;
    if (ch[i] == '1')
      besi[i] = bigmod(2, n - i - 1);
  }
  if (cnt > 1)
    for (int i = 0; i < n; i++) {
      csum[i + 1] = (csum[i] + kom[i]) % (cnt - 1);
    }

  if (cnt != 0)
    for (int i = 0; i < n; i++) {
      csum1[i + 1] = (csum1[i] + besi[i]) % (cnt + 1);
    }

  for (int i = 0; i < n; i++) {
    if (ch[i] == '1') {
      if (cnt == 1) {
        printf("0\\n");
        continue;
      }
      mod = cnt - 1;
      ll val = csum[n] - bigmod(2, n - i - 1);
      val = (val + mod) % mod;
      func(val);
    } else {
      mod = cnt + 1;
      ll val = csum1[n] + bigmod(2, n - i - 1);
      val %= mod;
      func(val);
    }
  }
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

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

#define mx 200005
#define ll long long

char ch[mx];
ll besi[mx], kom[mx];
ll csum[mx], csum1[mx];
int n, m, k, ii;
ll mod;

ll bigmod(ll e, ll x) {
  if (!x)
    return 1;
  ll p = bigmod(e, x / 2);
  p = (p * p) % mod;
  if (x % 2)
    p = (p * e) % mod;
  return p;
}

void func(ll val) {
  int re = 1;
  while (val) {
    int cnt = __builtin_popcount(val);
    re++;
    val %= cnt;
  }
  printf("%d\\n", re);
}

void solve() {
  scanf("%d%s", &n, ch);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (ch[i] == '1')
      cnt++;
  }
  for (int i = 0; i < n; i++) {
    if (cnt > 1) {
      mod = cnt - 1;
      if (ch[i] == '1')
        kom[i] = bigmod(2, n - i - 1);
    }

    mod = cnt + 1;
    if (ch[i] == '1')
      besi[i] = bigmod(2, n - i - 1);
  }

  for (int i = 0; i < n; i++) {
    csum[i + 1] = (csum[i] + kom[i]) % (cnt - 1);
  }

  for (int i = 0; i < n; i++) {
    csum1[i + 1] = (csum1[i] + besi[i]) % (cnt + 1);
  }

  for (int i = 0; i < n; i++) {
    if (ch[i] == '1') {
      if (cnt == 1) {
        printf("1\\n");
        continue;
      }
      mod = cnt - 1;
      ll val = csum[n] - bigmod(2, n - i - 1);
      val = (val + mod) % mod;
      func(val);
    } else {
      mod = cnt + 1;
      ll val = csum1[n] + bigmod(2, n - i - 1);
      val %= mod;
      func(val);
    }
  }
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    solve();
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;

  int cnt = 0;
  for (auto c : s) {
    cnt += (c == '1');
  }

  // special
  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << '\\n';
    }
    return 0;
  }
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << '\\n';
      else {
        if (s[n - 1] == '1' || i == n - 1)
          cout << 2 << '\\n';
        else
          cout << 1 << '\\n';
      }
    }
    return 0;
  }

  auto poly = [&](int mod) {
    vector<int> r(n);
    r[0] = 1;
    for (int i = 1; i < n; i++) {
      r[i] = r[i - 1] * 2 % mod;
    }
    return r;
  };

  auto f = [&](vector<int> &a, int mod) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum = (sum + (s[i] - '0') * a[n - 1 - i]) % mod;
    }
    return sum;
  };

  // one 1 to 0
  auto a0 = poly(cnt - 1);
  int r0 = f(a0, cnt - 1);

  auto a1 = poly(cnt + 1);
  int r1 = f(a1, cnt + 1);

  auto solve = [](int x) {
    int res = 0;
    while (x) {
      int cnt = __builtin_popcount(x);
      x = x % cnt;
      res++;
    }
    return res;
  };

  for (int i = 0; i < n; i++) {
    int sum;
    if (s[i] == '1') {
      sum = (r0 - a0[n - 1 - i]) % (cnt - 1);
      if (sum < 0)
        sum += (cnt - 1);
    } else {
      sum = (r1 + a1[n - 1 - i]) % (cnt + 1);
      if (sum < 0)
        sum += (cnt + 1);
    }
    cout << solve(sum) + 1 << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;

  int cnt = 0;
  for (auto c : s) {
    cnt += (c == '1');
  }

  auto poly = [&](int mod) {
    vector<int> r(n);
    r[0] = 1;
    for (int i = 1; i < n; i++) {
      r[i] = r[i - 1] * 2 % mod;
    }
    return r;
  };

  auto f = [&](vector<int> &a, int mod) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum = (sum + (s[i] - '0') * a[n - 1 - i]) % mod;
    }
    return sum;
  };

  // one 1 to 0
  auto a0 = poly(cnt - 1);
  int r0 = f(a0, cnt - 1);

  auto a1 = poly(cnt + 1);
  int r1 = f(a1, cnt + 1);

  auto solve = [](int x) {
    int res = 0;
    while (x) {
      int cnt = __builtin_popcount(x);
      x = x % cnt;
      res++;
    }
    return res;
  };

  for (int i = 0; i < n; i++) {
    int sum;
    if (s[i] == '1') {
      sum = (r0 - a0[n - 1 - i]) % (cnt - 1);
      if (sum < 0)
        sum += (cnt - 1);
    } else {
      sum = (r1 + a1[n - 1 - i]) % (cnt + 1);
      if (sum < 0)
        sum += (cnt + 1);
    }
    cout << solve(sum) + 1 << '\\n';
  }

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

#include <iostream>
using namespace std;

template <typename T> T power(T x, long long k, long long mod) {
  T ret{1};
  while (k > 0) {
    if (k & 1)
      ret *= x, ret %= mod;
    x *= x, x %= mod;
    k /= 2;
  }
  return ret;
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  int p = 0;
  for (int i = 0; i < N; ++i) {
    p += (X[i] - '0');
  }

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

  int l = 0, m = 0, r = 0;
  for (int i = 0; i < N; ++i) {

    m *= 2, r *= 2;
    int d = X[i] - '0';
    m += d, r += d;
    m %= p, r %= p + 1;
    if (p > 1) {
      l *= 2;
      l += d;
      l %= p - 1;
    }
  }

  for (int i = 0; i < N; ++i) {
    if (X[i] == '0') {
      int t = (r + power<long long>(2, N - i - 1, p + 1)) % (p + 1);
      cout << 1 + f(t) << '\\n';
    } else {
      if (p == 1) {
        cout << 0 << '\\n';
        continue;
      }
      int t = (l - power<long long>(2, N - i - 1, p - 1)) % (p - 1);
      t = (t + p - 1) % (p - 1);
      cout << 1 + f(t) << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

template <typename T> T power(T x, long long k, long long mod) {
  T ret{1};
  while (k > 0) {
    if (k & 1)
      ret *= x, ret %= mod;
    x *= x, x %= mod;
    k /= 2;
  }
  return ret;
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  int p = 0;
  for (int i = 0; i < N; ++i) {
    p += (X[i] - '0');
  }

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

  int l = 0, m = 0, r = 0;
  for (int i = 0; i < N; ++i) {
    l *= 2, m *= 2, r *= 2;
    int d = X[i] - '0';
    l += d, m += d, r += d;
    l %= p - 1, m %= p, r %= p + 1;
  }

  for (int i = 0; i < N; ++i) {
    if (X[i] == '0') {
      int t = (r + power<long long>(2, N - i - 1, p + 1)) % (p + 1);
      cout << 1 + f(t) << '\\n';
    } else {
      if (p == 1) {
        cout << 0 << '\\n';
        return 0;
      }
      int t = (l - power<long long>(2, N - i - 1, p - 1)) % (p - 1);
      t = (t + p - 1) % (p - 1);
      cout << 1 + f(t) << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int exp(int a, int b, int mod) {
  long long res{1}, z{a};
  while (b) {
    if (b & 1)
      res = (res * z) % mod;
    z = (z * z) % mod;
    b /= 2;
  }
  return res;
}

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

  int n;
  string s;
  cin >> n >> s;
  int z = count(s.begin(), s.end(), '1');
  int l{}, g{};
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (z != 1)
        l = (l + exp(2, n - i - 1, z - 1)) % (z - 1);
      g = (g + exp(2, n - i - 1, z + 1)) % (z + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    int k{}, ans{1};
    if (s[i] == '0') {
      k = (0ll + g + exp(2, n - i - 1, z + 1)) % (z + 1);
    } else {
      if (z != 1)
        k = (0ll + l - exp(2, n - i - 1, z - 1) + z - 1) % (z - 1);
      else
        ans = 0;
    }
    while (k) {
      k %= __builtin_popcount(k);
      ans++;
    }
    cout << ans << '\\n';
  }
}

Error: 0

Fixed submission:

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

int exp(int a, int b, int mod) {
  long long res{1}, z{a};
  while (b) {
    if (b & 1)
      res = (res * z) % mod;
    z = (z * z) % mod;
    b /= 2;
  }
  return res;
}

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

  int n;
  string s;
  cin >> n >> s;
  int z = count(s.begin(), s.end(), '1');
  int l{}, g{};
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (z > 1)
        l = (l + exp(2, n - i - 1, z - 1)) % (z - 1);
      g = (g + exp(2, n - i - 1, z + 1)) % (z + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    int k, ans{1};
    if (s[i] == '0') {
      k = (0ll + g + exp(2, n - i - 1, z + 1)) % (z + 1);
    } else {
      k = (0ll + l - exp(2, n - i - 1, z - 1) + z - 1) % (z - 1);
    }
    while (k > 0) {
      k %= __builtin_popcount(k);
      ans++;
    }
    cout << ans << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define power 210000
int rm[power] = {}, rp[power] = {};
void Initialize(int c) {
  rm[0] = 1;
  rp[0] = 1;
  for (int i = 1; i < power; i++) {
    rm[i] = (rm[i - 1] * 2) % (c - 1);
    rp[i] = (rp[i - 1] * 2) % (c + 1);
  }
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int c = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  if (c == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  if (c == 1) {
    if (s[n - 1] == '1') {
      for (int i = 0; i < n - 1; i++) {
        cout << 2 << endl;
      }
      cout << 0 << endl;
    } else {
      for (int i = 0; i < n - 1; i++) {
        if (s[i] == '1') {
          cout << 0 << endl;
          continue;
        }
        cout << 1 << endl;
      }
      cout << 2 << endl;
    }
    return 0;
  }
  Initialize(c);
  int val_minus = 0, val_plus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      val_minus += 1;
      val_plus += 1;
    }
    val_minus %= c - 1;
    val_plus %= c + 1;
    if (i == n - 1)
      break;
    val_minus *= 2;
    val_plus *= 2;
  }
  int ans[power] = {};
  for (int i = 1; i < power; i++) {
    int count = 0;
    for (int j = 0; j < 20; j++) {
      if (i & (1 << j))
        count++;
    }
    ans[i] = ans[i % count] + 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      int rest = val_minus - rm[n - 1 - i];
      rest += c - 1;
      rest %= c - 1;
      cout << ans[rest] + 1 << endl;
    } else {
      int rest = val_plus + rp[n - 1 - i];
      rest %= c + 1;
      cout << ans[rest] + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define power 210000
int rm[power] = {}, rp[power] = {};
void Initialize(int c) {
  rm[0] = 1;
  rp[0] = 1;
  for (int i = 1; i < power; i++) {
    rm[i] = (rm[i - 1] * 2) % (c - 1);
    rp[i] = (rp[i - 1] * 2) % (c + 1);
  }
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int c = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  Initialize(c);
  int val_minus = 0, val_plus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      val_minus += 1;
      val_plus += 1;
    }
    val_minus %= c - 1;
    val_plus %= c + 1;
    if (i == n - 1)
      break;
    val_minus *= 2;
    val_plus *= 2;
  }
  int ans[power] = {};
  for (int i = 1; i < power; i++) {
    int count = 0;
    for (int j = 0; j < 20; j++) {
      if (i & (1 << j))
        count++;
    }
    ans[i] = ans[i % count] + 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      int rest = val_minus - rm[n - 1 - i];
      rest += c - 1;
      rest %= c - 1;
      cout << ans[rest] + 1 << endl;
    } else {
      int rest = val_plus + rp[n - 1 - i];
      rest %= c + 1;
      cout << ans[rest] + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define NMAX 200005

using namespace std;
typedef long long ll;
typedef long double ld;
ll binPow(ll num, ll p, ll mod) {
  if (p == 0)
    return 1;
  ll aux = binPow(num, p / 2, mod);
  return aux * aux % mod * ((p & 1) ? num : 1) % mod;
}
ll solve(ll x) {
  ll iter = 0;
  while (x > 0) {
    iter++;
    x %= __builtin_popcount(x);
  }
  return iter;
}
int main() {
  ll n, cnt = 0, num1 = 0, num2 = 0;
  string s;
  cin >> n >> s;
  for (ll i = 0; i < n; i++)
    cnt += (s[i] == '1');
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt > 1)
        num1 = (num1 + binPow(2, n - i - 1, cnt - 1)) % (cnt - 1);
      num2 = (num2 + binPow(2, n - i - 1, cnt + 1)) % (cnt + 1);
    }
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      cout << solve((num2 + binPow(2, n - i - 1, cnt + 1)) % (cnt + 1)) + 1
           << '\\n';
    } else {
      if (cnt > 1)
        cout << solve((num1 - binPow(2, n - i - 1, cnt - 1) + cnt - 1) %
                      (cnt - 1)) +
                    1
             << '\\n';
      else
        cout << "0\\n";
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define NMAX 200005

using namespace std;
typedef long long ll;
typedef long double ld;
ll binPow(ll num, ll p, ll mod) {
  if (p == 0)
    return 1;
  ll aux = binPow(num, p / 2, mod);
  return aux * aux % mod * (p & 1 ? num : 1) % mod;
}
ll solve(ll x) {
  ll iter = 0;
  while (x) {
    iter++;
    x %= __builtin_popcount(x);
  }
  return iter;
}
int main() {
  ll n, cnt = 0, num1 = 0, num2 = 0;
  string s;
  cin >> n >> s;
  for (ll i = 0; i < n; i++)
    cnt += (s[i] == '1');
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      num1 = (num1 + binPow(2, n - i - 1, cnt - 1)) % (cnt - 1);
      num2 = (num2 + binPow(2, n - i - 1, cnt + 1)) % (cnt + 1);
    }
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      cout << solve((num2 + binPow(2, n - i - 1, cnt + 1)) % (cnt + 1)) + 1
           << '\\n';
    } else {
      cout << solve((num1 - binPow(2, n - i - 1, cnt - 1) + cnt - 1) %
                    (cnt - 1)) +
                  1
           << '\\n';
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>

using namespace std;

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

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(20) << std::fixed;

  int N;
  string X;
  cin >> N >> X;

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (X[i] == '1');
  }

  long long A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (cnt - 1 > 0) {
        A = (A + mod_pow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      }
      B = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    long long ans = 0, t = 0;
    if (X[i] == '1') {
      if (cnt != 1) {
        t = ((A - mod_pow(2, N - i - 1, cnt - 1)) + (cnt - 1)) % (cnt - 1);
        ans++;
      }
    } else {
      t = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      ans++;
    }
    while (t != 0LL) {
      t %= __builtin_popcountll(t);
      ans++;
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>

using namespace std;

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

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(20) << std::fixed;

  int N;
  string X;
  cin >> N >> X;

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (X[i] == '1');
  }

  long long A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (cnt - 1 >= 0) {
        A = (A + mod_pow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      }
      B = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    long long ans = 0, t = 0;
    if (X[i] == '1') {
      if (cnt != 1) {
        t = ((A - mod_pow(2, N - i - 1, cnt - 1)) + (cnt - 1)) % (cnt - 1);
        ans++;
      }
    } else {
      t = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      ans++;
    }
    while (t != 0LL) {
      t %= __builtin_popcountll(t);
      ans++;
    }
    cout << ans << "\\n";
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define endl '\\n'

ll powmod(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

int n;
string s;
void test() {
  cin >> n >> s;
  int pcnt = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      ++pcnt;
  }
  int s1 = 0, s2 = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      s1 = (s1 + (int)powmod(2, n - i - 1, pcnt + 1)) % (pcnt + 1);
      if (pcnt > 1) {
        s2 = (s2 + (int)powmod(2, n - i - 1, pcnt - 1)) % (pcnt - 1);
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    int ans = 1, si;
    if (s[i] == '0') {
      si = (s1 + (int)powmod(2, n - i - 1, pcnt + 1)) % (pcnt + 1);
    } else if (pcnt == 1) {
      cout << 0 << endl;
      continue;
    } else {
      si = (s2 - (int)powmod(2, n - i - 1, pcnt - 1) + pcnt - 1) % (pcnt - 1);
    }
    // cout << si << ' ';
    while (si) {
      si = si % __builtin_popcount(si);
      ++ans;
    }
    cout << ans << endl;
  }
}

int main() {
  IOS;

  // int t; cin>>t;
  // while (t--) {
  //     test();
  // }

  // int t; cin>>t;
  // for (int i=1; i<=t; ++i) {
  //     cout<<"Case #"<<i<<": ";
  //     test();
  // }

  test();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define endl '\\n'

ll powmod(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

int n;
string s;
void test() {
  cin >> n >> s;
  int pcnt = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      ++pcnt;
  }
  int s1 = 0, s2 = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      s1 = (s1 + (int)powmod(2, n - i - 1, pcnt + 1)) % (pcnt + 1);
      s2 = (s2 + (int)powmod(2, n - i - 1, pcnt - 1)) % (pcnt - 1);
    }
  }
  for (int i = 0; i < n; ++i) {
    int ans = 1, si;
    if (s[i] == '0') {
      si = (s1 + (int)powmod(2, n - i - 1, pcnt + 1)) % (pcnt + 1);
    } else {
      si = (s2 - (int)powmod(2, n - i - 1, pcnt - 1) + pcnt - 1) % (pcnt - 1);
    }
    // cout << si << ' ';
    while (si) {
      si = si % __builtin_popcount(si);
      ++ans;
    }
    cout << ans << endl;
  }
}

int main() {
  IOS;

  // int t; cin>>t;
  // while (t--) {
  //     test();
  // }

  // int t; cin>>t;
  // for (int i=1; i<=t; ++i) {
  //     cout<<"Case #"<<i<<": ";
  //     test();
  // }

  test();
  return 0;
}