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

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

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

void solve(int N, string X) {
  int pcX = 0;
  for (int i = 0; i < N; i++) {
    if (X.at(i) == '1')
      pcX++;
  }
  vector<int> pow2p(N);
  int tp = 1;
  int xp = 0;
  for (int i = 0; i < N; i++) {
    pow2p.at(N - 1 - i) = tp;
    tp = (tp << 1) % (pcX + 1);
    xp = (xp << 1) % (pcX + 1);
    xp = (xp + (X.at(i) - '0')) % (pcX + 1);
  }
  vector<int> pow2n(N);
  int tn = 1;
  int xn = 0;
  if (pcX > 1) {
    for (int i = 0; i < N; i++) {
      pow2n.at(N - 1 - i) = tn;
      tn = (tn << 1) % (pcX - 1);
      xn = (xn << 1) % (pcX - 1);
      xn = (xn + (X.at(i) - '0')) % (pcX - 1);
    }
  }

  for (int i = 0; i < N; i++) {
    int x;
    if (X.at(i) == '0') {
      x = (xp + pow2p.at(i)) % (pcX + 1);
    } else {
      if (pcX > 1) {
        x = ((pcX - 1) + xn - pow2n.at(i)) % (pcX - 1);
      } else {
        cout << 0 << endl;
        continue;
      }
    }
    cout << f(x) + 1 << endl;
  }
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  solve(N, X);
  return 0;
}


Error: 0

Fixed submission:

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

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

void solve(int N, string X) {
  int pcX = 0;
  for (int i = 0; i < N; i++) {
    if (X.at(i) == '1')
      pcX++;
  }
  vector<int> pow2p(N);
  int tp = 1;
  int xp = 0;
  for (int i = 0; i < N; i++) {
    pow2p.at(N - 1 - i) = tp;
    tp = (tp << 1) % (pcX + 1);
    xp = (xp << 1) % (pcX + 1);
    xp = (xp + (X.at(i) - '0')) % (pcX + 1);
  }
  vector<int> pow2n(N);
  int tn = 1;
  int xn = 0;
  if (pcX > 0) {
    for (int i = 0; i < N; i++) {
      pow2n.at(N - 1 - i) = tn;
      tn = (tn << 1) % (pcX - 1);
      xn = (xn << 1) % (pcX - 1);
      xn = (xn + (X.at(i) - '0')) % (pcX - 1);
    }
  }

  for (int i = 0; i < N; i++) {
    int x;
    if (X.at(i) == '0') {
      x = (xp + pow2p.at(i)) % (pcX + 1);
    } else {
      if (pcX > 0) {
        x = ((pcX - 1) + xn - pow2n.at(i)) % (pcX - 1);
      } else {
        cout << 0 << endl;
        continue;
      }
    }
    cout << f(x) + 1 << endl;
  }
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  solve(N, X);
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  if (p == 0) {
    cout << 1 << endl;
    return 0;
  }

  vector<int> pow2a(n), pow2b(n); // pow2[i] = 2^i
  pow2a[0] = 1;
  pow2b[0] = 1;
  for (int i = 0; i + 1 < n; i++) {
    if (p != 1) {
      pow2a[i + 1] = pow2a[i] * 2;
      pow2a[i + 1] %= (p - 1);
    }
    pow2b[i + 1] = pow2b[i] * 2;
    pow2b[i + 1] %= (p + 1);
  }

  int na = 0, nb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (p != 1) {
        na += pow2a[n - 1 - i];
        na %= (p - 1);
      }
      nb += pow2b[n - 1 - i];
      nb %= (p + 1);
    }
  }

  vector<int> ans(3 * 1e5);
  ans[0] = 0;
  for (int i = 1; i < 3 * 1e5; i++) {
    ans[i] = ans[i % __builtin_popcount(i)] + 1;
  }

  for (int i = 0; i < n; i++) {
    int flipped, mod;
    if (p == 1 && x[i] == '1') {
      cout << 0 << endl;
      continue;
    } else if (x[i] == '1') {
      flipped = na - pow2a[n - 1 - i];
      mod = p - 1;
    } else if (x[i] == '0') {
      flipped = nb + pow2b[n - 1 - i];
      mod = p + 1;
    }
    flipped = (flipped % mod + mod) % mod;
    // cout << flipped << " " << mod << " : ";
    cout << ans[flipped] + 1 << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

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

  int p = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      p += 1;
  }
  if (p == 0 || p == 1) {
    cout << 0 << endl << 0 << endl;
    return 0;
  }

  vector<int> pow2a(n), pow2b(n); // pow2[i] = 2^i
  pow2a[0] = 1;
  pow2b[0] = 1;
  for (int i = 0; i + 1 < n; i++) {
    pow2a[i + 1] = pow2a[i] * 2;
    pow2a[i + 1] %= (p - 1);
    pow2b[i + 1] = pow2b[i] * 2;
    pow2b[i + 1] %= (p + 1);
  }
  int na = 0, nb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      na += pow2a[n - 1 - i];
      na %= (p - 1);
      nb += pow2b[n - 1 - i];
      nb %= (p + 1);
    }
  }

  vector<int> ans(2 * 1e5 + 2);
  ans[0] = 0;
  for (int i = 1; i < 3 * 1e5; i++) {
    ans[i] = ans[i % __builtin_popcount(i)] + 1;
  }

  for (int i = 0; i < n; i++) {
    int flipped, mod;
    if (x[i] == '1') {
      flipped = na - pow2a[n - 1 - i];
      mod = p - 1;
    }
    if (x[i] == '0') {
      flipped = nb + pow2b[n - 1 - i];
      mod = p + 1;
    }
    flipped = (flipped % mod + mod) % mod;
    // cout << flipped << " " << mod << " : ";
    cout << ans[flipped] + 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)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define int ll
typedef bitset<200001> Mybit;
signed main() {
  ios::sync_with_stdio(false);
  int n;
  string x;
  cin >> n >> x;
  Mybit xbit(x);
  int x_popcnt = xbit.count();
  vector<int> mod_plus(n + 1),
      mod_minus(n + 1); // 2^iをx_popcnt+1, x_popcnt-1で割ったあまり
  mod_plus[0] = 1;
  mod_minus[0] = 1;

  for (int i = 1; i < n; i++) {
    mod_plus[i] = mod_plus[i - 1] * 2;
    mod_plus[i] %= x_popcnt + 1;
    if (x_popcnt == 1)
      continue;
    mod_minus[i] = mod_minus[i - 1] * 2;
    mod_minus[i] %= x_popcnt - 1;
  }

  int xmodp = 0, xmodm = 0;
  rep(i, n) {
    if (xbit[i] == 1) {
      xmodp += mod_plus[i];
      xmodp %= x_popcnt + 1;
      if (x_popcnt == 1)
        continue;
      xmodm += mod_minus[i];
      xmodm %= x_popcnt - 1;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    int ans = 1;
    int mod;
    // 一回だけ自力で
    if (xbit[i] == 0) {
      mod = xmodp;
      mod += mod_plus[i];
      mod %= x_popcnt + 1;
    } else {
      mod = xmodm;
      mod += x_popcnt - 1;
      if (x_popcnt == 1) {
        cout << 0 << endl;
        continue;
      }
      mod -= mod_minus[i];
      mod %= x_popcnt - 1;
    }
    while (mod > 0) {
      mod %= __builtin_popcount(mod);
      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;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define int ll
typedef bitset<200001> Mybit;
signed main() {
  ios::sync_with_stdio(false);
  int n;
  string x;
  cin >> n >> x;
  Mybit xbit(x);
  int cnt = xbit.count();
  vector<int> mod_plus(n + 1),
      mod_minus(n + 1); // 2^iをcnt+1, cnt-1で割ったあまり
  mod_plus[0] = 1;
  mod_minus[0] = 1;
  if (cnt == 1) {
    // cntが1のときの対策が必要
  }
  for (int i = 1; i <= n; i++) {
    mod_plus[i] = mod_plus[i - 1] * 2;
    mod_plus[i] %= cnt + 1;
    mod_minus[i] = mod_minus[i - 1] * 2;
    mod_minus[i] %= cnt - 1;
  }

  int xmodp = 0, xmodm = 0;
  rep(i, n) {
    if (xbit[i] == 1) {
      xmodp += mod_plus[i];
      xmodp %= cnt + 1;
      xmodm += mod_minus[i];
      xmodp %= cnt - 1;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    int ans = 1;
    int mod;
    // 一回だけ自力で
    if (xbit[i] == 0) {
      mod = xmodp;
      mod += mod_plus[i];
      mod %= cnt + 1;
    } else {
      mod = xmodm;
      mod += cnt - 1;
      mod -= mod_minus[i];
      mod %= cnt - 1;
    }
    while (mod > 0) {
      mod %= __builtin_popcount(mod);
      ans++;
    }
    cout << ans << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int n, m, pw[200005];
string s;
int get(int x) {
  if (x == 0)
    return 0;
  int d = __builtin_popcount(x);
  return 1 + get(x % d);
}
void solve() {
  cin >> n >> s;
  reverse(all(s));
  for (auto i : s)
    m += i == '1';
  int mod = (m + 1);
  if (m - 1 > 0)
    mod *= (m - 1);
  pw[0] = 1;
  rep(i, 1, n) pw[i] = 2 * pw[i - 1] % mod;
  int cur = 0;
  rep(i, 0, n) {
    if (s[i] == '1')
      cur = (cur + pw[i]) % mod;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (m == 1)
        cout << 0 << endl;
      else {
        int rem = (cur - pw[i] + mod) % (m - 1);
        cout << 1 + get(rem) << endl;
      }
    } else {
      int rem = (cur + pw[i] + mod) % (m + 1);
      cout << 1 + get(rem) << endl;
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int n, m, pw[200005];
string s;
int get(int x) {
  if (x == 0)
    return 0;
  int d = __builtin_popcount(x);
  return 1 + get(x % d);
}
void solve() {
  cin >> n >> s;
  reverse(all(s));
  for (auto i : s)
    m += i == '1';
  cout << m << endl;
  int mod = (m - 1) * (m + 1);
  pw[0] = 1;
  rep(i, 1, n) pw[i] = 2 * pw[i - 1] % mod;
  int cur = 0;
  rep(i, 0, n) {
    if (s[i] == '1')
      cur = (cur + pw[i]) % mod;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      int rem = (cur - pw[i] + mod) % (m - 1);
      cout << 1 + get(rem) << endl;
    } else {
      int rem = (cur + pw[i] + mod) % (m + 1);
      cout << 1 + get(rem) << endl;
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
int n, mod1 = 0, mod2, md1[200010], md2[200010], res1 = 0, res2 = 0, sa;
char s[200010];
void ru(int n) {
  while (n > 0) {
    int q = n, p = 0;
    while (q > 0) {
      q = q & (q - 1);
      p++;
    }
    n = n % p;
    sa++;
  }
}
int main() {
  scanf("%d", &n);
  scanf("%s", &s);
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      mod1++;
  mod2 = mod1 - 1, mod1++;
  if (mod2 == 0) {
    md1[n - 1] = md2[n - 1] = 1;
    for (int i = n - 2; i >= 0; i--)
      md1[i] = md1[i + 1] * 2 % mod1;
    for (int i = n - 1; i >= 0; i--)
      if (s[i] == '1')
        res1 = (res1 + md1[i]) % mod1;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        printf("0\\n");
      if (s[i] == '0') {
        sa = 1;
        int ss = (res1 + md1[i]) % mod1;
        // printf("%d\\n",ss);
        ru(ss);
        printf("%d\\n", sa);
      }
    }
    return 0;
  }
  md1[n - 1] = md2[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--)
    md1[i] = md1[i + 1] * 2 % mod1, md2[i] = md2[i + 1] * 2 % mod2;
  for (int i = n - 1; i >= 0; i--)
    if (s[i] == '1')
      res1 = (res1 + md1[i]) % mod1, res2 = (res2 + md2[i]) % mod2;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      sa = 1;
      int ss = (res2 - md2[i] + mod2) % mod2;
      // printf("%d\\n",ss);
      ru(ss);
      printf("%d\\n", sa);
    }
    if (s[i] == '0') {
      sa = 1;
      int ss = (res1 + md1[i]) % mod1;
      // printf("%d\\n",ss);
      ru(ss);
      printf("%d\\n", sa);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
int n, mod1 = 0, mod2, md1[200010], md2[200010], res1 = 0, res2 = 0, sa;
char s[200010];
void ru(int n) {
  while (n > 0) {
    int q = n, p = 0;
    while (n > 0) {
      n = n & (n - 1);
      p++;
    }
    n = q % p;
    sa++;
  }
}
int main() {
  scanf("%d", &n);
  scanf("%s", &s);
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      mod1++;
  mod2 = mod1 - 1, mod1++;
  md1[n - 1] = md2[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--)
    md1[i] = md1[i + 1] * 2 % mod1, md2[i] = md2[i + 1] * 2 % mod2;
  for (int i = n - 1; i >= 0; i--)
    if (s[i] == '1')
      res1 = (res1 + md1[i]) % mod1, res2 = (res2 + md2[i]) % mod2;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      sa = 1;
      int ss = (res2 - md2[i] + mod2) % mod2;
      // printf("%d\\n",ss);
      ru(ss);
      printf("%d\\n", sa);
    }
    if (s[i] == '0') {
      sa = 1;
      int ss = (res1 + md1[i]) % mod1;
      // printf("%d\\n",ss);
      ru(ss);
      printf("%d\\n", sa);
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1001001001;
vector<pair<int64_t, int64_t>> prime_factorize(int64_t x) {
  vector<pair<int64_t, int64_t>> p;
  for (int64_t i = 2; i * i <= x; i++) {
    int cnt = 0;
    if (x % i == 0) {
      while (x % i == 0) {
        cnt++;
        x /= i;
      }
      p.push_back(make_pair(i, cnt));
    }
  }
  if (x != 1) {
    p.push_back(make_pair(x, 1));
  }
  return p;
}

int f(int x) {
  if (x == 0)
    return 0;
  return f(x % __builtin_popcount(x)) + 1;
}
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    x[i] = S[i] - '0';
  }
  int pop = 0;
  for (int i = 0; i < N; i++) {
    pop += x[i];
  }
  vector<int> table(N);
  for (int b = 0; b < 2; b++) {
    int rev = pop;
    if (b == 0) {
      rev++;
    } else {
      rev--;
    }
    if (rev <= 0) {
      continue;
    }
    int start = 0;
    for (int i = 0; i < N; i++) {
      start *= 2;
      start += x[i];
      start %= rev;
    }
    int popi = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (x[i] == b) {
        int endi = start;
        if (b == 0) {
          endi += popi;
          endi %= rev;
        } else {
          endi -= popi;
          endi += rev; //?
          endi %= rev;
        }
        table[i] = f(endi) + 1;
      }
      popi *= 2;
      popi %= rev;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << table[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1001001001;
vector<pair<int64_t, int64_t>> prime_factorize(int64_t x) {
  vector<pair<int64_t, int64_t>> p;
  for (int64_t i = 2; i * i <= x; i++) {
    int cnt = 0;
    if (x % i == 0) {
      while (x % i == 0) {
        cnt++;
        x /= i;
      }
      p.push_back(make_pair(i, cnt));
    }
  }
  if (x != 1) {
    p.push_back(make_pair(x, 1));
  }
  return p;
}

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

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

    string x = to_string(N);
    int n = (x[N - 1 - i] - '0');
    n ^= 1;
    x[N - 1 - i] = (char)(n + '0');
    int t = stoi(x);
    int a = __builtin_popcount(t);
    int cnt = 0;
    while (t) {
      cnt++;
      t %= a;
    }
    cout << cnt << 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)
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>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  vector<int> bin(n);
  rep(i, n) bin[i] = s[i] - '0';
  rep(i, n) cnt += bin[i];

  vector<int> ans(n);
  rep(b, 2) {
    int ncnt = cnt;
    if (b == 0)
      ++ncnt;
    else
      --ncnt;

    int base = 0;
    rep(i, n) {
      base = (base * 2) % ncnt;
      base = base + bin[i];
    }
    int two = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (bin[i] == b) {
        int nb = base;
        if (b == 0)
          nb = (nb + two) % ncnt;
        else
          nb = (nb + ncnt - two) % ncnt;
        ans[i] = f(nb) + 1;
      }
      two = (two * 2) % ncnt;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

ll f(ll n) {
  if (n == 0)
    return 0;
  return f(n % __builtin_popcountll(n)) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  string x;
  cin >> n >> x;
  reverse(all(x));
  ll pc = 0;
  rep(i, n) pc += x[i] - '0';
  vector<vector<ll>> raw(3, vector<ll>(n + 1, 0));
  vector<vector<ll>> ket(3, vector<ll>(n + 1, 1));
  rep(i, n) {
    rep(j, 3) {
      if ((pc + (j - 1)) <= 0)
        continue;
      raw[j][i + 1] = (raw[j][i] + (x[i] - '0') * ket[j][i]) % (pc + (j - 1));
      ket[j][i + 1] = (ket[j][i] * 2) % (pc + (j - 1));
    }
  }
  rrep(i, n) {
    ll ans = 0;
    if (x[i] == '0') {
      ans = f((raw[2][n] + ket[2][i]) % (pc + 1)) + 1;
    } else {
      if (pc > 1)
        ans = f((raw[0][n] - ket[0][i] + (pc - 1)) % (pc - 1)) + 1;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

ll f(ll n) {
  if (n == 0)
    return 1;
  return f(n % __builtin_popcountll(n)) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  string x;
  cin >> n >> x;
  reverse(all(x));
  ll pc = 0;
  rep(i, n) pc += x[i] - '0';
  vector<vector<ll>> raw(3, vector<ll>(n + 1, 0));
  vector<vector<ll>> ket(3, vector<ll>(n + 1, 1));
  rep(i, n) {
    rep(j, 3) {
      if ((j == 0) && (pc == 0))
        continue;
      raw[j][i + 1] = (raw[j][i] + (x[i] - '0') * ket[j][i]) % (pc + (j - 1));
      ket[j][i + 1] = (ket[j][i] * 2) % (pc + (j - 1));
    }
  }
  rrep(i, n) {
    ll v;
    if (x[i] == '0') {
      v = (raw[2][n] + ket[2][i]) % (pc + 1);
    } else {
      v = (raw[0][n] - ket[0][i] + (pc - 1)) % (pc - 1);
    }
    cout << f(v) << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
  ll n;
  cin >> n;
  string s;
  cin >> s;

  ll t = 0;
  vector<ll> a(200010, 0), b(200010, 0);
  a[0] = b[0] = 1;
  rep(i, n) if (s[i] == '1') t++;
  if (t != 1)
    rep(i, n) a[i + 1] = (a[i] * 2) % (t - 1);
  rep(i, n) b[i + 1] = (b[i] * 2) % (t + 1);
  ll A = 0, B = 0;
  rep(i, n) {
    if (s[i] == '1') {
      A += a[n - i - 1];
      B += b[n - i - 1];
    }
  }
  rep(i, n) {
    if (t == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    string ns = s;
    ll buf2 = t;
    ll buf = 0;
    if (s[i] == '1') {
      ns[i] = '0';
      buf2--;
      buf = A - a[n - i - 1];
    } else {
      ns[i] = '1';
      buf2++;
      buf = B + b[n - i - 1];
    }

    // cout<<buf<<endl;
    buf %= buf2;
    // cout<<buf<<endl;
    int cou = 1;
    while (buf != 0) {
      cou++;
      ll buf3 = buf, x = 0;
      while (buf3) {
        if (buf3 % 2 == 1)
          x++;
        buf3 /= 2;
      }
      buf %= x;
    }
    cout << cou << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
  ll n;
  cin >> n;
  string s;
  cin >> s;

  ll t = 0;
  vector<ll> a(200010, 0), b(200010, 0);
  a[0] = b[0] = 1;
  rep(i, n) if (s[i] == '1') t++;
  if (t != 1)
    rep(i, n) a[i + 1] = (a[i] * 2) % (t - 1);
  rep(i, n) b[i + 1] = (b[i] * 2) % (t + 1);
  rep(i, n) {
    if (t == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    string ns = s;
    ll buf2 = t;
    if (s[i] == '1') {
      ns[i] = '0';
      buf2--;
    } else {
      ns[i] = '1';
      buf2++;
    }
    ll buf = 0;
    rep(j, n) {
      if (ns[j] == '1') {
        if (buf2 == t + 1) {
          buf += b[n - 1 - j];
        } else {
          buf += a[n - 1 - j];
        }
      }
    }
    // cout<<buf<<endl;
    buf %= buf2;
    // cout<<buf<<endl;
    int cou = 1;
    while (buf != 0) {
      cou++;
      ll buf3 = buf, x = 0;
      while (buf3) {
        if (buf3 % 2 == 1)
          x++;
        buf3 /= 2;
      }
      buf %= x;
    }
    cout << cou << endl;
  }
}