alexjercan/bugnet huggingface dataset:
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a); i > (b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (31 - __builtin_clz(x))
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i)&1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 19;

int T[maxN];
char S[maxN];
int res[maxN];

int f(int n, int m) {
  if (m == 1)
    return 0;
  int ret = 0, p = 1;
  FORD(i, n - 1, -1) {
    int b = S[i] - '0';
    ret = (ret + p * b) % m;
    p = p * 2 % m;
  }
  return ret;
}

void solve() {
  FOR(i, 1, maxN)
  T[i] = T[i % PPC(i)] + 1;
  int n;
  scanf("%d %s", &n, S);
  int b = count(S, S + n, '1');

  int r1 = b < 2 ? -1 : f(n, b - 1);
  int r2 = f(n, b + 1);
  int p1 = 1, p2 = 1;

  FORD(i, n - 1, -1) {
    if (S[i] == '0') {
      res[i] = (r2 + p2) % (b + 1);
      res[i] = T[res[i]] + 1;
    }

    else {
      if (b == 1) {
        res[i] = 0;
        continue;
      }

      res[i] = (r1 - p1 + (b - 1)) % (b - 1);
      res[i] = T[res[i]] + 1;
    }

    p1 = b == 1 ? 0 : p1 * 2 % (b - 1);
    p2 = p2 * 2 % (b + 1);
  }

  FOR(i, 0, n)
  printf("%d\\n", res[i]);
}

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


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a); i > (b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (31 - __builtin_clz(x))
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i)&1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 19;

int T[maxN];
char S[maxN];

int f(int n, int m) {
  if (m == 1)
    return 0;
  int ret = 0, p = 1;
  FORD(i, n - 1, -1) {
    int b = S[i] - '0';
    ret = (ret + p * b) % m;
    p = p * 2 % m;
  }
  return ret;
}

void solve() {
  FOR(i, 1, maxN)
  T[i] = T[i % PPC(i)] + 1;
  int n;
  scanf("%d %s", &n, S);
  int b = count(S, S + n, '1');

  FOR(i, 0, n) {
    if (S[i] == '0')
      b++, S[i] = '1';
    else
      b--, S[i] = '0';

    int w = b == 0 ? 0 : T[f(n, b)] + 1;

    printf("%d\\n", w);

    if (S[i] == '0')
      b++, S[i] = '1';
    else
      b--, S[i] = '0';
  }
}

int main() {
  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;

long long modpow(long long n, long long k, long long MOD) {
  long long ans = 1;
  n %= MOD;
  while (k > 0) {
    if (k & 1)
      ans = (ans * n) % MOD;
    k >>= 1;
    n = (n * n) % MOD;
  }
  return ans;
}

int rem(int n, string s, int pp) {
  if (pp <= 0)
    return -1;

  reverse(s.begin(), s.end());

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ans = (ans + modpow(2, i, pp)) % pp;
  }
  return ans;
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  string s;
  cin >> n >> s;
  int pp = 0;
  for (char c : s) {
    pp += (c == '1');
  }

  int loss = rem(n, s, pp - 1);
  int gain = rem(n, s, pp + 1);

  for (int i = 0, j = n - 1; i < n; i++, j--) {
    int ans = 1, cur = 0;
    if (s[i] == '0') {
      // gain a bit
      cur = gain;
      cur = (cur + modpow(2, j, pp + 1)) % (pp + 1);
    } else {
      // lose a bit
      if (pp == 1) {
        ans = 0;
        cur = 0;
      } else {
        cur = loss;
        cur = (cur - modpow(2, j, pp - 1)) % (pp - 1);
        if (cur < 0)
          cur += (pp - 1);
      }
    }

    while (cur > 0) {
      ans++;
      cur %= __builtin_popcount(cur);
    }
    cout << ans << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

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

long long modpow(long long n, long long k, long long MOD) {
  long long ans = 1;
  n %= MOD;
  while (k > 0) {
    if (k & 1)
      ans = (ans * n) % MOD;
    k >>= 1;
    n = (n * n) % MOD;
  }
  return ans;
}

int rem(int n, string s, int pp) {
  if (pp <= 0)
    return -1;

  reverse(s.begin(), s.end());

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ans = (ans + modpow(2, i, pp)) % pp;
  }
  return ans;
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  string s;
  cin >> n >> s;
  int pp = 0;
  for (char c : s) {
    pp += (c == '1');
  }

  int loss = rem(n, s, pp - 1);
  int gain = rem(n, s, pp + 1);

  for (int i = 0, j = n - 1; i < n; i++, j--) {
    int ans = 1, cur = 0;
    if (s[i] == '0') {
      // gain a bit
      cur = gain;
      cur = (cur + modpow(2, j, pp + 1)) % (pp + 1);
    } else {
      // lose a bit
      cur = loss;
      cur = (cur - modpow(2, j, pp - 1)) % (pp - 1);
      if (cur < 0)
        cur += (pp - 1);
    }

    while (cur > 0) {
      ans++;
      cur %= __builtin_popcount(cur);
    }
    cout << ans << '\\n';
  }

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

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

using ll = long long;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 1;

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

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bits = 0;
  for (char &c : s) {
    bits += (c == '1');
  }
  ll tmp1 = 0, tmp2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (bits != 1)
      tmp1 = (tmp1 + (binpow(2, n - i - 1, bits - 1) * (s[i] == '1'))) %
             (bits - 1);
    tmp2 =
        (tmp2 + (binpow(2, n - i - 1, bits + 1) * (s[i] == '1'))) % (bits + 1);
  }
  int org1 = tmp1;
  int org2 = tmp2;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (bits != 1) {
        tmp1 = (org1 - binpow(2, n - i - 1, bits - 1) + 2 * (bits - 1)) %
               (bits - 1);
        cout << 1 + f(tmp1) << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      tmp2 = (org2 + binpow(2, n - i - 1, bits + 1)) % (bits + 1);
      cout << 1 + f(tmp2) << endl;
    }
  }
}

Error: 0

Fixed submission:

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

using ll = long long;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 1;

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

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bits = 0;
  for (char &c : s) {
    bits += (c == '1');
  }
  ll tmp1 = 0, tmp2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    tmp1 =
        (tmp1 + (binpow(2, n - i - 1, bits - 1) * (s[i] == '1'))) % (bits - 1);
    tmp2 =
        (tmp2 + (binpow(2, n - i - 1, bits + 1) * (s[i] == '1'))) % (bits + 1);
  }
  int org1 = tmp1;
  int org2 = tmp2;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      tmp1 =
          (org1 - binpow(2, n - i - 1, bits - 1) + 2 * (bits - 1)) % (bits - 1);
      cout << 1 + f(tmp1) << endl;
    } else {
      tmp2 = (org2 + binpow(2, n - i - 1, bits + 1)) % (bits + 1);
      cout << 1 + f(tmp2) << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N;
string X;
ll g[400001];
ll f[400001];

ll calc(ll m) {
  if (m == 0)
    return 0;
  ll ret = 0;
  for (auto &x : X) {
    ret *= 2;
    ret += x - '0';
    ret %= m;
  }
  return ret;
}

int main() {
  cin >> N;
  cin >> X;
  vector<ll> ans;
  for (int i = 1; i <= 400000; i++) {
    bitset<32> bt(i);
    g[i] = i % bt.count();
  }
  for (int i = 1; i <= 400000; i++) {
    if (g[i] == 0) {
      f[i] = 1;
      continue;
    }
    f[i] = f[g[i]] + 1;
  }
  bitset<200000> btX(X);
  ll x_count = btX.count();
  ll mod_p = x_count + 1;
  ll mod_m = x_count - 1;
  ll X_mod_p = calc(mod_p);
  ll X_mod_m = calc(mod_m);
  ll pp, mp;
  pp = mp = 1;
  for (int i = N - 1; i >= 0; i--) {
    pp %= mod_p;
    if (mod_m != 0)
      mp %= mod_m;
    if (X[i] == '0') {
      ll t = pp + X_mod_p;
      t %= mod_p;
      ans.push_back(f[t] + 1);
    } else {
      if (mod_m == 0) {
        ans.push_back(0);
        continue;
      }
      ll t = X_mod_m - mp;
      t += mod_m;
      t %= mod_m;
      ans.push_back(f[t] + 1);
    }
    pp *= 2;
    mp *= 2;
  }
  for (int i = N - 1; i >= 0; i--)
    cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N;
string X;
ll g[400001];
ll f[400001];

ll calc(ll m) {
  if (m == 0)
    return 0;
  ll ret = 0;
  for (auto &x : X) {
    ret *= 2;
    ret += x - '0';
    ret %= m;
  }
  return ret;
}

int main() {
  cin >> N;
  cin >> X;
  vector<ll> ans;
  for (int i = 1; i <= 400000; i++) {
    bitset<32> bt(i);
    g[i] = i % bt.count();
  }
  for (int i = 1; i <= 400000; i++) {
    if (g[i] == 0) {
      f[i] = 1;
      continue;
    }
    f[i] = f[g[i]] + 1;
  }
  bitset<200000> btX(X);
  ll x_count = btX.count();
  ll mod_p = x_count + 1;
  ll mod_m = x_count - 1;
  ll X_mod_p = calc(mod_p);
  ll X_mod_m = calc(mod_m);
  ll pp, mp;
  pp = mp = 1;
  for (int i = N - 1; i >= 0; i--) {
    pp %= mod_p;
    mp %= mod_m;
    if (X[i] == '0') {
      ll t = pp + X_mod_p;
      t %= mod_p;
      ans.push_back(f[t] + 1);
    } else {
      if (mod_m == 0) {
        ans.push_back(0);
        continue;
      }
      ll t = X_mod_m - mp;
      t += mod_m;
      t %= mod_m;
      ans.push_back(f[t] + 1);
    }
    pp *= 2;
    mp *= 2;
  }
  for (int i = N - 1; i >= 0; i--)
    cout << ans[i] << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define mp make_pair
#define pb push_back
#define f first
#define s second

// var
int c1[200005], n, ar[200005], p2[200005][2], t1c = 0, vl[2];
char cc;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    int tmp = i;
    while (tmp) {
      if (tmp & 1)
        c1[i]++;
      tmp >>= 1;
      // cout << c1[i] << endl;
    }
    cin >> cc;
    if (cc == '1') {
      ar[i] = 1;
      t1c++;
    }
  }

  p2[0][0] = 1;
  p2[0][1] = 1;
  for (int i = 1; i <= n; i++) {
    p2[i][0] = p2[i - 1][0];
    p2[i][1] = p2[i - 1][1];
    if ((t1c + 1) != 0) {
      p2[i][0] *= 2;
      p2[i][0] %= (t1c + 1);
    }
    if ((t1c - 1) != 0) {
      p2[i][1] *= 2;
      p2[i][1] %= (t1c - 1);
    }
  }

  for (int i = 1; i <= n; i++) {
    if (ar[i]) {
      vl[0] += p2[n - i][0];
      if ((t1c + 1) != 0)
        vl[0] %= (t1c + 1);
      vl[1] += p2[n - i][1];
      if ((t1c - 1) != 0)
        vl[1] %= (t1c - 1);
    }
  }

  for (int i = 1; i <= n; i++) {
    int tmp = vl[ar[i]];
    if (ar[i]) {
      if (t1c == 1) {
        cout << 0 << endl;
        continue;
      }
      tmp -= p2[n - i][1];
    } else {
      if (t1c == 0) {
        cout << 1 << endl;
        continue;
      }
      tmp += p2[n - i][0];
    }
    tmp += (t1c + (ar[i] ? -1 : 1));
    tmp %= (t1c + (ar[i] ? -1 : 1));
    int cnt = 1;
    while (tmp != 0) {
      tmp %= c1[tmp];
      cnt++;
    }
    cout << cnt << endl;
  }

  cout.flush();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define mp make_pair
#define pb push_back
#define f first
#define s second

// var
int c1[200005], n, ar[200005], p2[200005][2], t1c = 0, vl[2];
char cc;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    int tmp = i;
    while (tmp) {
      if (tmp & 1)
        c1[i]++;
      tmp >>= 1;
      // cout << c1[i] << endl;
    }
    cin >> cc;
    if (cc == '1') {
      ar[i] = 1;
      t1c++;
    }
  }

  p2[0][0] = 1;
  p2[0][1] = 1;
  for (int i = 1; i <= n; i++) {
    p2[i][0] = p2[i - 1][0];
    p2[i][1] = p2[i - 1][1];
    p2[i][0] *= 2;
    p2[i][0] %= (t1c + 1);
    p2[i][1] *= 2;
    p2[i][1] %= (t1c - 1);
  }

  for (int i = 1; i <= n; i++) {
    if (ar[i]) {
      vl[0] += p2[n - i][0];
      vl[0] %= (t1c + 1);
      vl[1] += p2[n - i][1];
      vl[1] %= (t1c - 1);
    }
  }

  for (int i = 1; i <= n; i++) {
    int tmp = vl[ar[i]];
    if (ar[i])
      tmp -= p2[n - i][1];
    else
      tmp += p2[n - i][0];
    tmp += (t1c + (ar[i] ? -1 : 1));
    tmp %= (t1c + (ar[i] ? -1 : 1));
    int cnt = 1;
    while (tmp != 0) {
      tmp %= c1[tmp];
      cnt++;
    }
    cout << cnt << endl;
  }

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

#include <iostream>

const int MAX_N = 200000;

int f[MAX_N + 3], g[MAX_N + 3], h[MAX_N + 3];
char s[MAX_N + 3];

inline int modPow(int a, int b, int c) {
  int ret = 1;
  for (; b; b >>= 1, a = static_cast<long long>(a) * a % c)
    if (b & 1)
      ret = static_cast<long long>(ret) * a % c;
  return ret;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  int n, ones = 0;
  std::cin >> n >> (s + 1);

  for (int i = 1; i <= n; ++i)
    ones += s[i] == '1';
  for (int i = 1; i <= ones + 1; ++i)
    f[i] = f[i % __builtin_popcount(i)] + 1;
  if (ones == 1) {
    for (int i = 1; i <= n; ++i) {
      if (s[i] == '1') {
        std::cout << "0\\n";
      } else {
        s[i] = '1';
        std::cout << static_cast<int>(s[n] == '1') + 1 << '\\n';
        s[i] = '0';
      }
    }
    return 0;
  }
  for (int i = n; i; --i) {
    if (s[i] == '1') {
      g[i] = (g[i + 1] + modPow(2, n - i, ones + 1)) % (ones + 1);
      h[i] = (h[i + 1] + modPow(2, n - i, ones - 1)) % (ones - 1);
    } else {
      g[i] = g[i + 1];
      h[i] = h[i + 1];
    }
  }
  int tmp[2]{0, 0};
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1') {
      std::cout << f[(tmp[1] + h[i + 1]) % (ones - 1)] + 1 << '\\n';
    } else {
      std::cout
          << f[(tmp[0] + g[i + 1] + modPow(2, n - i, ones + 1)) % (ones + 1)] +
                 1
          << '\\n';
    }
    if (s[i] == '1') {
      tmp[0] = (tmp[0] + modPow(2, n - i, ones + 1)) % (ones + 1);
      tmp[1] = (tmp[1] + modPow(2, n - i, ones - 1)) % (ones - 1);
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>

const int MAX_N = 200000;

int f[MAX_N + 3], g[MAX_N + 3], h[MAX_N + 3];
char s[MAX_N + 3];

inline int modPow(int a, int b, int c) {
  int ret = 1;
  for (; b; b >>= 1, a = static_cast<long long>(a) * a % c)
    if (b & 1)
      ret = static_cast<long long>(ret) * a % c;
  return ret;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  int n, ones = 0;
  std::cin >> n >> (s + 1);
  if (n == 1) {
    std::cout << static_cast<int>(s[1] == '0');
    return 0;
  }
  for (int i = 1; i <= n; ++i)
    ones += s[i] == '1';

  for (int i = 1; i <= ones + 1; ++i)
    f[i] = f[i % __builtin_popcount(i)] + 1;
  for (int i = n; i; --i) {
    if (s[i] == '1') {
      g[i] = (g[i + 1] + modPow(2, n - i, ones + 1)) % (ones + 1);
      h[i] = (h[i + 1] + modPow(2, n - i, ones - 1)) % (ones - 1);
    } else {
      g[i] = g[i + 1];
      h[i] = h[i + 1];
    }
  }
  int tmp[2]{0, 0};
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1') {
      std::cout << f[(tmp[1] + h[i + 1]) % (ones - 1)] + 1 << '\\n';
    } else {
      std::cout
          << f[(tmp[0] + g[i + 1] + modPow(2, n - i, ones + 1)) % (ones + 1)] +
                 1
          << '\\n';
    }
    if (s[i] == '1') {
      tmp[0] = (tmp[0] + modPow(2, n - i, ones + 1)) % (ones + 1);
      tmp[1] = (tmp[1] + modPow(2, n - i, ones - 1)) % (ones - 1);
    }
  }
}
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;
const int MAX = 200010;
int main() {
  int n;
  string x;
  cin >> n >> x;
  int pc = 0;
  rep(i, n) {
    if ((x[i] - '0'))
      pc++;
  }
  vector<int> base_0(n), base_1(n);

  base_0[n - 1] = 1;
  base_1[n - 1] = 1;
  int val_0 = 0;
  int val_1 = 0;
  for (int i = n - 1; i >= 0; i--) {
    val_0 += (x[i] - '0') * base_0[i];
    val_1 += (x[i] - '0') * base_1[i];
    val_0 %= (pc + 1);
    if (pc != 1)
      val_1 %= (pc - 1);
    if (i != 0) {
      base_0[i - 1] = base_0[i] * 2;
      base_0[i - 1] %= (pc + 1);
      base_1[i - 1] = base_1[i] * 2;
      if (pc != 1)
        base_1[i - 1] %= (pc - 1);
    }
  }
  // 埋め込み
  vector<int> f(MAX, 0);
  f[0] = 0;
  rep(i, 200002) { f[i + 1] = 1 + f[(i + 1) % __builtin_popcount(i + 1)]; }
  // 操作
  rep(i, n) {
    int num;
    if ((x[i] - '0') && pc == 1) {
      cout << 0 << endl;
      continue;
    } else if ((x[i] - '0'))
      num = (val_1 - base_1[i] + (pc - 1)) % (pc - 1);
    else
      num = (val_0 + base_0[i]) % (pc + 1);
    // printf("num:%d\\n", num);
    cout << f[num] + 1 << 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;
const int MAX = 200010;
int main() {
  int n;
  string x;
  cin >> n >> x;
  int pc = 0;
  rep(i, n) {
    if ((x[i] - '0'))
      pc++;
  }
  vector<int> base_0(n), base_1(n);

  base_0[n - 1] = 1;
  base_1[n - 1] = 1;
  int val_0 = 0;
  int val_1 = 0;
  for (int i = n - 1; i >= 0; i--) {
    val_0 += (x[i] - '0') * base_0[i];
    val_1 += (x[i] - '0') * base_1[i];
    val_0 %= (pc + 1);
    if (pc != 1)
      val_1 %= (pc - 1);
    if (i != 0) {
      base_0[i - 1] = base_0[i] * 2;
      base_0[i - 1] %= (pc + 1);
      base_1[i - 1] = base_1[i] * 2;
      base_1[i - 1] %= (pc - 1);
    }
  }
  // 埋め込み
  vector<int> f(MAX, 0);
  f[0] = 0;
  rep(i, 200002) { f[i + 1] = 1 + f[(i + 1) % __builtin_popcount(i + 1)]; }
  // 操作
  rep(i, n) {
    int num;
    if ((x[i] - '0') && pc == 1) {
      cout << 0 << endl;
      continue;
    } else if ((x[i] - '0'))
      num = (val_1 - base_1[i] + (pc - 1)) % (pc - 1);
    else
      num = (val_0 + base_0[i]) % (pc + 1);
    // printf("num:%d\\n", num);
    cout << f[num] + 1 << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\\n"
typedef long long ll;
int find(int n, int mod) {
  int ans = 0;
  while (n > 0) {
    ans += 1;
    int x = setbits(n);
    n %= x;
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1')
      cnt++;
  }
  /// corner case if cnt==0.

  int l = cnt - 1, r = cnt + 1;

  int suml = 0, sumr = 0;
  int resl = 1, resr = 1;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1') {
      suml += resl;
      sumr += resr;
    }
    if (l != 0)
      suml %= l;
    sumr %= r;
    resl *= 2;
    resr *= 2;
    if (l != 0)
      resl %= l;
    resr %= r;
  }
  resl = 1;
  resr = 1;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int ans = 0;
    if (str[i] == '1') {
      int x = suml;
      x -= resl;
      if (l != 0)
        x %= l;
      if (x < 0)
        x += l;
      if (l != 0)
        ans += 1 + find(x, l);
    } else {
      int x = sumr;
      x += resr;
      x %= r;
      ans += 1 + find(x, r);
    }
    resl *= 2;
    resr *= 2;
    if (l != 0)
      resl %= l;
    resr %= r;
    a.pb(ans);
  }
  reverse(a.begin(), a.end());
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\\n"
typedef long long ll;
int find(int n, int mod) {
  int ans = 0;
  while (n > 0) {
    ans += 1;
    int x = setbits(n);
    n %= x;
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1')
      cnt++;
  }
  /// corner case if cnt==0.

  int l = cnt - 1, r = cnt + 1;
  int suml = 0, sumr = 0;
  int resl = 1, resr = 1;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1') {
      suml += resl;
      sumr += resr;
    }
    suml %= l;
    sumr %= r;
    resl *= 2;
    resr *= 2;
    resl %= l;
    resr %= r;
  }
  resl = 1;
  resr = 1;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int ans = 0;
    if (str[i] == '1') {
      int x = suml;
      x -= resl;
      x %= l;
      if (x < 0)
        x += l;
      ans += 1 + find(x, l);
    } else {
      int x = sumr;
      x += resr;
      x %= r;
      ans += 1 + find(x, r);
    }
    resl *= 2;
    resr *= 2;
    resl %= l;
    resr %= r;
    a.pb(ans);
  }
  reverse(a.begin(), a.end());
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << endl;

  return 0;
}