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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  auto modpow = [&](long long a, long long b, long long MOD) -> int {
    long long res = 1;
    while (b) {
      if (b & 1) {
        res = (res % MOD * a % MOD) % MOD;
        res %= MOD;
      }
      a = (a % MOD * a % MOD) % MOD;
      b >>= 1;
    }
    res %= MOD;
    return (int)res;
  };
  int cnt = count(s.begin(), s.end(), '1');
  int inc = 0, dec = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      inc += modpow(2, n - i - 1, cnt + 1);
      inc %= (cnt + 1);
      if (cnt > 1) {
        dec += modpow(2, n - i - 1, cnt - 1);
        dec %= (cnt - 1);
      }
    }
  }
  auto solve = [&](int num) {
    int res = 0;
    while (num != 0) {
      ++res;
      num %= __builtin_popcount(num);
    }
    return res;
  };
  for (int i = 0; i < n; i++) {
    long long val;
    if (s[i] == '0') {
      val = modpow(2, n - i - 1, cnt + 1) + inc;
      while (val < 0)
        val += cnt + 1;
      val %= (cnt + 1);
    } else {
      if (cnt > 1) {
        val = dec - modpow(2, n - i - 1, cnt - 1);
        while (val < 0)
          val += cnt - 1;
        val %= (cnt - 1);
      } else {
        cout << "0\\n";
        continue;
      }
    }
    cout << solve(val) + 1 << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  auto modpow = [&](long long a, long long b, long long MOD) -> int {
    long long res = 1;
    while (b) {
      if (b & 1) {
        res = (res % MOD * a % MOD) % MOD;
        res %= MOD;
      }
      a = (a % MOD * a % MOD) % MOD;
      b >>= 1;
    }
    res %= MOD;
    return (int)res;
  };
  int cnt = count(s.begin(), s.end(), '1');
  int inc = 0, dec = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      inc += modpow(2, n - i - 1, cnt + 1);
      inc %= (cnt + 1);
      dec += modpow(2, n - i - 1, cnt - 1);
      dec %= (cnt - 1);
    }
  }
  auto solve = [&](int num) {
    int res = 0;
    while (num != 0) {
      ++res;
      num %= __builtin_popcount(num);
    }
    return res;
  };
  for (int i = 0; i < n; i++) {
    long long val;
    if (s[i] == '0') {
      val = modpow(2, n - i - 1, cnt + 1) + inc;
      while (val < 0)
        val += cnt + 1;
      val %= (cnt + 1);
    } else {
      val = dec - modpow(2, n - i - 1, cnt - 1);
      while (val < 0)
        val += cnt - 1;
      val %= (cnt - 1);
    }
    cout << solve(val) + 1 << '\\n';
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int steps(int num) {
  if (num == 0)
    return 0;
  int count = __builtin_popcount(num);
  return 1 + steps(num % count);
}

signed main() {
  int n;
  cin >> n;
  vector<char> x(n);
  int m = 0;
  for (int i = 0; i < n; ++i) {
    char inp;
    cin >> inp;
    if (inp == '1')
      m++;
    x[i] = inp;
  }

  int rem_minus = 0;
  int rem_plus = 0;
  int cur_m = 1;
  int cur_p = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      if (m > 1) {
        rem_minus = (rem_minus + cur_m) % (m - 1);
      }
      rem_plus = (rem_plus + cur_p) % (m + 1);
    }

    if (m > 1)
      cur_m = (cur_m * 2) % (m - 1);
    cur_p = (cur_p * 2) % (m + 1);
  }

  vector<int> ans(n);
  cur_m = 1;
  cur_p = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '0') {
      int val = rem_plus;
      val = (val + cur_p) % (m + 1);
      ans[i] = 1 + steps(val);
    } else {
      if (m == 1) {
        ans[i] = 0;
      } else {
        int val = rem_minus;
        val = (val - cur_m) % (m - 1);
        if (val < 0)
          val += (m - 1);
        ans[i] = 1 + steps(val);
      }
    }
    if (m > 1)
      cur_m = (cur_m * 2) % (m - 1);
    cur_p = (cur_p * 2) % (m + 1);
  }

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

Error: 0

Fixed submission:

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

int steps(int num) {
  if (num == 0)
    return 0;
  int count = __builtin_popcount(num);
  return 1 + steps(num % count);
}

signed main() {
  int n;
  cin >> n;
  vector<char> x(n);
  int m = 0;
  for (int i = 0; i < n; ++i) {
    char inp;
    cin >> inp;
    if (inp == '1')
      m++;
    x[i] = inp;
  }

  int rem_minus = 0;
  int rem_plus = 0;
  int cur_m = 1;
  int cur_p = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      rem_minus = (rem_minus + cur_m) % (m - 1);
      rem_plus = (rem_plus + cur_p) % (m + 1);
    }

    cur_m = (cur_m * 2) % (m - 1);
    cur_p = (cur_p * 2) % (m + 1);
  }

  vector<int> ans(n);
  cur_m = 1;
  cur_p = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '0') {
      int val = rem_plus;
      val = (val + cur_p) % (m + 1);
      ans[i] = 1 + steps(val);
    } else {
      if (m == 1) {
        ans[i] = 0;
      } else {
        int val = rem_minus;
        val = (val - cur_m) % (m - 1);
        if (val < 0)
          val += (m - 1);
        ans[i] = 1 + steps(val);
      }
    }
    cur_m = (cur_m * 2) % (m - 1);
    cur_p = (cur_p * 2) % (m + 1);
  }

  for (int i = 0; i < n; ++i) {
    cout << ans[i] << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

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

const int N = 2e5 + 10, mod = 1e9 + 7;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int f[N];
  f[0] = 0;
  for (int i = 1; i < N; i++) {
    f[i] = f[i % __builtin_popcount(i)] + 1;
  }
  string x;
  cin >> x;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (x[i] == '1');
  if (cnt == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << "\\n";
    return 0;
  }
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (x[i] == '1')
        cout << 0 << "\\n";
      else if (i == n - 1 || x[n - 1] == '1')
        cout << 2 << "\\n";
      else
        cout << 1 << "\\n";
    }
    return 0;
  }
  int pw1[n], pw2[n];
  int val1 = 0, val2 = 0;
  for (int i = n - 1, cur1 = 1, cur2 = 1; i >= 0;
       i--, cur1 = (cur1 * 2) % (cnt - 1), cur2 = (cur2 * 2) % (cnt + 1)) {
    pw1[n - 1 - i] = cur1;
    pw2[n - 1 - i] = cur2;
    if (x[i] == '1') {
      val1 = (val1 + cur1) % (cnt - 1);
      val2 = (val2 + cur2) % (cnt + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    int add = -1, newVal = -1;
    if (x[i] == '0') {
      add = pw2[n - 1 - i];
      newVal = val2;
      newVal = (newVal + add) % (cnt + 1);
    } else {
      add = (cnt - 1) - pw1[n - 1 - i];
      newVal = val1;
      newVal = (newVal + add) % (cnt - 1);
    }
    cout << f[newVal] + 1 << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

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

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

const int N = 2e5 + 10, mod = 1e9 + 7;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int f[N];
  f[0] = 0;
  for (int i = 1; i < N; i++) {
    f[i] = f[i % __builtin_popcount(i)] + 1;
  }
  string x;
  cin >> x;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (x[i] == '1');
  int pw1[n], pw2[n];
  int val1 = 0, val2 = 0;
  for (int i = n - 1, cur1 = 1, cur2 = 1; i >= 0;
       i--, cur1 = (cur1 * 2) % (cnt - 1), cur2 = (cur2 * 2) % (cnt + 1)) {
    pw1[n - 1 - i] = cur1;
    pw2[n - 1 - i] = cur2;
    if (x[i] == '1') {
      val1 = (val1 + cur1) % (cnt - 1);
      val2 = (val2 + cur2) % (cnt + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    int add = -1, newVal = -1;
    if (x[i] == '0') {
      add = pw2[n - 1 - i];
      newVal = val2;
      newVal = (newVal + add) % (cnt + 1);
    } else {
      add = (cnt - 1) - pw1[n - 1 - i];
      newVal = val1;
      newVal = (newVal + add) % (cnt - 1);
    }
    cout << f[newVal] + 1 << "\\n";
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
const int N = 2 * 1e5 + 5;
ll mpow(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

int n;
string x;

int comp[N];

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> x;
  reverse(all(x));

  for (int i = 1; i < n; ++i) {
    comp[i] = comp[i % __builtin_popcount(i)] + 1;
  }

  int nb = 0;
  for (char i : x) {
    nb += (i == '1');
  }

  // sum when nb - 1 and nb + 1
  ll pos[2] = {0, 0};
  for (int i = 0; i < n; ++i) {
    if (nb - 1 > 0 && x[i] == '1')
      pos[0] = (pos[0] + mpow(2, i, nb - 1)) % (nb - 1);
    if (nb + 1 > 0 && x[i] == '1')
      pos[1] = (pos[1] + mpow(2, i, nb + 1)) % (nb + 1);
  }

  for (int i = n - 1; i >= 0; --i) {
    string tx = x;
    if (tx[i] == '1') {
      if (!(nb - 1)) {
        cout << "0\\n";
        continue;
      }

      ll fin = pos[0] - mpow(2, i, nb - 1);
      fin = (fin + nb - 1) % (nb - 1);
      cout << 1 + comp[fin] << '\\n';
    } else {
      ll fin = pos[1] + mpow(2, i, nb + 1);
      fin %= (nb + 1);
      cout << 1 + comp[fin] << '\\n';
    }
  }
}


Error: TLE

Fixed submission:

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

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
const int N = 2 * 1e5 + 5;
ll mpow(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

int n;
string x;

int comp[N];

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> x;
  reverse(all(x));

  for (int i = 1; i < n; ++i) {
    comp[i] = comp[i % __builtin_popcount(i)] + 1;
  }

  int nb = 0;
  for (char i : x) {
    nb += (i == '1');
  }

  for (int i = n - 1; i >= 0; --i) {
    string tx = x;
    int tnb = nb;
    if (tx[i] == '1') {
      tx[i] = '0';
      --tnb;
    } else {
      tx[i] = '1';
      ++tnb;
    }

    ll tmp = 0;
    for (int j = 0; j < n; ++j) {
      if (tx[j] == '1')
        tmp = (tmp + mpow(2, j, tnb)) % tnb;
    }

    if (tnb)
      cout << 1 + comp[tmp] << '\\n';
    else
      cout << "0\\n";
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> p(2e5 + 50, 0);
  for (int i = 1; i < 2e5 + 50; i++) {
    int a = i;
    int cnt = 0;
    while (a) {
      cnt++;
      a %= __builtin_popcount(a);
    }
    p[i] = cnt;
  }
  int kosuu = 0;
  rep(i, n) {
    if (s[i] == '1')
      kosuu++;
  }
  if (kosuu > 1) {
    vector<int> qp(n), qm(n);
    qp[n - 1] = 1;
    qm[n - 1] = 1;
    if (kosuu == 2)
      qm[n - 1] = 0;
    for (int i = n - 2; i >= 0; i--) {
      qp[i] = qp[i + 1] * 2;
      qp[i] %= (kosuu + 1);
      qm[i] = qm[i + 1] * 2;
      qm[i] %= (kosuu - 1);
    }
    int karisump = 0, karisumm = 0;
    rep(i, n) {
      if (s[i] == '1') {
        karisump += qp[i];
        karisump %= (kosuu + 1);
        karisumm += qm[i];
        karisumm %= (kosuu - 1);
      }
    }
    rep(i, n) {
      int kari;
      if (s[i] == '0') {
        kari = karisump;
        kari += qp[i];
        kari %= (kosuu + 1);
      } else {
        kari = karisumm;
        kari -= qm[i];
        if (kari < 0) {
          kari += (kosuu - 1);
        }
        kari %= (kosuu - 1);
      }
      cout << p[kari] + 1 << endl;
    }
  } else if (kosuu == 1) {
    rep(i, n) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        if (i == n - 1 || s[n - 1] == '1')
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
  } else {
    rep(i, n) { cout << 1 << endl; }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> p(2e5 + 5, 0);
  for (int i = 1; i < 2e5 + 5; i++) {
    int a = i;
    int cnt = 0;
    while (a) {
      cnt++;
      a %= __builtin_popcount(a);
    }
    p[i] = cnt;
  }
  int kosuu = 0;
  rep(i, n) {
    if (s[i] == '1')
      kosuu++;
  }
  vector<int> qp(n), qm(n);
  qp[n - 1] = 1;
  qm[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    qp[i] = qp[i + 1] * 2;
    qp[i] %= (kosuu + 1);
    qm[i] = qm[i + 1] * 2;
    qm[i] %= (kosuu - 1);
  }
  int karisump = 0, karisumm = 0;
  rep(i, n) {
    if (s[i] == '1') {
      karisump += qp[i];
      karisump %= (kosuu + 1);
      karisumm += qm[i];
      karisumm %= (kosuu - 1);
    }
  }
  rep(i, n) {
    int kari;
    if (s[i] == '0') {
      kari = karisump;
      kari += qp[i];
      kari %= (kosuu + 1);
    } else {
      kari = karisumm;
      kari -= qm[i];
      if (kari < 0) {
        kari += (kosuu - 1);
      }
      kari %= (kosuu - 1);
    }
    cout << p[kari] + 1 << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
ll p[N];
int pre[N], bk[N], ans[N];
int n;
char s[N];
int get(int x) {
  int ans = 0;
  while (x)
    x = x % __builtin_popcount(x), ans++;
  return ans;
}
void solve(int x, int sum) {
  if (sum <= 0)
    return;
  p[0] = 1;
  for (int i = 1; i < N; i++)
    p[i] = p[i - 1] * 2 % sum;
  for (int i = 1; i <= n; i++)
    pre[i] = (pre[i - 1] + (s[i] == '1') * p[i - 1]) % sum;
  for (int i = n; i >= 1; i--)
    bk[i] = (bk[i + 1] + (s[i] == '1') * p[i - 1]) % sum;
  for (int i = 1; i <= n; i++)
    if (x == 1 && s[i] == '1') {
      int res = (pre[i - 1] + bk[i + 1]) % sum;
      ans[i] = 1 + get(res);
    } else if (x == 0 && s[i] == '0') {
      int res = (pre[i - 1] + bk[i + 1] + p[i - 1]) % sum;
      ans[i] = 1 + get(res);
    }
}
int main() {
  scanf("%d%s", &n, s + 1);
  reverse(s + 1, s + 1 + n);
  int sum = 0;
  for (int i = 1; i <= n; i++)
    sum += s[i] == '1';
  solve(1, sum - 1);
  solve(0, sum + 1);
  reverse(ans + 1, ans + 1 + n);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char t[200005];
int count(int x, char y[]) {
  int i, sum = 0;
  for (i = 0; i < x; i++) {
    int p = y[i] - '0';
    sum = sum + p * pow(2, x - i - 1);
  }
  return sum;
}
int jishu(int r) {
  int t1 = 0;
  while (r > 0) {
    int n = r % 2;
    if (n == 1)
      t1++;
    r = r / 2;
  }
  return t1;
}
int qout(int x, int y) {
  int sum = 0;
  while (x > 0) {
    x = x % y;
    sum++;
    y = jishu(x);
  }
  return sum;
}
int main() {
  int a;
  scanf("%d", &a);
  memset(t, 0, sizeof(t));
  int i, k = 0;
  scanf("%s", &t);
  for (i = 0; i < a; i++) {
    if (t[i] == '1')
      k++;
  }
  int cnt;
  for (i = 0; i < a; i++) {
    cnt = k;
    if (t[i] == '0') {
      t[i] = '1';
      cnt++;
    } else {
      t[i] = '0';
      cnt--;
    }
    // printf("%d ",cnt);
    int s = count(a, t);
    int s1 = qout(s, cnt);
    printf("%d\\n", s1);
    if (t[i] == '1')
      t[i] = '0';
    else
      t[i] = '1';
  }
  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;
typedef long long ll;
typedef pair<int, int> P;

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;
  string s;
  cin >> n >> 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(bit, 2) {
    int npc = pc;
    bit == 0 ? npc++ : npc--;
    if (npc <= 0)
      continue;
    int r = 0;
    rep(i, n) {
      r = (r * 2) % npc;
      r += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (x[i] == bit) {
        int r2 = r;
        if (bit == 0)
          r2 = (r + k) % npc;
        else
          r2 = (r - k + npc) % npc;
        ans[i] = f(r2) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  rep(i, n) cout << ans[i] << 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;
using P = pair<int, int>;

int main() {
  int n;
  string x, s;
  cin >> n >> x;
  int cnt = 0, cnt2;
  rep(i, x.size()) {
    if (x[i] == '1')
      cnt++;
  }
  rep(i, n) {
    s = x;
    cnt2 = cnt;
    if (x[i] == '1')
      cnt2--, s[i] = '0';
    else
      cnt2++, s[i] = '1';
    ll a = 0;
    rep(j, s.size()) {
      if (s[j] == '1') {
        a *= 2;
        a++;
        a %= cnt2;
      } else {
        a *= 2;
        a %= cnt2;
      }
    }
    int ans = 1, d = a;
    while (1) {
      if (d == 0)
        break;
      int f = __builtin_popcount(d);
      d = d % f;
      ans++;
    }
    cout << ans << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<int> L(N), H(N);
  int m = count(S.begin(), S.end(), '1');
  reverse(S.begin(), S.end());

  int l = m - 1, h = m + 1;
  int sl = 0, sh = 0;
  int x = 1;

  if (l) {
    for (int i = 0; i < N; ++i) {
      L[i] = x;
      if (S[i] == '1')
        sl += x;
      x *= 2, x %= l;
      sl %= l;
    }
  }
  x = 1;
  for (int i = 0; i < N; ++i) {
    H[i] = x;
    if (S[i] == '1')
      sh += x;
    x *= 2, x %= h;
    sh %= h;
  }

  auto rec = [&](auto &&rec, int x, int ret) -> int {
    int cnt = __builtin_popcount(x);
    if (x == 0)
      return ret;
    return rec(rec, x % cnt, ret + 1);
  };

  vector<int> ans(N);
  for (int i = 0; i < N; ++i) {
    if (S[i] == '1') {
      if (l == 0)
        ans[i] = 0;
      else {
        int n = (sl - L[i] + l) % l;
        ans[i] = rec(rec, n, 1);
      }
    } else {
      int n = (sh + H[i]) % h;
      ans[i] = rec(rec, n, 1);
    }
  }

  for (int i = N - 1; i >= 0; --i)
    cout << ans[i] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<int> L(N), H(N);
  int m = count(S.begin(), S.end(), '1');
  reverse(S.begin(), S.end());

  int l = m - 1, h = m + 1;
  int sl = 0, sh = 0;
  int x = 1;
  for (int i = 0; i < N; ++i) {
    L[i] = x;
    if (S[i] == '1')
      sl += x;
    x *= 2, x %= l;
    sl %= l;
  }
  x = 1;
  for (int i = 0; i < N; ++i) {
    H[i] = x;
    if (S[i] == '1')
      sh += x;
    x *= 2, x %= h;
    sh %= h;
  }

  auto rec = [&](auto &&rec, int x, int ret) -> int {
    int cnt = __builtin_popcount(x);
    if (x == 0)
      return ret;
    return rec(rec, x % cnt, ret + 1);
  };

  vector<int> ans(N);
  for (int i = 0; i < N; ++i) {
    if (S[i] == '1') {
      if (l == 0)
        ans[i] = 1;
      else {
        int n = (sl - L[i] + l) % l;
        ans[i] = rec(rec, n, 1);
      }
    } else {
      int n = (sh + H[i]) % h;
      ans[i] = rec(rec, n, 1);
    }
  }

  for (int i = N - 1; i >= 0; --i)
    cout << ans[i] << '\\n';
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int ll

int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

signed main() {
  int n, pop = 0, defm = 0, defp = 0;
  string x;
  cin >> n >> x;
  for (auto i : x)
    pop += i == '1';
  for (int cm = 1, cp = 1, i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      (defp += cp) %= (pop + 1);
      if (pop > 1)
        (defm += cm) %= (pop - 1);
    }
    if (pop > 1)
      (cm *= 2) %= (pop - 1);
    (cp *= 2) %= (pop + 1);
  };
  for (int pos = 0; pos < n; ++pos) {
    int k = 1, p, d;

    p = pop + (x[pos] == '0' ? 1 : -1);
    if (p == 0) {
      cout << 0 << endl;
      continue;
    }
    d = (x[pos] == '0' ? (defp + power(2, n - 1 - pos, p)) % p
                       : (defm - power(2, n - 1 - pos, p) + p) % p);
    if (d < 0) {
      d = -d;
      d = p - d % p;
    }
    d %= p;
    while (d > 0) {
      int dt = d;
      p = 0;
      while (dt > 0) {
        p += dt % 2;
        dt /= 2;
      }
      d %= p;
      ++k;
    }
    cout << k << endl;
  }
}


Error: TLE

Fixed submission:

#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 main() {
  int n;
  string x;
  cin >> n >> x;
  vector<int> res;
  for (int pos = 0; pos < n; ++pos) {
    int k = 1;
    string y = x;
    y[pos] = '0' + 1 - (y[pos] - '0');
    for (; k < 10; ++k) {
      int pop = 0;
      for (auto i : y)
        pop += i == '1';
      if (pop == 0 && k == 1) {
        k = 0;
        break;
      }
      int t = 0;
      for (int i = y.size() - 1, c = 1; i >= 0; --i) {
        if (y[i] == '1')
          (t += c) %= pop;
        (c *= 2) %= pop;
      }
      if (t == 0) {
        break;
      }
      string tmp;
      while (t > 0) {
        tmp = (t % 2 ? "1" : "0") + tmp;
        t /= 2;
      }
      y = tmp;
    }
    res.push_back(k);
  }
  for (int i : res)
    cout << i << endl;
}