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

#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = a; i < b; i++)
#define int long long
#define mod 1000000009
#define garou                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
int pow(int p, int r, int m) {
  int ans = 1;
  while (r > 0) {
    if (r & 1) {
      ans = ((ans % m) * (p % m)) % m;
    }
    r = r >> 1;
    p = ((p % m) * (p % m)) % m;
  }
  return ans;
}
int popcount(int n) {
  int ans = 0;
  REP(i, 0, 32) {
    if ((n & (1 << i)))
      ans++;
  }
  return ans;
}
int binary(string x, int m) {
  int n = x.length();
  int ans = 0, j = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] != '0')
      ans = (ans + pow(2, j, m)) % m;
    j++;
  }
  return ans;
}
signed main() {
  garou;
  int n;
  cin >> n;
  string x;
  cin >> x;
  int one = 0;
  int n1 = x.length();
  REP(i, 0, n) {
    if (x[i] == '1')
      one++;
  }
  int a = binary(x, one + 1);
  int b = 0;
  if (one > 1)
    b = binary(x, one - 1);
  REP(i, 0, n1) {
    if (x[i] == '1' && (one - 1) == 0) {
      cout << 0 << endl;
      continue;
    }
    int c = 0;
    if (x[i] == '0')
      c = (a + pow(2, n1 - i - 1, one + 1)) % (one + 1);
    else
      c = (b - pow(2, n1 - i - 1, one - 1) + one - 1) % (one - 1);
    int re = 0;
    while (c) {
      c %= popcount(c);
      re++;
    }
    cout << re + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = a; i < b; i++)
#define int long long
#define mod 1000000009
#define garou                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
int pow(int p, int r, int m) {
  int ans = 1;
  while (r > 0) {
    if (r & 1) {
      ans = ((ans % m) * (p % m)) % m;
    }
    r = r >> 1;
    p = ((p % m) * (p % m)) % m;
  }
  return ans;
}
int popcount(int n) {
  int ans = 0;
  REP(i, 0, 32) {
    if ((n & (1 << i)))
      ans++;
  }
  return ans;
}
int binary(string x, int m) {
  int n = x.length();
  int ans = 0, j = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] != '0')
      ans = (ans + pow(2, j, m)) % m;
    j++;
  }
  return ans;
}
signed main() {
  garou;
  int n;
  cin >> n;
  string x;
  cin >> x;
  int one = 0;
  int n1 = x.length();
  REP(i, 0, n) {
    if (x[i] == '1')
      one++;
  }
  int a = binary(x, one + 1);
  int b = binary(x, one - 1);
  REP(i, 0, n1) {
    if (x[i] == '1' && (one - 1) == 0) {
      cout << 0 << endl;
      continue;
    }
    int c = 0;
    if (x[i] == '0')
      c = (a + pow(2, n1 - i - 1, one + 1)) % (one + 1);
    else
      c = (b - pow(2, n1 - i - 1, one - 1) + one - 1) % (one - 1);
    int re = 0;
    while (c) {
      c %= popcount(c);
      re++;
    }
    cout << re + 1 << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sum(x) accumulate(all(x), 0)
#define gll greater<ll>()

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

void solve() {
  ll n;
  string x;

  cin >> n >> x;

  ll bit = 0;
  for (auto c : x)
    if (c == '1')
      bit++;

  ll plus = 0, minus = 0;
  repr(i, n, 0) {
    if (x[i] == '0')
      continue;
    plus += mod_pow(2, n - 1 - i, bit + 1);
    if (bit > 1)
      minus += mod_pow(2, n - 1 - i, bit - 1);
  }

  rep(i, 0, n) {
    ll ans = 1;
    ll cnt;
    if (x[i] == '0')
      cnt = (plus + mod_pow(2, n - 1 - i, bit + 1)) % (bit + 1);
    else if (bit > 1)
      cnt = (minus - mod_pow(2, n - 1 - i, bit - 1) + (bit - 1)) % (bit - 1);
    else {
      cout << 0 << endl;
      continue;
    }
    while (cnt) {
      cnt = cnt % __builtin_popcount(cnt);
      ans++;
    }

    cout << ans << endl;
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sum(x) accumulate(all(x), 0)
#define gll greater<ll>()

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

void solve() {
  ll n;
  string x;

  cin >> n >> x;

  ll bit = 0;
  for (auto c : x)
    if (c == '1')
      bit++;

  ll plus = 0, minus = 0;
  repr(i, n, 0) {
    if (x[i] == '0')
      continue;
    plus += mod_pow(2, n - 1 - i, bit + 1);
    minus += mod_pow(2, n - 1 - i, bit - 1);
  }

  rep(i, 0, n) {
    ll ans = 1;
    ll cnt;
    if (x[i] == '0')
      cnt = (plus + mod_pow(2, n - 1 - i, bit + 1)) % (bit + 1);
    else
      cnt = (minus - mod_pow(2, n - 1 - i, bit - 1) + (bit - 1)) % (bit - 1);

    while (cnt) {
      cnt = cnt % __builtin_popcount(cnt);
      ans++;
    }

    cout << ans << endl;
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pb push_back
#define ii pair<int, int>
#define iii pair<ii, int>
#define ppb pop_back
#define st first
#define nd second
#define ll long long
#define N 500005
#define inf 2000000005
#define MOD 1000000007
#define LOG 14
#define EPS 0.000000001
using namespace std;

int n;
int mod[2];
int f[N];
int p[2][N], s[2];
string S;

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  for (int i = 1; i < N; i++) {
    int cnt = __builtin_popcount(i);
    f[i] = f[i % cnt] + 1;
  }
  cin >> n;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (S[i] == '1');
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (S[i] == '1')
        cout << "0\\n";
      else {
        if (i == n - 1 or S[n - 1] == '1') {
          cout << "2\\n";
        } else {
          cout << "1\\n";
        }
      }
    }
    return 0;
  }
  mod[0] = cnt - 1;
  mod[1] = cnt + 1;
  for (int i = 0; i < 2; i++) {
    p[i][0] = 1;
    for (int j = 1; j <= n; j++) {
      p[i][j] = (ll)p[i][j - 1] * 2 % mod[i];
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {
      int bit = n - j - 1;
      if (S[j] == '1')
        s[i] = ((ll)s[i] + p[i][bit]) % mod[i];
    }
  }
  for (int i = 0; i < n; i++) {
    int bit = n - i - 1;
    int sum;
    if (S[i] == '1') {
      sum = s[0];
      sum = (((ll)sum - p[0][bit]) % mod[0] + mod[0]) % mod[0];
    } else {
      sum = s[1];
      sum = (((ll)sum + p[1][bit]) % mod[1] + mod[1]) % mod[1];
    }
    cout << f[sum] + 1 << "\\n";
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pb push_back
#define ii pair<int, int>
#define iii pair<ii, int>
#define ppb pop_back
#define st first
#define nd second
#define ll long long
#define N 500005
#define inf 2000000005
#define MOD 1000000007
#define LOG 14
#define EPS 0.000000001
using namespace std;

int n;
int mod[2];
int f[N];
int p[2][N], s[2];
string S;

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  for (int i = 1; i < N; i++) {
    int cnt = __builtin_popcount(i);
    f[i] = f[i % cnt] + 1;
  }
  cin >> n;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (S[i] == '1');
  mod[0] = cnt - 1;
  mod[1] = cnt + 1;
  for (int i = 0; i < 2; i++) {
    p[i][0] = 1;
    for (int j = 1; j <= n; j++) {
      p[i][j] = (ll)p[i][j - 1] * 2 % mod[i];
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {
      int bit = n - j - 1;
      if (S[j] == '1')
        s[i] = ((ll)s[i] + p[i][bit]) % mod[i];
    }
  }
  for (int i = 0; i < n; i++) {
    int bit = n - i - 1;
    int sum;
    if (S[i] == '1') {
      sum = s[0];
      sum = (((ll)sum - p[0][bit]) % mod[0] + mod[0]) % mod[0];
    } else {
      sum = s[1];
      sum = (((ll)sum + p[1][bit]) % mod[1] + mod[1]) % mod[1];
    }
    cout << f[sum] + 1 << "\\n";
  }
}
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;
using ll = long long;
using P = pair<int, int>;

int pcnt(int x) { return __builtin_popcount(x); }
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  rep(i, n) x[i] = s[i] - '0';
  int pc = 0;
  rep(i, n) pc += x[i];
  vector<int> ans(n);
  rep(b, 2) {
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    if (npc <= 0)
      continue;
    int r0 = 0;
    rep(i, n) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (x[i] == b) {
        int r = r0;
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[i] = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }

  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int pcnt(int x) { return __builtin_popcount(x); }

int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  rep(i, n) x[i] = s[i] - '0';
  int pc = 0;
  rep(i, n) pc += x[i];
  vector<int> ans(n);
  rep(b, 2) {
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    int r0 = 0;
    rep(i, n) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == b) {
        int r = r0;
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[i] = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int modpow(int base, int n, int mod) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = 1L * res * base % mod;
    base = 1L * base * base % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int bcnt = 0;
  vector<bool> x(n);
  for (int i = 0; i < n; ++i) {
    char X;
    cin >> X;
    x.at(i) = (X - '0');
    bcnt += x.at(i);
  }
  reverse(x.begin(), x.end());
  int set = 0, mask = 0;
  for (int i = 0; i < n; ++i) {
    if (x.at(i)) {
      set = (set + modpow(2, i, bcnt + 1)) % (bcnt + 1);
      if (bcnt - 1 > 0)
        mask = (mask + modpow(2, i, bcnt - 1)) % (bcnt - 1);
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    int ans = 0, bitc = bcnt, tmask = 0;
    if (x.at(i)) {
      --bitc;
      if (bitc > 0)
        tmask = mask - modpow(2, i, bitc) + bitc;
    } else {
      ++bitc;
      tmask = set + modpow(2, i, bitc);
    }
    if (bitc > 0) {
      tmask %= bitc;
      ++ans;
    }
    while (tmask) {
      bitc = 0;
      for (int j = 1; j <= tmask; j <<= 1) {
        if (j & tmask)
          ++bitc;
      }
      tmask %= bitc;
      ++ans;
    }
    cout << ans << "\\n"s;
  }
}


Error: 0

Fixed submission:

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

int modpow(int base, int n, int mod) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = 1L * res * base % mod;
    base = 1L * base * base % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int bcnt = 0;
  vector<bool> x(n);
  for (int i = 0; i < n; ++i) {
    char X;
    cin >> X;
    x.at(i) = (X - '0');
    bcnt += x.at(i);
  }
  reverse(x.begin(), x.end());
  int set = 0, mask = 0;
  for (int i = 0; i < n; ++i) {
    if (x.at(i)) {
      set += modpow(2, i, bcnt + 1);
      if (bcnt - 1 > 0)
        mask += modpow(2, i, bcnt - 1);
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    int ans = 0, bitc = bcnt, tmask = 0;
    if (x.at(i)) {
      --bitc;
      if (bitc > 0)
        tmask = mask - modpow(2, i, bitc) + bitc;
    } else {
      ++bitc;
      tmask = set + modpow(2, i, bitc);
    }
    if (bitc > 0) {
      tmask %= bitc;
      ++ans;
    }
    while (tmask) {
      bitc = 0;
      for (int j = 1; j <= tmask; j <<= 1) {
        if (j & tmask)
          ++bitc;
      }
      tmask %= bitc;
      ++ans;
    }
    cout << ans << "\\n"s;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

long long mpow(long long x, long long y, long long mod) {
  if (y == 0)
    return 1;
  long long ret = mpow(x, y / 2, mod);
  ret *= ret;
  ret %= mod;
  if (y % 2 == 1)
    ret *= x;
  ret %= mod;
  return ret;
}

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

  vector<long long> V(N, 0);
  long long one = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      V[N - i - 1] = 1;
      one++;
    }
  }

  long long A0 = 0, A1 = 0;
  for (long long i = 0; i < N; i++) {
    if (V[i] == 1) {
      A0 += mpow(2, i, one + 1);
      A0 %= one + 1;
    }
  }
  if (one >= 2) {
    for (long long i = 0; i < N; i++) {
      if (V[i] == 1) {
        A1 += mpow(2, i, one - 1);
        A1 %= one - 1;
      }
    }
  }

  for (long long i = N - 1; i >= 0; i--) {
    long long mod, Y;
    if (V[i] == 0) {
      mod = one + 1;
      Y = A0;
      Y += mpow(2, i, mod);
      Y %= mod;
    } else {
      mod = one - 1;
      if (mod == 0) {
        cout << 0 << endl;
        continue;
      }
      Y = A1;
      Y += mod;
      Y -= mpow(2, i, mod);
      Y %= mod;
    }

    int ans = 1;
    while (Y > 0) {
      bitset<32> now(Y);
      Y %= now.count();
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

long long mpow(long long x, long long y, long long mod) {
  if (y == 0)
    return 1;
  long long ret = mpow(x, y / 2, mod);
  ret *= ret;
  ret %= mod;
  if (y % 2 == 1)
    ret *= x;
  ret %= mod;
  return ret;
}

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

  vector<long long> V(N, 0);
  long long one = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      V[N - i - 1] = 1;
      one++;
    }
  }

  long long A0 = 0, A1 = 0;
  for (long long i = 0; i < N; i++) {
    if (V[i] == 1) {
      A0 += mpow(2, i, one + 1);
      A0 %= one + 1;
    }
  }
  for (long long i = 0; i < N; i++) {
    if (V[i] == 1) {
      A1 += mpow(2, i, one - 1);
      A1 %= one - 1;
    }
  }

  for (long long i = N - 1; i >= 0; i--) {
    long long mod, Y;
    if (V[i] == 0) {
      mod = one + 1;
      Y = A0;
      Y += mpow(2, i, mod);
      Y %= mod;
    } else {
      mod = one - 1;
      Y = A1;
      Y += mod;
      Y -= mpow(2, i, mod);
      Y %= mod;
    }

    int ans = 1;
    while (Y > 0) {
      bitset<32> now(Y);
      Y %= now.count();
      ans++;
    }
    cout << ans << endl;
  }

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

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

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)

int memo[202020];

int f(int x) {
  if (x == 0)
    return 0;
  if (memo[x] >= 0)
    return memo[x];
  int p = __builtin_popcount(x);
  return memo[x] = f(x % p) + 1;
}

int modpow(int b, int e, int p) {
  if (e == 0)
    return 1;
  return modpow(b * b % p, e / 2, p) * (e % 2 ? b : 1) % p;
}

signed main() {
  fill(memo, memo + 202020, -1);

  int n;
  cin >> n;
  string x;
  cin >> x;

  int p = 0;
  for (auto c : x)
    if (c == '1')
      p++;

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

  int one = 0, zero = 0, b_one = 1, b_zero = 1;
  for (auto c : x) {
    if (c == '1') {
      one += b_one;
      if (p > 1)
        one %= p - 1;
      zero += b_zero;
      zero %= p + 1;
    }
    b_one *= 2;
    if (p > 1)
      b_one %= p - 1;
    b_zero *= 2;
    b_zero %= p + 1;
  }

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

  rep(i, 0, n) {
    if (x[i] == '1') {
      if (p == 1)
        cout << 0 << endl;
      else {
        int t = (one - modpow(2, n - 1 - i, p - 1) + p - 1) % (p - 1);
        cout << f(t) + 1 << endl;
      }
    } else {
      int t = (zero + modpow(2, n - 1 - i, p + 1)) % (p + 1);
      cout << f(t) + 1 << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)

int memo[202020];

int f(int x) {
  if (x == 0)
    return 0;
  if (memo[x] >= 0)
    return memo[x];
  int p = __builtin_popcount(x);
  return memo[x] = f(x % p) + 1;
}

int modpow(int b, int e, int p) {
  if (e == 0)
    return 1;
  return modpow(b * b % p, e / 2, p) * (e % 2 ? b : 1) % p;
}

signed main() {
  fill(memo, memo + 202020, -1);

  int n;
  cin >> n;
  string x;
  cin >> x;

  if (n < 30) {
    int xx = 0;
    rep(i, 0, n) if (x[i] == '1') xx += 1LL << (n - 1 - i);
    rep(i, 1, n + 1) cout << f(xx ^ (1LL << (n - i))) << endl;
    return 0;
  }

  int p = 0;
  for (auto c : x)
    if (c == '1')
      p++;

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

  int one = 0, zero = 0, b_one = 1, b_zero = 1;
  for (auto c : x) {
    if (c == '1') {
      one += b_one;
      one %= p - 1;
      zero += b_zero;
      zero %= p + 1;
    }
    b_one *= 2;
    b_one %= p - 1;
    b_zero *= 2;
    b_zero %= p + 1;
  }

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

  rep(i, 0, n) {
    if (x[i] == '1') {
      int t = (one - modpow(2, n - 1 - i, p - 1) + p - 1) % (p - 1);
      cout << f(t) + 1 << endl;
    } else {
      int t = (zero + modpow(2, n - 1 - i, p + 1)) % (p + 1);
      cout << f(t) + 1 << endl;
    }
  }

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

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

double PI = 3.141592653589793;

int64_t mypow(int64_t x, int64_t y, int64_t MOD) // x ^ y
{
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  else if (y % 2 == 1)
    return mypow(x, y - 1, MOD) * x % MOD;
  else {
    int64_t tmp = mypow(x, y / 2, MOD);
    return tmp * tmp % MOD;
  }
}

int popcount(int64_t N) {
  int x = 0;
  while (N > 0) {
    if (N % 2 == 1)
      x++;
    N /= 2;
  }
  return x;
}

int main() {
  int N, one = 0;
  string X;
  cin >> N >> X;

  for (int i = 0; i < N; i++)
    if (X[i] == '1')
      one++;

  int plus = one + 1, minus = one - 1;

  bool minus_ = false;
  if (minus == 0)
    minus_ = true, minus = 1;

  int p = 0, p_keta = 1, m = 0, m_keta = 1; // 0→1,1→0
  reverse(X.begin(), X.end());
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      p += p_keta, m += m_keta;
    p %= plus, m %= minus;
    p_keta = (p_keta * 2) % plus, m_keta = (m_keta * 2) % minus;
  }
  int tmp;
  reverse(X.begin(), X.end());
  for (int i = 0; i < N; i++) {
    int count = 0;
    if (X[i] == '0') {
      tmp = p;
      tmp = (tmp + mypow(2, N - 1 - i, plus)) % plus;
      while (tmp != 0) {
        tmp %= popcount(tmp);
        count++;
      }
      cout << count + 1 << endl;
    }
    if (X[i] == '1') {
      if (minus_)
        cout << 0 << endl;
      else {
        tmp = m;
        tmp = (tmp - mypow(2, N - 1 - i, minus) + minus) % minus;
        while (tmp != 0) {
          tmp %= popcount(tmp);
          count++;
        }
        cout << count + 1 << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

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

double PI = 3.141592653589793;

int64_t mypow(int64_t x, int64_t y, int64_t MOD) // x ^ y
{
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  else if (y % 2 == 1)
    return mypow(x, y - 1, MOD) * x % MOD;
  else {
    int64_t tmp = mypow(x, y / 2, MOD);
    return tmp * tmp % MOD;
  }
}

int popcount(int64_t N) {
  int x = 0;
  while (N > 0) {
    if (N % 2 == 1)
      x++;
    N /= 2;
  }
  return x;
}

int main() {
  int N, one = 0;
  string X;
  cin >> N >> X;

  for (int i = 0; i < N; i++)
    if (X[i] == '1')
      one++;

  int plus = one + 1, minus = one - 1;
  int p = 0, p_keta = 1, m = 0, m_keta = 1; // 0→1,1→0
  reverse(X.begin(), X.end());
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      p += p_keta, m += m_keta;
    p %= plus, m %= minus;
    p_keta = (p_keta * 2) % plus, m_keta = (m_keta * 2) % minus;
  }
  int tmp;
  reverse(X.begin(), X.end());
  for (int i = 0; i < N; i++) {
    int count = 0;
    if (X[i] == '0') {
      tmp = p;
      tmp = (tmp + mypow(2, N - 1 - i, plus)) % plus;
      while (tmp != 0) {
        tmp %= popcount(tmp);
        count++;
      }
      cout << count + 1 << endl;
    }
    if (X[i] == '1') {
      tmp = m;
      tmp = (tmp - mypow(2, N - 1 - i, minus) + minus) % minus;
      while (tmp != 0) {
        tmp %= popcount(tmp);
        count++;
      }
      cout << count + 1 << endl;
    }
  }
}