alexjercan/bugnet huggingface dataset:
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];
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 (i == n)
          printf("2\\n");
        else
          printf("1\\n");
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 500;
int a[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];
  }
  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);
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s;
long long br, n, ans, mod, MOD, BR[2][200005], val[2];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      MOD++;
  if (MOD < 2) {
    for (int i = 0; i < n; i++) {
      val[1] *= 2;
      val[1] += s[i] - '0';
      val[1] %= (MOD + 1);
    }
    long long pot2 = 1;
    for (int i = n - 1; i >= 0; i--) {
      BR[1][i] = pot2;
      pot2 *= 2;
      pot2 %= (MOD + 1);
    }
  } else {
    for (int i = 0; i < n; i++) {
      val[1] *= 2;
      val[1] += s[i] - '0';
      val[1] %= (MOD + 1);
    }
    for (int i = 0; i < n; i++) {
      val[0] *= 2;
      val[0] += s[i] - '0';
      val[0] %= (MOD - 1);
    }
    long long pot2 = 1;
    for (int i = n - 1; i >= 0; i--) {
      BR[1][i] = pot2;
      pot2 *= 2;
      pot2 %= (MOD + 1);
    }
    pot2 = 1;
    for (int i = n - 1; i >= 0; i--) {
      BR[0][i] = pot2;
      pot2 *= 2;
      pot2 %= (MOD - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      mod = 1;
    else
      mod = -1;
    if (mod == 1)
      br = val[1] + BR[1][i];
    else
      br = val[0] - BR[0][i];
    mod += MOD;
    if (mod == 0) {
      cout << 0 << endl;
      continue;
    }
    br = (br + 3 * mod) % mod;
    ans = 1;
    //	cout << "br: " << br << "  mod: " << mod << endl;
    while (br) {
      br = br % __builtin_popcount(br);
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s;
long long br, n, ans, mod, MOD, BR[2][200005], val[2];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      MOD++;
  if (MOD < 2) {
    for (int i = 0; i < n; i++) {
      val[1] *= 2;
      val[1] += s[i] - '0';
      val[1] %= (MOD + 1);
    }
    long long pot2 = 1;
    for (int i = n - 1; i >= 0; i--) {
      BR[1][i] = pot2;
      pot2 *= 2;
      pot2 %= (MOD + 1);
    }
  } else {
    for (int i = 0; i < n; i++) {
      val[1] *= 2;
      val[1] += s[i] - '0';
      val[1] %= (MOD + 1);
    }
    for (int i = 0; i < n; i++) {
      val[0] *= 2;
      val[0] += s[i] - '0';
      val[0] %= (MOD - 1);
    }
    long long pot2 = 1;
    for (int i = n - 1; i >= 0; i--) {
      BR[1][i] = pot2;
      pot2 *= 2;
      pot2 %= (MOD + 1);
    }
    pot2 = 1;
    for (int i = n - 1; i >= 0; i--) {
      BR[0][i] = pot2;
      pot2 *= 2;
      pot2 %= (MOD - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      mod = 1;
    else
      mod = -1;
    if (mod == 1)
      br = val[1] + BR[1][i];
    else
      br = val[0] - BR[0][i];
    mod += MOD;
    br = (br + mod) % mod;
    ans = 1;
    //	cout << "br: " << br << "  mod: " << mod << endl;
    if (mod == 0) {
      cout << 0 << endl;
      continue;
    }
    while (br) {
      br = br % __builtin_popcount(br);
      ans++;
    }
    cout << ans << endl;
  }
  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);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;

    int count = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1')
        count++;
    }
    if (count == 0) {
      for (int i = 0; i < s.size(); i++) {
        cout << "1\\n";
      }
      continue;
    }
    int f = count - 1;
    int g = count + 1;
    vector<int> po(s.size());
    vector<int> sm(s.size());
    long long int ans1 = 0, ans2 = 0;
    long long int h = 1;
    long long int k = 1;
    for (int i = s.size() - 1; i >= 0; i--) {
      if (count != 1)
        po[i] = h % f;
      sm[i] = k % g;
      if (count != 1)
        ans1 = ((ans1 % f) + ((s[i] - '0') % f) * (h % f)) % f;
      ans2 = ((ans2 % g) + ((s[i] - '0') % g) * (k % g)) % g;
      if (count != 1)
        h = ((h % f) * (2 % f)) % f;
      k = ((k % g) * (2 % g)) % g;
    }
    for (int i = 0; i < s.size(); i++) {
      long long int ans = 1;
      if (s[i] == '0') {
        int j;
        j = (ans2 + sm[i]) % g;
        while (j > 0) {
          ans++;
          j = j % __builtin_popcount(j);
        }
      } else {
        if (count == 1) {
          cout << "0\\n";
          continue;
        }
        int j = ans1 % f;
        j = ((j - po[i]) % f + f) % f;
        while (j > 0) {
          ans++;
          j = j % __builtin_popcount(j);
        }
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;

    int count = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1')
        count++;
    }
    if (count == 0) {
      for (int i = 0; i < s.size(); i++) {
        cout << "1\\n";
      }
      continue;
    }
    int f = count - 1;
    int g = count + 1;
    vector<int> po(s.size());
    vector<int> sm(s.size());
    long long int ans1 = 0, ans2 = 0;
    long long int h = 1;
    long long int k = 1;
    for (int i = s.size() - 1; i >= 0; i--) {
      po[i] = h % f;
      sm[i] = k % g;
      ans1 = ((ans1 % f) + ((s[i] - '0') % f) * (h % f)) % f;
      ans2 = ((ans2 % g) + ((s[i] - '0') % g) * (k % g)) % g;
      h = ((h % f) * (2 % f)) % f;
      k = ((k % g) * (2 % g)) % g;
    }
    for (int i = 0; i < s.size(); i++) {
      long long int ans = 1;
      if (s[i] == '0') {
        int j;
        j = (ans2 + sm[i]) % g;
        while (j > 0) {
          ans++;
          j = j % __builtin_popcount(j);
        }
      } else {
        int j = ans1 % f;
        j = ((j - po[i]) % f + f) % f;
        while (j > 0) {
          ans++;
          j = j % __builtin_popcount(j);
        }
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX = 200001;

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

  int pop_count_origin = count(x.begin(), x.end(), '1');
  int pop_count_m = pop_count_origin - 1;
  int pop_count_p = pop_count_origin + 1;

  int x_m_origin = 0;
  int x_p_origin = 0;
  rep(i, n) {
    if (pop_count_m != 0)
      x_m_origin = (x_m_origin * 2 + int(x[i] - '0')) % pop_count_m;
    x_p_origin = (x_p_origin * 2 + int(x[i] - '0')) % pop_count_p;
  }

  vector<int> pow_m(MAX, 1);
  vector<int> pow_p(MAX, 1);
  for (int i = 1; i < MAX; i++) {
    if (pop_count_m != 0)
      pow_m[i] = (pow_m[i - 1] * 2) % pop_count_m;
    pow_p[i] = (pow_p[i - 1] * 2) % pop_count_p;
  }

  vector<int> f(MAX);
  vector<int> pop_count(MAX);
  for (int i = 1; i < MAX; i++) {
    pop_count[i] = pop_count[i / 2] + i % 2;
    f[i] = f[i % pop_count[i]] + 1;
  }

  rep(i, n) {
    if (x[i] == '1') {
      if (pop_count_m != 0) {
        int next = (x_m_origin - pow_m[n - 1 - i]) % pop_count_m;
        if (next < 0)
          next += pop_count_m;
        cout << f[next] + 1 << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      int next = (x_p_origin + pow_p[n - 1 - i]) % pop_count_p;
      if (next < 0)
        next += pop_count_p;
      cout << f[next] + 1 << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

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

const int MAX = 200001;

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

  int pop_count_origin = count(x.begin(), x.end(), '1');
  int pop_count_m = pop_count_origin - 1;
  int pop_count_p = pop_count_origin + 1;

  int x_m_origin = 0;
  int x_p_origin = 0;
  rep(i, n) {
    x_m_origin = (x_m_origin * 2 + int(x[i] - '0')) % pop_count_m;
    x_p_origin = (x_p_origin * 2 + int(x[i] - '0')) % pop_count_p;
  }

  vector<int> pow_m(MAX, 1);
  vector<int> pow_p(MAX, 1);
  for (int i = 1; i < MAX; i++) {
    pow_m[i] = (pow_m[i - 1] * 2) % pop_count_m;
    pow_p[i] = (pow_p[i - 1] * 2) % pop_count_p;
  }

  vector<int> f(MAX);
  vector<int> pop_count(MAX);
  for (int i = 1; i < MAX; i++) {
    pop_count[i] = pop_count[i / 2] + i % 2;
    f[i] = f[i % pop_count[i]] + 1;
  }

  rep(i, n) {
    if (x[i] == '1') {
      int next = (x_m_origin - pow_m[n - 1 - i]) % pop_count_m;
      if (next < 0)
        next += pop_count_m;
      cout << f[next] + 1 << endl;
    } else { // x[i] == 1
      int next = (x_p_origin - pow_p[n - 1 - i]) % pop_count_p;
      if (next < 0)
        next += pop_count_p;
      cout << f[next] + 1 << endl;
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  ll mul = x;
  while (n) {
    if (n % 2)
      res = res * mul % mod;
    mul = mul * mul % mod;
    n /= 2;
  }
  return res;
}

ll calc(ll a, ll cnt) {
  if (a == 0)
    return cnt;
  ll c = __builtin_popcount(a);
  return calc(a % c, cnt + 1);
}

ll count(string a, ll q) {
  ll ret = 0;
  int m = a.size();
  rep(i, m) if (a[i] == '1') {
    ret += modpow(2, m - i - 1, q);
    // cout << "mp: " << modpow(2, m-i-1, q) << endl;
  }
  return ret % q;
}

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

  ll t = count(x, p + 1);

  if (p == 1) { // 例外処理
    ll c;
    rep(i, n) {
      if (x[i] == '0') {
        c = t;
        c += modpow(2, n - i - 1, p + 1);
        c %= p + 1;
        cout << calc(c, 1) << endl;
      } else
        cout << 0 << endl;
    }
    return 0;
  }

  ll s = count(x, p - 1);

  rep(i, n) {
    ll c;
    if (x[i] == '1') {
      c = s + p - 1;
      c -= modpow(2, n - i - 1, p - 1);
      c %= p - 1;
    } else {
      c = t;
      c += modpow(2, n - i - 1, p + 1);
      c %= p + 1;
    }
    cout << calc(c, 1) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  ll mul = x;
  while (n) {
    if (n % 2)
      res = res * mul % mod;
    mul = mul * mul % mod;
    n /= 2;
  }
  return res;
}

ll calc(ll a, ll cnt) {
  if (a == 0)
    return cnt;
  ll c = __builtin_popcount(a);
  return calc(a % c, cnt + 1);
}

int n;

ll count(string a, ll q) {
  ll ret = 0;
  int m = a.size();
  rep(i, m) if (a[i] == '1') {
    ret += modpow(2, m - i - 1, q);
    // cout << "mp: " << modpow(2, m-i-1, q) << endl;
  }
  return ret % q;
}

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

  // if (p == 1){// 例外処理
  //     return 0;
  // }

  ll s = count(x, p - 1);
  ll t = count(x, p + 1);

  rep(i, n) {
    ll c;
    if (x[i] == '1') { // p-1のパターン
      c = s + p - 1;
      c -= modpow(2, n - i - 1, p - 1);
      c %= p - 1;
    } else { // p+1のパターン
      c += modpow(2, n - i - 1, p + 1);
      c %= p + 1;
    }
    cout << calc(c, 1) << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int v[2][200010], res[200010];

int f(int n) {
  int res = 0;
  while (n > 0) {
    n %= __builtin_popcount(n);
    ++res;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int d = count(begin(x), end(x), '1');

  for (int i = 0; i < 1 + (d > 1); ++i) {
    v[i][0] = 1;
    for (int j = 0; j < n; ++j)
      v[i][j + 1] = v[i][j] * 2 % (i ? d - 1 : d + 1);
  }

  int r0 = 0, r1 = 0;
  for (auto &c : x)
    r0 = (r0 * 2 + (c - '0')) % (d + 1);
  if (d > 1)
    for (auto &c : x)
      r1 = (r1 * 2 + (c - '0')) % (d - 1);
  reverse(begin(x), end(x));
  for (int i = 0; i < n; ++i) {
    char &c = x[i];
    if (c == '0') {
      int t = (r0 + v[0][i]) % (d + 1);
      res[i] = f(t) + 1;
    } else {
      if (d > 1) {
        int t = (r1 - v[1][i] + d - 1) % (d - 1);
        res[i] = f(t) + 1;
      }
    }
  }
  reverse(res, res + n);
  for (int i = 0; i < n; ++i)
    cout << res[i] << '\\n';
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int v[2][200010], res[200010];

int f(int n) {
  int res = 0;
  while (n > 0) {
    n %= __builtin_popcount(n);
    ++res;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int d = count(begin(x), end(x), '1');

  for (int i = 0; i < 1 + (d > 1); ++i) {
    v[i][0] = 1;
    for (int j = 0; j < n; ++j)
      v[i][j + 1] = v[i][j] * 2 % (i ? d - 1 : d + 1);
  }

  int r0 = 0, r1 = 0;
  for (auto &c : x)
    r0 = (r0 * 2 + (c - '0')) % (d + 1);
  for (auto &c : x)
    r1 = (r1 * 2 + (c - '0')) % (d - 1);
  reverse(begin(x), end(x));
  for (int i = 0; i < n; ++i) {
    char &c = x[i];
    if (c == '0') {
      int t = (r0 + v[0][i]) % (d + 1);
      res[i] = f(t) + 1;
    } else {
      if (d > 1) {
        int t = (r1 - v[1][i] + d - 1) % (d - 1);
        res[i] = f(t) + 1;
      }
    }
  }
  reverse(res, res + n);
  for (int i = 0; i < n; ++i)
    cout << res[i] << '\\n';
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int m[200005];
int f(int n) {
  if (n == 0)
    return 0;
  if (m[n] > 0)
    return m[n];
  return m[n] = f(n % __builtin_popcount(n)) + 1;
}
int main() {
  int n;
  string x;
  cin >> n >> x;
  reverse(x.begin(), x.end());
  int b = 0;
  rep(i, n) if (x[i] == '1')++ b;
  vector<int> v0(n), v1(n);
  int d0 = 1, d1 = 1;
  ll s0 = 0, s1 = 0;
  rep(i, n) {
    v0[i] = d0;
    v1[i] = d1;
    d0 <<= 1;
    d0 %= b + 1;
    if (b != 1)
      d1 <<= 1, d1 %= b - 1;
    if (x[i] == '1')
      s0 += v0[i], s1 += v1[i];
  }
  vector<int> ans(n);
  rep(i, n) {
    if (x[i] == '0') {
      ans[i] = f((s0 + v0[i]) % (b + 1)) + 1;
    } else {
      ans[i] = b == 1 ? 0 : f((s1 - v1[i]) % (b - 1)) + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int m[200005];
int f(int n) {
  if (n == 0)
    return 0;
  if (m[n] > 0)
    return m[n];
  return m[n] = f(n % __builtin_popcount(n)) + 1;
}
int main() {
  int n;
  string x;
  cin >> n >> x;
  reverse(x.begin(), x.end());
  int b = 0;
  rep(i, n) if (x[i] == '1')++ b;
  vector<int> v0(n), v1(n);
  int d0 = 1, d1 = 1;
  ll s0 = 0, s1 = 0;
  rep(i, n) {
    v0[i] = d0;
    v1[i] = d1;
    d0 <<= 1;
    d0 %= b + 1;
    d1 <<= 1;
    d1 %= b - 1;
    if (x[i] == '1')
      s0 += v0[i], s1 += v1[i];
  }
  vector<int> ans(n);
  rep(i, n) {
    if (x[i] == '0') {
      ans[i] = f((s0 + v0[i]) % (b + 1)) + 1;
    } else {
      ans[i] = f((s1 - v1[i]) % (b - 1)) + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans[i] << endl;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int n;
string x;

lli pop_count(lli n) {
  lli ret = 0;
  while (n) {
    if (n & 1)
      ret++;
    n >>= 1;
  }
  return ret;
}

int main(void) {
  cin >> n;
  cin >> x;
  lli m, m1, m2;
  m = 0;
  for (lli i = 0; i < n; i++)
    if (x[i] == '1')
      m++;
  m1 = m - 1;
  m2 = m + 1;
  vector<lli> mod1(n), mod2(n); // 2^i mod m1, m2
  lli b1, b2;
  b1 = b2 = 1;
  for (lli i = n - 1; i >= 0; i--) {
    if (m1 > 0) {
      mod1[i] = b1 % m1;
      b1 <<= 1;
      b1 %= m1;
    }
    mod2[i] = b2 % m2;
    b2 <<= 1;
    b2 %= m2;
  }
  lli x1, x2;
  x1 = x2 = 0;
  for (lli i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (m1 > 0) {
        x1 += mod1[i] % m1;
        x1 %= m1;
      }
      x2 += mod2[i] % m2;
      x2 %= m2;
    }
  }
  for (lli i = 0; i < n; i++) {
    lli xi;
    if (x[i] == '1') {
      if (m1 == 0) {
        cout << 0 << endl;
        continue;
      }
      xi = x1;
      xi -= mod1[i];
      xi %= m1;
      if (xi < 0)
        xi += m1;
    } else {
      xi = x2;
      xi += mod2[i];
      xi %= m2;
    }
    lli res = 1;
    while (xi) {
      xi %= pop_count(xi);
      res++;
    }
    cout << res << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int n;
string x;

int pop_count(int n) {
  int ret = 0;
  while (n) {
    if (n & 1)
      ret++;
    n >>= 1;
  }
  return ret;
}

int main(void) {
  cin >> n;
  cin >> x;
  int m, m1, m2;
  m = 0;
  for (int i = 0; i < n; i++)
    if (x[i] == '1')
      m++;
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  m1 = m - 1;
  m2 = m + 1;
  vector<int> mod1(n), mod2(n); // 2^i mod m1, m2
  int b1, b2;
  b1 = b2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    mod1[i] = b1 % m1;
    mod2[i] = b2 % m2;
    b1 <<= 1;
    b2 <<= 1;
    b1 %= m1;
    b2 %= m2;
  }
  int x1, x2;
  x1 = x2 = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      x1 += mod1[i] % m1;
      x2 += mod2[i] % m2;
      x1 %= m1;
      x2 %= m2;
    }
  }
  for (int i = 0; i < n; i++) {
    int xi;
    if (x[i] == '1') {
      xi = x1;
      xi -= mod1[i];
      if (xi < 0)
        xi += m1;
    } else {
      xi = x2;
      xi += mod2[i];
      xi %= m2;
    }
    int res = 1;
    while (xi) {
      xi %= pop_count(xi);
      res++;
    }
    cout << res << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

int main() {
  int n;
  cin >> n;
  vi f(n + 2), x(n + 1);
  FOR(i, 1, n + 1) { f[i] = f[i % __builtin_popcount(i)] + 1; }
  int pc = 0;
  FOR(i, 1, n) {
    char c;
    cin >> c;
    x[i] = c == '1';
    pc += x[i];
  }
  vi ans(n + 1);
  if (pc > 1) {
    int s1 = 0, s2 = 0, m1 = pc - 1, m2 = pc + 1;
    for (int i = n, j1 = 1, j2 = 1; i > 0;
         i--, j1 = j1 * 2 % m1, j2 = j2 * 2 % m2) {
      if (x[i]) {
        s1 = (s1 + j1) % m1;
        s2 = (s2 + j2) % m2;
      }
    }
    for (int i = n, j1 = 1, j2 = 1; i > 0;
         i--, j1 = j1 * 2 % m1, j2 = j2 * 2 % m2) {
      if (x[i])
        ans[i] = f[(s1 - j1 + m1) % m1] + 1;
      else
        ans[i] = f[(s2 + j2) % m2] + 1;
    }
  } else if (pc == 0)
    FOR(i, 1, n) ans[i] = 1;
  else {
    FOR(i, 1, n) {
      if (x[i])
        ans[i] = 0;
      else if (i == n)
        ans[i] = 2;
      else
        ans[i] = 1;
    }
  }
  FOR(i, 1, n)
  cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

int main() {
  int n;
  cin >> n;
  vi f(n + 2), x(n + 1);
  FOR(i, 1, n + 1) { f[i] = f[i % __builtin_popcount(i)] + 1; }
  int pc = 0;
  FOR(i, 1, n) {
    char c;
    cin >> c;
    x[i] = c == '1';
    pc += x[i];
  }
  vi ans(n + 1);
  int s1 = 0, s2 = 0, m1 = pc - 1, m2 = pc + 1;
  for (int i = n, j1 = 1, j2 = 1; i > 0;
       i--, j1 = j1 * 2 % m1, j2 = j2 * 2 % m2) {
    if (x[i]) {
      s1 = (s1 + j1) % m1;
      s2 = (s2 + j2) % m2;
    }
  }
  for (int i = n, j1 = 1, j2 = 1; i > 0;
       i--, j1 = j1 * 2 % m1, j2 = j2 * 2 % m2) {
    if (x[i])
      ans[i] = f[(s1 - j1 + m1) % m1] + 1;
    else
      ans[i] = f[(s2 + j2) % m2] + 1;
  }
  FOR(i, 1, n)
  cout << ans[i] << endl;
}