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

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

int Ps[200000], Ms[200000];

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

  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  int cntP = cnt + 1, cntM = cnt - 1;
  int modP = 0, modM = 0;
  int twoP = 1, twoM = 1;
  for (int i = n - 1; i >= 0; i--) {
    Ps[i] = twoP % cntP;
    if (cntM > 0)
      Ms[i] = twoM % cntM;
    if (s[i] == '1') {
      modP = (modP + twoP) % cntP;
      if (cntM > 0)
        modM = (modM + twoM) % cntM;
    }
    twoP = (twoP * 2) % cntP;
    if (cntM > 0)
      twoM = (twoM * 2) % cntM;
  }

  rep(i, n) {
    int ans = 1;
    if (s[i] == '1') {
      if (cntM == 0) {
        cout << 0 << endl;
        continue;
      }
      int m = (modM - Ms[i] + cntM) % cntM;
      while (m > 0) {
        int cnt2 = 0;
        rep(j, 18) {
          if (m >> j & 1)
            cnt2++;
        }
        m %= cnt2;
        ans++;
      }
      cout << ans << endl;
    } else {
      int m = (modP + Ps[i]) % cntP;
      while (m > 0) {
        int cnt2 = 0;
        rep(j, 18) {
          if (m >> j & 1)
            cnt2++;
        }
        m %= cnt2;
        ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}
/*
 */


Error: 0

Fixed submission:

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

int Ps[200000], Ms[200000];

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

  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  int cntP = cnt + 1, cntM = cnt - 1;
  int modP = 0, modM = 0;
  int twoP = 1, twoM = 1;
  for (int i = n - 1; i >= 0; i--) {
    Ps[i] = twoP % cntP;
    Ms[i] = twoM % cntM;
    if (s[i] == '1') {
      modP = (modP + twoP) % cntP;
      modM = (modM + twoM) % cntM;
    }
    twoP = (twoP * 2) % cntP;
    twoM = (twoM * 2) % cntM;
  }

  rep(i, n) {
    int ans = 1;
    if (s[i] == '1') {
      int m = (modM - Ms[i] + cntM) % cntM;
      while (m > 0) {
        int cnt2 = 0;
        rep(j, 18) {
          if (m >> j & 1)
            cnt2++;
        }
        m %= cnt2;
        ans++;
      }
      cout << ans << endl;
    } else {
      int m = (modP + Ps[i]) % cntP;
      while (m > 0) {
        int cnt2 = 0;
        rep(j, 18) {
          if (m >> j & 1)
            cnt2++;
        }
        m %= cnt2;
        ans++;
      }
      cout << ans << endl;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
int popcount(string s) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '1')
      ans++;

  // else for(int i=0;i<32;i++)if((1<<i)&n)ans++;
  return ans;
}
long long op(int n) {
  long long ans = 0;
  int t = n;
  while (t != 0) {
    t = t % __builtin_popcount(t);
    ans++;
  }
  return ans;
}
string tc() {
  string s = "";
  for (int i = 0; i < 200000; i++) {
    char c = (rand() % 2) + '0';
    s.push_back(c);
  }
  return s;
}
long long fast_exp(int a, int b, int mod) {
  long long x = a;
  long long y = (b % 2) ? x : 1;
  b >>= 1;
  while (b > 0) {
    x = ((x % mod) * (x % mod)) % mod;
    if (b % 2) {
      y = (y == 1) ? x : ((x % mod) * (y % mod)) % mod;
    }
    b >>= 1;
  }
  return y;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int pc1 = popcount(s);
  int pos_x = 0;
  int neg_x = 0;
  int pos_t = 1;
  int neg_t = 1;
  vector<int> pp(n);
  vector<int> np(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    if (pc1 > 0) {
      pp[n - 1 - i] = pos_t % (pc1 + 1);
      pos_x = (pos_x + (fast_exp(2, n - i - 1, pc1 + 1))) % (pc1 + 1);
    }
    if (pc1 > 1) {
      np[n - 1 - i] = neg_t % (pc1 - 1);
      neg_x = (neg_x + (fast_exp(2, n - i - 1, pc1 - 1))) % (pc1 - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int o = (pos_x + fast_exp(2, n - 1 - i, pc1 + 1)) % (pc1 + 1);
      cout << op(o) + 1 << endl;
    } else if (s[i] == '1' && pc1 > 1) {
      int y = (neg_x + pc1 - 1 - fast_exp(2, n - 1 - i, pc1 - 1)) % (pc1 - 1);
      cout << op(y) + 1 << endl;
    } else
      cout << 0 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int popcount(string s, int n) {
  int ans = 0;
  if (!n) {
    for (int i = 0; i < s.length(); i++)
      if (s[i] == '1')
        ans++;
  } else
    for (int i = 0; i < 32; i++)
      if ((1 << i) & n)
        ans++;
  return ans;
}
vector<int> O(200001);
long long op(int n) {
  if (O[n])
    return O[n];
  long long ans = 0;
  int t = popcount("", n);
  while (t != 0) {
    if (O[t]) {
      ans += O[t];
      O[n] = ans;
      return ans;
    }
    t %= popcount("", t);
    ans++;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int pc1 = popcount(s, 0);
  int pos_x = 0;
  int neg_x = 0;
  int pos_t = 1;
  int neg_t = 1;
  vector<int> pp(n);
  vector<int> np(n);
  for (int i = 0; i < n; i++) {
    pp[n - 1 - i] = pos_t;
    np[n - 1 - i] = neg_t;
    pos_x = (pos_x + (pos_t * (s[n - i - 1] - '0'))) % (pc1 + 1);
    neg_x = (neg_x + (neg_t * (s[n - i - 1] - '0'))) % (pc1 - 1);
    pos_t = (pos_t * 2) % (pc1 + 1);
    neg_t = (neg_t * 2) % (pc1 - 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int o = (pos_x + pp[i]) % (pc1 + 1);
      cout << op(o) + 1 << endl;
    } else {
      int y = neg_x - np[i];
      while (y < 0) {
        y += (pc1 - 1);
      }
      cout << op(y) + 1 << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, A = 0;
  string S;
  cin >> N >> S;
  vector<int> p(N);
  vector<int> q(N);
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '1') {
      A++;
    }
  }
  int Q = A - 1, W = A + 1;
  int X = 0, Y = 0, E = 1, R = 1;
  for (int i = N - 1; i >= 0; i--) {
    p[i] = E;
    q[i] = R;
    if (S[i] == '1') {
      X += p[i];
      Y += q[i];
      if (Q != 0) {
        X %= Q;
      }
      Y %= W;
    }
    E *= 2;
    if (Q != 0) {
      E %= Q;
    }
    R *= 2;
    R %= W;
  }
  for (int i = 0; i < N; i++) {
    int b, Z = 1;
    if (A == 1 && S[i] == '1') {
      cout << "0" << endl;
      continue;
    }
    if (S[i] == '1') {
      b = (X - p[i] + Q) % Q;
    } else {
      b = (Y + q[i]) % W;
    }
    while (b != 0) {
      int D = b, T = 1, P = 0;
      while (D != 0) {
        if (D % (T * 2) != 0) {
          D -= T;
          P++;
        }
        T *= 2;
      }
      b %= P;
      Z++;
    }
    cout << Z << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int N, A = 0;
  string S;
  cin >> N >> S;
  vector<int> p(N);
  vector<int> q(N);
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '1') {
      A++;
    }
  }
  int Q = A - 1, W = A + 1;
  int X = 0, Y = 0, E = 1, R = 1;
  for (int i = N - 1; i >= 0; i--) {
    p[i] = E;
    q[i] = R;
    if (S[i] == '1') {
      X += p[i];
      Y += q[i];
      X %= Q;
      Y %= W;
    }
    E *= 2;
    E %= Q;
    R *= 2;
    R %= W;
  }
  for (int i = 0; i < N; i++) {
    int b, Z = 1;
    if (S[i] == '1') {
      b = (X - p[i] + Q) % Q;
    } else {
      b = (Y + q[i]) % W;
    }
    while (b != 0) {
      int D = b, T = 1, P = 0;
      while (D != 0) {
        if (D % (T * 2) != 0) {
          D -= T;
          P++;
        }
        T *= 2;
      }
      b %= P;
      Z++;
    }
    cout << Z << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error



#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

ll popcount(ll n) {
  ll p = 0;
  for (int i = 0; i < 64; i++) {
    if (((n >> i) & 1) == 1) {
      p++;
    }
  }
  return p;
}

ll func(ll n) {
  int cnt = 0;
  int num = n;
  while (num > 0) {
    num = num % popcount(num);
    cnt++;
  }

  return cnt;
}

int main() {

  int n;
  cin >> n;

  string s;
  cin >> s;

  int pop0 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      pop0++;
    }
  }

  vector<vector<ll>> pow(2, vector<ll>(n + 1));
  pow[0][n] = pow[1][n] = 1;

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

  vector<ll> sum(2);
  for (int i = 0; i < n; i++) {

    if (s[i] == '1') {
      sum[0] = (sum[0] + pow[0][i + 1]) % (pop0 + 1);
      if (pop0 > 1) {
        sum[1] = (sum[1] + pow[1][i + 1]) % (pop0 - 1);
      }
    }
  }

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

    ll su = sum[j];
    ll pop1 = pop0;
    if (j == 0) {
      su += pow[j][i + 1];
      pop1++;
    } else {
      su -= pow[j][i + 1];
      pop1--;
    }
    if (pop1 == 0) {
      cout << 0 << endl;
      continue;
    }

    ll p = (su + pop1) % pop1;

    cout << (func(p) + 1) << endl;
  }
}

Error: 0

Fixed submission:



#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

int popcount(int n) {
  int p = 0;
  for (int i = 0; i < 32; i++) {
    if (((n >> i) & 1) == 1) {
      p++;
    }
  }
  return p;
}

int func(int n) {
  int cnt = 0;
  int num = n;
  while (num > 0) {
    num = num % popcount(num);
    cnt++;
  }

  return cnt;
}

int main() {

  int n;
  cin >> n;

  string s;
  cin >> s;

  int pop0 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      pop0++;
    }
  }

  vector<vector<int>> pow(2, vector<int>(n + 1));
  pow[0][n] = pow[1][n] = 1;

  for (int i = n; i > 0; i--) {
    pow[0][i - 1] = (2 * pow[0][i]) % (pop0 + 1);
    pow[1][i - 1] = (2 * pow[1][i]) % (pop0 - 1);
  }

  vector<int> sum(2);
  for (int i = 0; i < n; i++) {

    if (s[i] == '1') {
      sum[0] = (sum[0] + pow[0][i + 1]) % (pop0 + 1);
      sum[1] = (sum[1] + pow[1][i + 1]) % (pop0 - 1);
    }
  }

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

    int su = sum[j];
    int pop1 = pop0;
    if (j == 0) {
      su += pow[j][i + 1];
      pop1++;
    } else {
      su -= pow[j][i + 1];
      pop1--;
    }

    int p = (su + pop1) % pop1;

    cout << (func(p) + 1) << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <string_view>
#include <vector>

using namespace std;

int ModAtoi(string_view sv, int d) {
  int r = 0;
  for (char c : sv) {
    r *= 2;
    r %= d;
    r += c - '0';
  }
  return r;
}

inline int popcount(int x) { return __builtin_popcount(x); }

int f(int x) { return x == 0 ? 0 : f(x % popcount(x)) + 1; }

int main() {
  int n;
  string s;
  cin >> n >> s;
  int pc0 = 0;
  for (char c : s)
    pc0 += c - '0';
  vector<int> ans(n);
  for (int b : {0, 1}) {
    const int pc = b == 0 ? pc0 + 1 : pc0 - 1;
    if (pc <= 0)
      continue;
    const int r0 = ModAtoi(s, pc); // X0 % popcount(X0)

    int k = 1;
    for (int i = n - 1; i >= 0; --i) {
      const int x = s[i] - '0';
      if (x == b) {
        int r = r0;
        if (b == 0) {
          r = (r + k) % pc;
        } else {
          r = (r - k + pc) % pc;
        }
        ans[i] = f(r) + 1;
      }
      k = (2 * k) % pc;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bitset>
#include <cstdint>
#include <iostream>
#include <map>
#include <string>

using namespace std;

constexpr int M = 2 * 100000;

int main() {
  int N;
  string X;
  cin >> N;
  cin >> X;
  bitset<M> bits(X);
  const size_t count = bits.count();
  map<uint64_t, int> map;
  for (int i = 1; i <= N; i++) {
    size_t cn = count;
    if (bits.test(N - i)) {
      cn--;
    } else {
      cn++;
    }
    bits.flip(N - i);
    uint64_t n = bits.to_ullong();
    bits.flip(N - i);
    auto it = map.find(n);
    if (it != map.end()) {
      cout << it->second << endl;
      continue;
    }
    int f = 0;
    while (n) {
      n = n % cn;
      cn = bitset<M>(n).count();
      f++;
    }
    map[n] = f;
    cout << f << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ppc = pair<int, int>;

int binMod(vector<int> &b, int val) {
  int d = 0;
  int N = b.size();
  for (int i = 0; i < N; i++) {
    d = ((d << 1) + b[i]) % val;
  }
  return d;
}

int mod2pow(int n, int val) {
  int d = 1;
  for (int i = 0; i < n; i++) {
    d = (d << 1) % val;
  }
  return d;
}

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

int main(void) {
  int N;
  string X;
  cin >> N >> X;

  vector<int> b(N);
  int pc = 0;
  for (int i = 0; i < N; i++) {
    b[i] = (X[i] == '0') ? 0 : 1;
    if (b[i] == 1)
      pc++;
  }

  vector<int> mod_up(N, 0), mod_down(N, 0);
  int up = 0, down = 0;
  if (pc < N) {
    up = binMod(b, pc + 1);
  }

  if (pc > 1) {
    down = binMod(b, pc - 1);
  }

  for (int i = 0; i < N; i++) {

    int mod = 0;

    if (b[i] == 0) {
      mod = (up + modpow(2, N - i - 1, pc + 1)) % (pc + 1);
    } else {
      if (pc == 1) {
        cout << 0 << endl;
        continue;
      }
      mod = (down - modpow(2, N - i - 1, pc - 1));
      while (mod < 0)
        mod += pc - 1;
      mod = mod % (pc - 1);
    }

    int cnt = 1;
    while (mod > 0) {
      mod = mod % __builtin_popcount(mod);
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ppc = pair<int, int>;

int binMod(vector<int> &b, int val) {
  int d = 0;
  int N = b.size();
  for (int i = 0; i < N; i++) {
    d = ((d << 1) + b[i]) % val;
  }
  return d;
}

int mod2pow(int n, int val) {
  int d = 1;
  for (int i = 0; i < n; i++) {
    d = (d << 1) % val;
  }
  return d;
}

int main(void) {
  int N;
  string X;
  cin >> N >> X;

  vector<int> b(N);
  int pc = 0;
  for (int i = 0; i < N; i++) {
    b[i] = (X[i] == '0') ? 0 : 1;
    if (b[i] == 1)
      pc++;
  }

  vector<int> mod_up(N, 0), mod_down(N, 0);
  int up = 0, down = 0;
  if (pc < N) {
    up = binMod(b, pc + 1);
  }

  if (pc > 1) {
    down = binMod(b, pc - 1);
  }

  for (int i = 0; i < N; i++) {

    int mod = 0;

    if (b[i] == 0) {
      mod = (up + mod2pow(N - i - 1, pc + 1)) % (pc + 1);
    } else {
      if (pc == 1) {
        cout << 0 << endl;
        continue;
      }
      mod = (down - mod2pow(N - i - 1, pc - 1));
      while (mod < 0)
        mod += pc - 1;
      mod = mod % (pc - 1);
    }

    int cnt = 1;
    while (mod > 0) {
      mod = mod % __builtin_popcount(mod);
      cnt++;
    }
    cout << cnt << endl;
  }

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

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

const int N = 200005;
int n, g, a[2][N], r[2];
string x;

int F(int a) {
  if (a == 0)
    return 0;
  return F(a % (int)__builtin_popcount(a)) + 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> x;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      ++g;
    }
  }
  if (g != 1)
    a[0][n - 1] = 1 % (g - 1);
  a[1][n - 1] = 1 % (g + 1);
  for (int i = n - 2; i >= 0; --i) {
    if (g != 1)
      a[0][i] = a[0][i + 1] * 2 % (g - 1);
    a[1][i] = a[1][i + 1] * 2 % (g + 1);
  }
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (g != 1)
        (r[0] += a[0][i]) %= (g - 1);
      (r[1] += a[1][i]) %= (g + 1);
    }
  }
  for (int i = 0; i < n; ++i) {
    int y = 0;
    if (x[i] == '1') {
      y = r[0] - a[0][i];
      if (y < 0)
        y += g - 1;
    } else {
      y = r[1] + a[1][i];
      if (y >= g + 1)
        y -= g + 1;
    }
    if (x[i] == '1' && g == 1) {
      cout << 0 << "\\n";
    } else {
      cout << F(y) + 1 << "\\n";
    }
  }
  return 0;
}


Error: 0

Fixed submission:

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

const int N = 200005;
int n, g, a[2][N], r[2];
string x;

int F(int a) {
  if (a == 0)
    return 0;
  return F(a % (int)__builtin_popcount(a)) + 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> x;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      ++g;
    }
  }
  a[0][n - 1] = 1 % (g - 1);
  a[1][n - 1] = 1 % (g + 1);
  for (int i = n - 2; i >= 0; --i) {
    a[0][i] = a[0][i + 1] * 2 % (g - 1);
    a[1][i] = a[1][i + 1] * 2 % (g + 1);
  }
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1')
      (r[0] += a[0][i]) %= (g - 1), (r[1] += a[1][i]) %= (g + 1);
  }
  for (int i = 0; i < n; ++i) {
    int y = 0;
    if (x[i] == '1') {
      y = r[0] - a[0][i];
      if (y < 0)
        y += g - 1;
    } else {
      y = r[1] + a[1][i];
      if (y >= g + 1)
        y -= g + 1;
    }
    if (x[i] == '1' && g == 1) {
      cout << 0 << "\\n";
    } else {
      cout << F(y) + 1 << "\\n";
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

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

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  auto f = [](ll n) -> ll {
    ll cnt{0};
    while (true) {
      if (n == 0)
        return cnt;
      ++cnt;
      n %= __builtin_popcount(n);
    }
  };

  ll N;
  string S;
  cin >> N >> S;

  ll cnt1{0};
  for (auto e : S)
    cnt1 += e - '0';

  ll s1, s2;
  s1 = s2 = 0;
  REP(i, N) {
    if (S[i] == '1') {
      if (cnt1 > 1)
        (s1 += modpow(2, N - 1 - i, cnt1 - 1)) %= (cnt1 - 1);
      (s2 += modpow(2, N - 1 - i, cnt1 + 1)) %= (cnt1 + 1);
    }
  }

  REP(i, N) {
    ll res;
    if (S[i] == '1') {
      if (cnt1 == 1)
        res = 0;
      else
        res = f((s1 - modpow(2, N - 1 - i, cnt1 - 1) + cnt1 - 1) % (cnt1 - 1)) +
              1;
    } else {
      res = f((s2 + modpow(2, N - 1 - i, cnt1 + 1)) % (cnt1 + 1)) + 1;
    }

    std::cout << res << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

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

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  auto f = [](ll n) -> ll {
    ll cnt{0};
    while (true) {
      if (n == 0)
        return cnt;
      ++cnt;
      n %= __builtin_popcount(n);
    }
  };

  ll N;
  string S;
  cin >> N >> S;

  ll cnt1{0};
  for (auto e : S)
    cnt1 += e - '0';

  ll s1, s2;
  s1 = s2 = 0;
  REP(i, N) {
    if (S[i] == '1') {
      (s1 += modpow(2, N - 1 - i, cnt1 - 1)) %= (cnt1 - 1);
      if (cnt1 > 1)
        (s2 += modpow(2, N - 1 - i, cnt1 + 1)) %= (cnt1 + 1);
    }
  }

  REP(i, N) {
    ll res;
    if (S[i] == '1') {
      if (cnt1 == 1)
        res = 0;
      else
        res = f((s1 - modpow(2, N - 1 - i, cnt1 - 1) + cnt1 - 1) % (cnt1 - 1)) +
              1;
    } else {
      res = f((s2 + modpow(2, N - 1 - i, cnt1 + 1)) % (cnt1 + 1)) + 1;
    }

    std::cout << res << std::endl;
  }

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

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

using ll = long long;

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int cnt = 0;
  rep(i, n) if (x[i] == '1')++ cnt;
  vector<ll> mod1(n, 1), mod2(n, 1);
  ll cnt1 = cnt + 1;
  ll cnt2 = max(cnt - 1, 1);
  rng(i, 1, n) {
    mod1[i] = (mod1[i - 1] * 2) % cnt1;
    mod2[i] = (mod2[i - 1] * 2) % cnt2;
  }
  ll m1 = 0, m2 = 0;
  rep(i, n) {
    if (x[n - i - 1] == '1') {
      m1 += mod1[i];
      m2 += mod2[i];
      m1 %= cnt1;
      m2 %= cnt2;
    }
  }
  vector<ll> tans(n);
  rep(i, n) {
    if (x[i] == '0') {
      ll a = mod1[n - i - 1] + m1;
      a %= cnt1;
      tans[i] = a;
    } else {
      if (cnt == 1)
        tans[i] = -1;
      else {
        ll a = m2 - mod2[n - i - 1];
        a = (a + cnt2) % cnt2;
        tans[i] = a;
      }
    }
  }
  vector<ll> ans(n);
  rep(i, n) {
    ll a = tans[i];
    if (a < 0) {
      ans[i] = 0;
      continue;
    }
    int cnt = 1;
    while (a) {
      ll ta = a;
      ll p = 0;
      while (ta) {
        if (ta & 1)
          ++p;
        ta >>= 1;
      }
      a %= p;
      ++cnt;
    }
    ans[i] = cnt;
  }
  for (int a : ans)
    cout << a << endl;
  /*
  rep(i,n) {
      cout << i << ","  << tans[i] << "," << ans[i] << endl;
  }
  */
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int cnt = 0;
  rep(i, n) if (x[i] == '1')++ cnt;
  vector<ll> mod1(n, 1), mod2(n, 1);
  ll cnt1 = (cnt == n ? cnt : cnt + 1);
  ll cnt2 = (cnt == 0 ? cnt : cnt - 1);
  rng(i, 1, n) {
    mod1[i] = (mod1[i - 1] * 2) % cnt1;
    mod2[i] = (mod2[i - 1] * 2) % cnt2;
  }
  ll m1 = 0, m2 = 0;
  rep(i, n) {
    if (x[n - i - 1] == '1') {
      m1 += mod1[i];
      m2 += mod2[i];
      m1 %= cnt1;
      m2 %= cnt2;
    }
  }
  vector<ll> tans(n);
  rep(i, n) {
    if (x[i] == '0') {
      ll a = mod1[n - i - 1] + m1;
      a %= cnt1;
      tans[i] = a;
    } else {
      ll a = m2 - mod2[n - i - 1];
      a = cnt2 == 0 ? 0 : (a + cnt2) % cnt2;
      tans[i] = a;
    }
  }
  vector<ll> ans(n);
  rep(i, n) {
    ll a = tans[i];
    int cnt = 1;
    while (a) {
      ll ta = a;
      ll p = 0;
      while (ta) {
        if (ta & 1)
          ++p;
        ta >>= 1;
      }
      a %= p;
      ++cnt;
    }
    ans[i] = cnt;
  }
  for (int a : ans)
    cout << a << endl;
  /*
  rep(i,n) {
      cout << i << ","  << tans[i] << "," << ans[i] << endl;
  }
  */
  return 0;
}