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

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

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll ans = 0;
  ll mod = 0, tm = 0, tp = 0;
  for (ll i = N - 1; i >= 0; --i) {
    if (X[i] == '1')
      mod++;
  }
  for (ll i = 0; i < N; ++i) {
    tp = (tp * 2) % (mod + 1);
    if (X[i] == '1') {
      tp = (tp + 1) % (mod + 1);
    }
  }
  if (mod == 1) {
    tm = 0;
  } else {
    for (ll i = 0; i < N; ++i) {
      tm = (tm * 2) % (mod - 1);
      if (X[i] == '1')
        tm = (tm + 1) % (mod - 1);
    }
  }
  //  cout << "mod-1 : " << tm << " ,  mod : " << t << " ,  mod+1 : " << tp <<
  //  endl;
  vector<ll> pm2(N + 1, 0), pp2(N + 1);
  pp2[0] = 1;
  pm2[0] = 1;
  for (int i = 1; i < N + 1; ++i) {
    pp2[i] = (pp2[i - 1] * 2) % (mod + 1);
  }
  if (mod != 1) {
    for (int i = 1; i < N + 1; ++i)
      pm2[i] = (pm2[i - 1] * 2) % (mod - 1);
  }
  for (int i = 0; i < N; i++) {
    ll subt;
    ans = 1;
    if (X[i] == '1') {
      if (mod == 1)
        subt = 0, ans = 0;
      else {
        subt = tm - pm2[N - 1 - i];
        //        cout << "i: " << i << ", " << subt << " " << pm2[N-1-i] <<
        //        endl;
      }
      if (subt < 0)
        subt += (mod - 1);
    } else {
      subt = (tp + pp2[N - 1 - i]) % (mod + 1);
    }
    while (subt != 0) {
      ll a = subt;
      ll pc = 0;
      while (a > 0) {
        pc += (a % 2);
        a /= 2;
      }
      subt %= pc;
      ans++;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll ans = 0;
  ll mod = 0, t = 0, tm = 0, tp = 0;
  for (ll i = N - 1; i >= 0; --i) {
    if (X[i] == '1')
      mod++;
  }
  for (ll i = 0; i < N; ++i) {
    t = (t * 2) % mod;
    tm = (tm * 2) % (mod - 1);
    tp = (tp * 2) % (mod + 1);
    if (X[i] == '1') {
      t = (t + 1) % mod;
      tm = (tm + 1) % (mod - 1);
      tp = (tp + 1) % (mod + 1);
    }
  }
  //  cout << "mod-1 : " << tm << " ,  mod : " << t << " ,  mod+1 : " << tp <<
  //  endl;
  vector<ll> p2(N + 1), pm2(N + 1), pp2(N + 1);
  p2[0] = 1;
  pp2[0] = 1;
  pm2[0] = 1;
  for (int i = 1; i < N + 1; ++i) {
    p2[i] = (p2[i - 1] * 2) % mod;
    pp2[i] = (pp2[i - 1] * 2) % (mod + 1);
    pm2[i] = (pm2[i - 1] * 2) % (mod - 1);
  }
  for (int i = 0; i < N; i++) {
    ll subt;
    ans = 1;
    if (X[i] == '1') {
      subt = tm - pm2[N - 1 - i];
      //      cout << "i: " << i << ", " << subt << " " << pm2[N-1-i] << endl;
      if (subt < 0)
        subt += (mod - 1);
    } else {
      subt = (tp + pp2[N - 1 - i]) % (mod + 1);
    }
    while (subt != 0) {
      ll a = subt;
      ll pc = 0;
      while (a > 0) {
        pc += (a % 2);
        a /= 2;
      }
      subt %= pc;
      ans++;
    }
    cout << ans << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define N 200005
int n, m, i, j, k, a, b, x, y, p, q;
int u[N], v[N];
char s[N];
void f(int k, int n) {
  if (n == 0)
    printf("%d\\n", k);
  else {
    int m = n, c = 0;
    while (m) {
      c += m & 1;
      m >>= 1;
    }
    f(k + 1, n % c);
  }
}
int main() {
  scanf("%d%s", &n, s + 1);
  for (i = 1; i <= n; i++) {
    a += s[i] & 1;
  }
  b = a + 1;
  if (a)
    a--;
  u[n] = v[n] = 1;
  for (p = q = 1, i = n; i >= 1; i--) {
    if (s[i] & 1) {
      if (a)
        x = (x + p) % a;
      y = (y + q) % b;
    }
    if (a)
      u[i - 1] = p = (p + p) % a;
    v[i - 1] = q = (q + q) % b;
  }
  for (i = 1; i <= n; i++) {
    if (s[i] & 1) {
      if (a)
        f(1, (x - u[i] + a) % a);
      else
        printf("0\\n");
    } else
      f(1, (y + v[i]) % b);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#define N 200005
int n, m, i, j, k, a, b, x, y, p, q;
int u[N], v[N];
char s[N];
void f(int k, int n) {
  if (n == 0)
    printf("%d\\n", k);
  else {
    int m = n, c = 0;
    while (m) {
      c += m & 1;
      m >>= 1;
    }
    f(k + 1, n % c);
  }
}
int main() {
  scanf("%d%s", &n, s + 1);
  for (i = 1; i <= n; i++) {
    a += s[i] & 1;
  }
  b = a + 1;
  if (a)
    a--;
  u[n] = v[n] = 1;
  for (p = q = 1, i = n; i >= 1; i--) {
    if (s[i] & 1) {
      x = (x + p) % a;
      y = (y + q) % b;
    }
    u[i - 1] = p = (p + p) % a;
    v[i - 1] = q = (q + q) % b;
  }
  for (i = 1; i <= n; i++) {
    if (s[i] & 1)
      f(1, (x - u[i] + a) % a);
    else
      f(1, (y + v[i]) % b);
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;
const double EPS = 1e-10;
const ll mod = 1000000007;
// const ll mod=998244353;

ll z[200100];

void memo(ll x) {
  ll k = 0;
  bitset<30> b(x);
  ll a = x % b.count();
  k++;
  k += z[a];
  z[x] = k;
}
ll w1[200100] = {}, w2[200100] = {};
ll p1[200100] = {}, p2[200100] = {};

int main(void) {
  fill(z, z + 200100, -1);
  z[0] = 0;
  ll n;
  string s;
  cin >> n >> s;
  ll kosuu = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      kosuu++;
  }
  for (int i = 1; i <= 200010; i++) {
    memo(i);
  }
  ll k1 = kosuu + 1, k2 = kosuu - 1;
  for (int i = 0; i < n; i++) {
    int e = s[i] - '0';
    w1[i + 1] = (w1[i] * 2 + e) % k1;
    if (k2 <= 0)
      continue;
    w2[i + 1] = (w2[i] * 2 + e) % k2;
  }
  p1[0] = (1 % k1);
  if (k2 >= 1)
    p2[0] = (1 % k2);
  for (int i = 1; i < 200010; i++) {
    p1[i] = (p1[i - 1] * 2) % k1;
    if (k2 >= 1)
      p2[i] = (p2[i - 1] * 2) % k2;
  }
  for (int i = 0; i < n; i++) {
    ll ans = 0;
    if (s[i] == '0') {
      ll c = (w1[n] + p1[n - 1 - i]) % k1;
      ans = z[c] + 1;
    }
    if (s[i] == '1') {
      if (k2 == 0) {
        printf("0\\n");
        continue;
      }
      ll c = (w2[n] - p2[n - 1 - i] + k2) % k2;
      ans = z[c] + 1;
    }
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;
const double EPS = 1e-10;
const ll mod = 1000000007;
// const ll mod=998244353;

ll z[200100];

void memo(ll x) {
  ll k = 0;
  bitset<30> b(x);
  ll a = x % b.count();
  k++;
  k += z[a];
  z[x] = k;
}
ll w1[200100] = {}, w2[200100] = {};
ll p1[200100] = {}, p2[200100] = {};

int main(void) {
  fill(z, z + 200100, -1);
  z[0] = 0;
  ll n;
  string s;
  cin >> n >> s;
  ll kosuu = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      kosuu++;
  }
  for (int i = 1; i <= 200010; i++) {
    memo(i);
  }
  ll k1 = kosuu + 1, k2 = kosuu - 1;
  for (int i = 0; i < n; i++) {
    int e = s[i] - '0';
    w1[i + 1] = (w1[i] * 2 + e) % k1;
    if (k2 <= 0)
      continue;
    w2[i + 1] = (w2[i] * 2 + e) % k2;
  }
  p1[0] = (1 % k1);
  if (k2 >= 1)
    p2[0] = (1 % k2);
  for (int i = 1; i < 200010; i++) {
    p1[i] = (p1[i - 1] * 2) % k1;
    if (k2 >= 1)
      p2[i] = (p2[i - 1] * 2) % k2;
  }
  for (int i = 0; i < n; i++) {
    ll ans = 0;
    if (s[i] == '0') {
      ll c = (w1[n] + p1[i]) % k1;
      ans = z[c] + 1;
    }
    if (s[i] == '1') {
      ll c = (w2[n] - p2[i] + k2) % k2;
      ans = z[c] + 1;
      if (kosuu == 1)
        ans = 0;
    }
    printf("%d\\n", ans);
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 262144

char s[N];

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

int ans[N];

int main() {
  int n;
  scanf("%d%s", &n, s);
  int cnt = 0, ca, cb;
  int rma = 0, rmb = 0;
  int ta = 1, tb = 1;
  for (int i = 0; i < n; ++i)
    cnt += s[i] - '0';
  ca = cnt + 1, cb = cnt - 1;
  cb += !cb; // mod 0
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1')
      rma = (rma + ta) % ca, rmb = (rmb + tb) % cb;
    ta = (ta << 1) % ca, tb = (tb << 1) % cb;
  }
  ta = 1, tb = 1;
  for (int i = n - 1; i >= 0; --i) {
    int k, res;
    if (s[i] == '0') {
      k = (rma + ta) % ca;
      res = f(k) + 1;
    } else {
      if (cnt == 1) {
        res = 0;
      } else {
        k = (rmb + cnt - 1 - tb) % cb;
        res = f(k) + 1;
      }
    }
    ans[i] = res;
    ta = (ta << 1) % ca, tb = (tb << 1) % cb;
  }
  for (int i = 0; i < n; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: 0

Fixed submission:

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 262144

char s[N];

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

int ans[N];

int main() {
  int n;
  scanf("%d%s", &n, s);
  int cnt = 0;
  int rma = 0, rmb = 0;
  int ta = 1, tb = 1;
  for (int i = 0; i < n; ++i)
    cnt += s[i] - '0';
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1')
      rma = (rma + ta) % (cnt + 1), rmb = (rmb + tb) % (cnt - 1);
    ta = (ta << 1) % (cnt + 1), tb = (tb << 1) % (cnt - 1);
  }
  ta = 1, tb = 1;
  for (int i = n - 1; i >= 0; --i) {
    int k, res;
    if (s[i] == '0') {
      k = (rma + ta) % (cnt + 1);
      res = f(k) + 1;
    } else {
      if (cnt == 1) {
        res = 0;
      } else {
        k = (rmb + cnt - 1 - tb) % (cnt - 1);
        res = f(k) + 1;
      }
    }
    ans[i] = res;
    ta = (ta << 1) % (cnt + 1), tb = (tb << 1) % (cnt - 1);
  }
  for (int i = 0; i < n; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 2 * 1e5;
const int REP = 32;
int func(string &x, int mod) {
  if (mod <= 0)
    return 0;
  int64_t rem = 0;
  for (auto i = 0; i < (int)x.size(); i++)
    rem <<= 1, rem = (rem + (x[i] - '0')) % mod;
  return rem;
}

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

int main() {
  int n, cnt = 0;
  string x;
  cin >> n >> x;
  for (auto i = 0; i < n; i++)
    if (x[i] == '1')
      cnt++;
  int r1 = func(x, cnt + 1), r2 = func(x, cnt - 1);
  for (auto i = 0; i < n; i++) {
    int r, ans = 1, tmp = cnt;
    if (x[i] == '1') {
      if (tmp != 1)
        r = r2 - modpow(2, n - i - 1, --tmp);
      else
        r = 0, ans = 0;
    } else
      r = r1 + modpow(2, n - i - 1, ++tmp);
    r += tmp, r %= tmp;
    while (r > 0) {
      ans++;
      r %= __builtin_popcount(r);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const int N = 2 * 1e5;
const int REP = 32;
int func(bitset<N> &x, int cnt) {
  if (cnt <= 0)
    return 0;
  int64_t rem = 0;
  for (auto j = N / REP - 1; j >= 0; j--) {
    int64_t a = (((x << (REP * (N / REP - j - 1))) >> (REP * (N / REP - 1)))
                     .to_ullong()) +
                (rem << REP);
    rem = a % cnt;
  }
  return rem;
}

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

int main() {
  int n;
  bitset<N> x;
  cin >> n >> x;

  int r1 = func(x, x.count() + 1), r2 = func(x, x.count() - 1);
  for (auto i = n - 1; i >= 0; i--) {
    int r, ans = 1;
    if (x.test(i)) {
      if (x.count() - 1 <= 0)
        r = 0, ans = 0;
      else {
        r = r2 - modpow(2, i, x.count() - 1);
        if (r < 0)
          r += x.count() - 1;
        if (r >= (int)(x.count() - 1))
          r %= (x.count() - 1);
      }
    } else {
      r = r1 + modpow(2, i, x.count() + 1);
      if (r >= (int)(x.count() + 1))
        r %= (x.count() + 1);
    }
    while (r > 0) {
      ans++;
      r %= __builtin_popcount(r);
    }
    cout << ans << endl;
  }

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

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

int deal(int x) {
  int res = 0;
  while (x) {
    res++;
    x = x % __builtin_popcount(x);
  }
  return res;
}

int n, p1 = -1, p2 = 1, x1, x2;
char s[200005];
int pw1[200005], pw2[200005];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> s + 1;
  for (int i = 1; i <= n; i++) {
    p1 += s[i] == '1';
    p2 += s[i] == '1';
  }
  pw1[0] = pw2[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (p1)
      x1 = (x1 << 1 | (s[i] == '1')) % p1;
    x2 = (x2 << 1 | (s[i] == '1')) % p2;
    if (p1)
      pw1[i] = (pw1[i - 1] << 1) % p1;
    pw2[i] = (pw2[i - 1] << 1) % p2;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (p1)
        cout << deal((x1 - pw1[n - i] + p1) % p1) + 1 << endl;
      else
        cout << 0 << endl;
    } else {
      cout << deal((x2 + pw2[n - i] + p2) % p2) + 1 << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

int deal(int x) {
  int res = 0;
  while (x) {
    res++;
    x = x % __builtin_popcount(x);
  }
  return res;
}

int n, p1 = -1, p2 = 1, x1, x2;
char s[200005];
int pw1[200005], pw2[200005];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> s + 1;
  for (int i = 1; i <= n; i++) {
    p1 += s[i] == '1';
    p2 += s[i] == '1';
  }
  pw1[0] = pw2[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (p1)
      x1 = (x1 << 1 | (s[i] == '1')) % p1;
    x2 = (x2 << 1 | (s[i] == '1')) % p2;
    if (p1)
      pw1[i] = (pw1[i - 1] << 1) % p1;
    pw2[i] = (pw2[i - 1] << 1) % p2;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      cout << deal((x1 - pw1[n - i] + p1) % p1) + 1 << endl;
    } else {
      cout << deal((x2 + pw2[n - i] + p2) % p2) + 1 << endl;
    }
  }

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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

int calc(int x) {
  if (x == 0) {
    return 0;
  }
  // cout << x << endl;
  int a = 0;
  int tmp = x;
  while (tmp > 0) {
    a += tmp & 1;
    tmp >>= 1;
  }

  return calc(x % a) + 1;
}

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

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

  string X;
  cin >> X;
  int a = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      a++;
    }
  }

  int a0 = a + 1;
  int a1 = max(1, a - 1);
  int r0 = 0;
  int r1 = 0;
  vector<int> fact0(N + 1, 1);
  vector<int> fact1(N + 1, 1);
  for (int i = 0; i < N; i++) {
    if (X[N - 1 - i] == '1') {
      r0 = (r0 + fact0[i]) % a0;
      r1 = (r1 + fact1[i]) % a1;
    }
    fact0[i + 1] = 2 * fact0[i] % a0;
    fact1[i + 1] = 2 * fact1[i] % a1;
  }

  // cout << r << endl;
  // cout_vec(fact);

  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      cout << calc((r0 + fact0[N - 1 - i]) % a0) + 1 << '\\n';
    } else {
      if (a == 1) {
        cout << 0 << '\\n';
      } else {
        cout << calc(((r1 - fact1[N - 1 - i]) % a1 + a1) % a1) + 1 << '\\n';
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

int calc(int x) {
  if (x == 0) {
    return 0;
  }
  // cout << x << endl;
  int a = 0;
  int tmp = x;
  while (tmp > 0) {
    a += tmp & 1;
    tmp >>= 1;
  }

  return calc(x % a) + 1;
}

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

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

  string X;
  cin >> X;
  int a = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      a++;
    }
  }

  int r0 = 0;
  int r1 = 0;
  vector<int> fact0(N + 1, 1);
  vector<int> fact1(N + 1, 1);
  for (int i = 0; i < N; i++) {
    if (X[N - 1 - i] == '1') {
      r0 = (r0 + fact0[i]) % (a + 1);
      r1 = (r1 + fact1[i]) % (a - 1);
    }
    fact0[i + 1] = 2 * fact0[i] % (a + 1);
    fact1[i + 1] = 2 * fact1[i] % (a - 1);
  }

  // cout << r << endl;
  // cout_vec(fact);

  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      cout << calc((r0 + fact0[N - 1 - i]) % (a + 1)) + 1 << '\\n';
    } else {
      cout << calc(((r1 - fact1[N - 1 - i]) % (a - 1) + (a - 1)) % (a - 1)) + 1
           << '\\n';
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  string x;
  cin >> x;
  int c1 = 0;
  rep(i, 0, n) {
    if (x[i] == '1')
      c1++;
  }
  ll nxu = 0, nxm = 0;
  ll num_u = 1;
  ll num_m = 1;

  repp(i, n - 1, 0) {
    if (x[i] == '1') {
      nxu = (nxu + num_u) % (c1 + 1);
      if (c1 > 1)
        nxm = (nxm + num_m) % (c1 - 1);
    }
    num_u = num_u * 2 % (c1 + 1);
    if (c1 > 1)
      num_m = num_m * 2 % (c1 - 1);
  }
  num_u = 1, num_m = 1;
  vector<ll> ans(n);
  repp(i, n - 1, 0) {
    ll a;
    if (x[i] == '0')
      a = (nxu + num_u % (c1 + 1) + c1 + 1) % (c1 + 1);
    else {
      if (c1 > 1)
        a = (nxm - num_m % (c1 - 1) + c1 - 1) % (c1 - 1);
      if (c1 == 1) {
        ans[i] = 0;
        continue;
      }
    }
    num_u = num_u * 2 % (c1 + 1);
    if (c1 > 1)
      num_m = num_m * 2 % (c1 - 1);
    ll cnt = 1;
    while (a > 0) {
      ll p = 0, b = a;
      while (b > 0) {
        if (1 & b)
          p++;
        b /= 2;
      }
      cnt++;
      a %= p;
    }
    ans[i] = cnt;
  }
  rep(i, 0, n) { cout << ans[i] << endl; }
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  string x;
  cin >> x;
  int c1 = 0;
  rep(i, 0, n) {
    if (x[i] == '1')
      c1++;
  }
  ll nxu = 0, nxm = 0;
  ll num_u = 1;
  ll num_m = 1;
  repp(i, n - 1, 0) {
    if (x[i] == '1') {
      nxu = (nxu + num_u) % (c1 + 1);
      if (c1 > 0)
        nxm = (nxm + num_m) % (c1 - 1);
    }
    num_u = num_u * 2 % (c1 + 1);
    if (c1 > 0)
      num_m = num_m * 2 % (c1 - 1);
  }
  num_u = 1, num_m = 1;
  vector<ll> ans(n);
  repp(i, n - 1, 0) {
    ll a;
    if (x[i] == '0')
      a = (nxu + num_u % (c1 + 1) + c1 + 1) % (c1 + 1);
    else {
      if (c1 > 0)
        a = (nxm - num_m % (c1 - 1) + c1 - 1) % (c1 - 1);
    }
    num_u = num_u * 2 % (c1 + 1);
    if (c1 > 0)
      num_m = num_m * 2 % (c1 - 1);
    ll cnt = 1;
    while (a > 0) {
      ll p = 0, b = a;
      while (b > 0) {
        if (1 & b)
          p++;
        b /= 2;
      }
      cnt++;
      a %= p;
    }
    ans[i] = cnt;
  }
  rep(i, 0, n) { cout << ans[i] << endl; }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int f[N], ans[N];
char s[N];
int main() {
  int n, sum = 0, s1, s2, sum1 = 0, sum2 = 0, d1 = 1, d2 = 1;
  scanf("%d%s", &n, s);
  for (int i = 0; s[i]; i++)
    sum += (s[i] ^= 0x30);
  s1 = sum - 1, s2 = sum + 1;
  f[0] = 1;
  for (int i = 1; i <= s2; i++)
    f[i] = 1 + f[i % __builtin_popcount(i)];
  if (s1)
    for (int i = n - 1; ~i; i--)
      (sum1 += s[i] * d1) %= s1, (sum2 += s[i] * d2) %= s2, d1 = d1 * 2 % s1,
                                                            d2 = d2 * 2 % s2;
  else
    for (int i = n - 1; ~i; i--)
      (sum2 += s[i] * d2) %= s2, d2 = d2 * 2 % s2;
  d1 = d2 = 1;
  for (int i = n - 1; ~i; i--) {
    if (s[i])
      ans[i] = s1 ? f[(sum1 - d1 + s1) % s1] : 0;
    else
      ans[i] = f[(sum2 + d2) % s2];
    d2 = d2 * 2 % s2;
    if (s1)
      d1 = d1 * 2 % s1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int f[N], ans[N];
char s[N];
int main() {
  int n, sum = 0, s1, s2, sum1 = 0, sum2 = 0, d1 = 1, d2 = 1;
  scanf("%d%s", &n, s);
  for (int i = 0; s[i]; i++)
    sum += (s[i] ^= 0x30);
  s1 = sum - 1, s2 = sum + 1;
  f[0] = 1;
  for (int i = 1; i <= s2; i++)
    f[i] = 1 + f[i % __builtin_popcount(i)];
  if (s1)
    for (int i = n - 1; ~i; i--)
      (sum1 += s[i] * d1) %= s1, (sum2 += s[i] * d2) %= s2, d1 = d1 * 2 % s1,
                                                            d2 = d2 * 2 % s2;
  else
    for (int i = n - 1; ~i; i--)
      (sum2 += s[i] * d2) %= s2, d2 = d2 * 2 % s2;
  d1 = d2 = 1;
  for (int i = n - 1; ~i; i--) {
    if (s[i])
      ans[i] = s1 ? f[(sum1 - d1 + s1) % s1] : 0;
    else
      ans[i] = f[(sum2 + d2) % s2];
    d2 = d2 * 2 % s2, d1 = d1 * 2 % s1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}