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

#include <bits/stdc++.h>

using namespace std;

// FILE *fi=freopen("1.txt","r",stdin);

int n, a[200005];
long long l = 0, d[2];

void nhap() {
  scanf("%d", &n);
  for (int i = n; i > 0; --i) {
    char x;
    cin >> x;
    a[i] = (x == '1');
    l += a[i];
  }
}

long long moe(long long a, long long b, long long c) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return moe((a * a) % c, b / 2, c);
  return (a * moe((a * a) % c, b / 2, c)) % c;
}

int solve(long long x) {
  if (x == -1)
    return 0;
  int dem = 1;
  while (x) {
    x %= __builtin_popcount(x);
    dem++;
  }
  return dem;
}

void process() {
  for (int i = 1; i <= n; ++i) {
    if (l > 1)
      d[0] = (d[0] + moe(1LL * 2, i - 1, l - 1) * a[i]) % (l - 1);
    d[1] = (d[1] + moe(1LL * 2, i - 1, l + 1) * a[i]) % (l + 1);
  }
  for (int i = n; i > 0; --i) {
    long long x;
    if (a[i]) {
      if (l > 1)
        x = ((l - 1) * (l - 1) - moe(1LL * 2, i - 1, l - 1) + d[0]) % (l - 1);
      else
        x = -1;
    } else {
      x = (d[1] + moe(1LL * 2, i - 1, l + 1)) % (l + 1);
    }
    cout << solve(x) << "\\n";
  }
}

int main() {
  nhap();
  process();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// FILE *fi=freopen("1.txt","r",stdin);

int n, a[200005];
long long l = 0, d[2];

void nhap() {
  scanf("%d", &n);
  for (int i = n; i > 0; --i) {
    char x;
    cin >> x;
    a[i] = (x == '1');
    l += a[i];
  }
}

long long moe(long long a, long long b, long long c) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return moe((a * a) % c, b / 2, c);
  return (a * moe((a * a) % c, b / 2, c)) % c;
}

int solve(long long x) {
  int dem = 1;
  while (x) {
    x %= __builtin_popcount(x);
    dem++;
  }
  return dem;
}

void process() {
  for (int i = 1; i <= n; ++i) {
    d[0] = (d[0] + moe(1LL * 2, i - 1, l - 1) * a[i]) % (l - 1);
    d[1] = (d[1] + moe(1LL * 2, i - 1, l + 1) * a[i]) % (l + 1);
  }
  for (int i = n; i > 0; --i) {
    long long x;
    if (a[i]) {
      x = ((l - 1) * (l - 1) - moe(1LL * 2, i - 1, l - 1) + d[0]) % (l - 1);
    } else {
      x = (d[1] + moe(1LL * 2, i - 1, l + 1)) % (l + 1);
    }
    cout << solve(x) << "\\n";
  }
}

int main() {
  nhap();
  process();
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <limits.h>

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(x) x.begin(), x.end()
using namespace std;
using P = pair<int, int>;
using ll = long long;
template <typename T = long long> vector<T> make_v(size_t a) {
  return vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  reverse(all(x));
  int p = count(all(x), '1');
  vector<int> pow1(n, 1); // 2^i(mod popcount(x)-1)
  vector<int> pow2(n, 1); // 2^i(mod popcount(x)+1)
  for (int i = 1; i < n; i++) {
    if (p != 1)
      pow1[i] = pow1[i - 1] * 2 % (p - 1);
    pow2[i] = pow2[i - 1] * 2 % (p + 1);
  }
  int x1 = 0, x2 = 0;
  rep(i, n) {
    if (x[i] == '1') {
      x1 += pow1[i];
      x2 += pow2[i];
      if (p != 1)
        x1 %= p - 1;
      x2 %= p + 1;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    int now, cnt = 1;
    if (x[i] == '0') {
      now = x2 + pow2[i];
      now %= p + 1;
    } else {
      if (p == 1) {
        cout << 0 << endl;
        continue;
      }
      now = x1 - pow1[i];
      if (p != 1)
        now += p - 1;
      now %= p - 1;
    }
    while (now > 0) {
      now %= __builtin_popcount(now);
      cnt++;
    }
    cout << cnt << endl;
  }
}


Error: 0

Fixed submission:

#include <limits.h>

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(x) x.begin(), x.end()
using namespace std;
using P = pair<int, int>;
using ll = long long;
template <typename T = long long> vector<T> make_v(size_t a) {
  return vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  reverse(all(x));
  int p = count(all(x), '1');
  vector<int> pow1(n, 1); // 2^i(mod popcount(x)-1)
  vector<int> pow2(n, 1); // 2^i(mod popcount(x)+1)
  for (int i = 1; i < n; i++) {
    if (p != 1)
      pow1[i] = pow1[i - 1] * 2 % (p - 1);
    pow2[i] = pow2[i - 1] * 2 % (p + 1);
  }
  int x1 = 0, x2 = 0;
  rep(i, n) {
    if (x[i] == '1') {
      x1 += pow1[i];
      x2 += pow2[i];
      x1 %= p - 1;
      x2 %= p + 1;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    int now, cnt = 1;
    if (x[i] == '0') {
      now = x2 + pow2[i];
      now %= p + 1;
    } else {
      if (p == 1) {
        cout << 0 << endl;
        continue;
      }
      now = x1 - pow1[i];
      now += p - 1;
      now %= p - 1;
    }
    while (now > 0) {
      now %= __builtin_popcount(now);
      cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll, ll>
int get_ans(int a) {
  int ans = 0;
  while (a) {
    ++ans;
    int b = a;
    int cnt = 0;
    while (b) {
      if (b % 2)
        ++cnt;
      b = b / 2;
    }
    a = a % cnt;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int cnt = 0;
  string s;
  cin >> s;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '1')
      ++cnt;
  ll pow[n][2];
  pow[0][0] = pow[0][1] = 1;
  for (int i = 1; i < n; i++) {
    if (cnt != 1)
      pow[i][0] = (pow[i - 1][0] * 2) % (cnt - 1);

    pow[i][1] = (pow[i - 1][1] * 2) % (cnt + 1);
  }
  ll ans[2] = {0};
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt != 1)
        ans[0] = (ans[0] * 2 + 1) % (cnt - 1);

      ans[1] = (ans[1] * 2 + 1) % (cnt + 1);
    } else {
      if (cnt != 1)
        ans[0] = (ans[0] * 2) % (cnt - 1);

      ans[1] = (ans[1] * 2) % (cnt + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt == 1) {
        cout << 0 << "\\n";
      } else {
        int a = ans[0];
        a = (a - pow[n - 1 - i][0] + (cnt - 1)) % (cnt - 1);
        cout << 1 + get_ans(a) << "\\n";
      }
    } else {
      int a = ans[1];
      a = (a + pow[n - 1 - i][1]) % (cnt + 1);
      cout << 1 + get_ans(a) << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll, ll>
int get_ans(int a) {
  int ans = 0;
  while (a) {
    ++ans;
    int b = a;
    int cnt = 0;
    while (b) {
      if (b % 2)
        ++cnt;
      b = b / 2;
    }
    a = a % cnt;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int cnt = 0;
  string s;
  cin >> s;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '1')
      ++cnt;
  ll pow[n][2];
  pow[0][0] = pow[0][1] = 1;
  for (int i = 1; i < n; i++) {
    pow[i][0] = (pow[i - 1][0] * 2) % (cnt - 1);
    pow[i][1] = (pow[i - 1][1] * 2) % (cnt + 1);
  }
  ll ans[2] = {0};
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      ans[0] = (ans[0] * 2 + 1) % (cnt - 1);
      ans[1] = (ans[1] * 2 + 1) % (cnt + 1);
    } else {
      ans[0] = (ans[0] * 2) % (cnt - 1);
      ans[1] = (ans[1] * 2) % (cnt + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt == 1) {
        cout << 0 << "\\n";
      } else {
        int a = ans[0];
        a = (a - pow[n - 1 - i][0] + (cnt - 1)) % (cnt - 1);
        cout << 1 + get_ans(a) << "\\n";
      }
    } else {
      int a = ans[1];
      a = (a + pow[n - 1 - i][1]) % (cnt + 1);
      cout << 1 + get_ans(a) << "\\n";
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

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

int f(int n) { return n % __builtin_popcount(n); }

int main() {
  int N;
  string S;
  cin >> N >> S;
  int cnt = count(S.begin(), S.end(), '1');
  int mod1 = cnt - 1;
  int mod2 = cnt + 1;
  reverse(S.begin(), S.end());
  int now1 = 1, sum1 = 0;
  if (mod1) {
    for (int i = 0; i < N; i++) {
      sum1 += (S.at(i) - '0') * now1;
      sum1 %= mod1;
      now1 *= 2;
      now1 %= mod1;
    }
  }
  int now2 = 1, sum2 = 0;
  for (int i = 0; i < N; i++) {
    sum2 += (S.at(i) - '0') * now2;
    sum2 %= mod2;
    now2 *= 2;
    now2 %= mod2;
  }
  reverse(S.begin(), S.end());
  for (int i = 0; i < N; i++) {
    if (S.at(i) == '1') {
      if (cnt == 1) {
        cout << 0 << "\\n";
        continue;
      }
      int tmp = sum1;
      tmp -= mpow(2, N - i - 1, mod1);
      while (tmp < 0)
        tmp += mod1;
      int ans = 1;
      while (tmp)
        tmp = f(tmp), ans++;
      cout << ans << "\\n";
    } else {
      int tmp = sum2;
      tmp += mpow(2, N - i - 1, mod2);
      tmp %= mod2;
      int ans = 1;
      while (tmp)
        tmp = f(tmp), ans++;
      cout << ans << "\\n";
    }
  }
}

Error: 0

Fixed submission:

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

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

long f(long n) { return n % __builtin_popcount(n); }

int main() {
  long N;
  string S;
  cin >> N >> S;
  long cnt = count(S.begin(), S.end(), '1');
  long mod1 = cnt - 1;
  long mod2 = cnt + 1;
  reverse(S.begin(), S.end());
  long now1 = 1, sum1 = 0;
  for (long i = 0; i < N; i++) {
    sum1 += (S.at(i) - '0') * now1;
    sum1 %= mod1;
    now1 *= 2;
    now1 %= mod1;
  }
  long now2 = 1, sum2 = 0;
  for (long i = 0; i < N; i++) {
    sum2 += (S.at(i) - '0') * now2;
    sum2 %= mod2;
    now2 *= 2;
    now2 %= mod2;
  }
  reverse(S.begin(), S.end());
  for (long i = 0; i < N; i++) {
    if (S.at(i) == '1') {
      long tmp = sum1;
      tmp -= mpow(2, N - i - 1, mod1);
      while (tmp < 0)
        tmp += mod1;
      long ans = 1;
      while (tmp)
        tmp = f(tmp), ans++;
      cout << ans << "\\n";
    } else {
      long tmp = sum2;
      tmp += mpow(2, N - i - 1, mod2);
      tmp %= mod2;
      long ans = 1;
      while (tmp)
        tmp = f(tmp), ans++;
      cout << ans << "\\n";
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
ll n;
string s;
ll b;
ll p, m;
ll ans[200005];
int main(void) {
  cin >> n >> s;
  if (s == "1") {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      b++;
  }
  ll k = 1, l = 1;
  for (int i = 0; i < n; i++) {
    p = (p + (s[n - 1 - i] - '0') * k) % (b + 1);
    if (b != 1) {
      m = (m + (s[n - 1 - i] - '0') * l) % (b - 1);
      l = (l * 2) % (b - 1);
    }
    k = (k * 2) % (b + 1);
  }
  // cout<<p<<" "<<m<<endl;
  k = 1, l = 1;
  ll c;
  for (int i = n - 1; i >= 0; i--) {
    c = 1;
    if (s[i] == '0') {
      ll t = (p + k) % (b + 1);
      // cout<<i<<" "<<t<<endl;
      while (t != 0) {
        ll a = 0;
        ll co = t;
        while (co > 0) {
          a += (co % 2);
          co = co / 2;
        }
        t = t % a;
        c++;
      }
    } else {
      if (b == 1) {
        ans[i] = 0;
        continue;
      }
      ll T = (m - l) % (b - 1);
      // cout<<i<<" "<<T<<endl;
      if (T < 0) {
        T += (b - 1);
      }
      while (T != 0) {
        ll A = 0;
        ll CO = T;
        while (CO > 0) {
          A += (CO % 2);
          CO = CO / 2;
        }
        T = T % A;
        c++;
      }
    }
    ans[i] = c;
    k = (k * 2) % (b + 1);
    if (b != 1) {
      l = (l * 2) % (b - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
ll n;
string s;
ll b;
ll p, m;
ll ans[200005];
int main(void) {
  cin >> n >> s;
  if (s == "1") {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      b++;
  }
  ll k = 1, l = 1;
  for (int i = 0; i < n; i++) {
    p = (p + (s[n - 1 - i] - '0') * k) % (b + 1);
    if (b != 1) {
      m = (m + (s[n - 1 - i] - '0') * l) % (b - 1);
      l = (l * 2) % (b - 1);
    }
    k = (k * 2) % (b + 1);
  }
  // cout<<p<<" "<<m<<endl;
  k = 1, l = 1;
  ll c;
  for (int i = n - 1; i >= 0; i--) {
    c = 1;
    if (s[i] == '0') {
      ll t = (p + k) % (b + 1);
      // cout<<i<<" "<<t<<endl;
      while (t != 0) {
        ll a = 0;
        ll co = t;
        while (co > 0) {
          a += (co % 2);
          co = co / 2;
        }
        t = t % a;
        c++;
      }
    } else {
      if (b == 1) {
        ans[i] = 0;
        continue;
      }
      ll T = (m - l) % (b - 1);
      // cout<<i<<" "<<T<<endl;
      if (T < 0) {
        T += (b - 1);
      }
      while (T != 0) {
        ll A = 0;
        ll CO = T;
        while (CO > 0) {
          A += (CO % 2);
          CO = CO / 2;
        }
        T = T % A;
        c++;
      }
    }
    ans[i] = c;
    k = (k * 2) % (b + 1);
    l = (l * 2) % (b - 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;
#define rep(i, a) for (int i = 0; i < (a); i++)
const ll MOD = 1000000007;
// const ll MOD=998244353;

ll pp[202020], mm[202020];

int solve(int X) {
  if (X == 0)
    return 0;
  int pc = __builtin_popcount(X);
  return (solve(X % pc) + 1);
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int pc = 0;
  rep(i, N) pc += (S[i] == '1');
  ll m = 0, p = 0;
  pp[0] = 1;
  mm[0] = 1;
  rep(i, N) {
    p = (p + pp[i] * (S[N - 1 - i] == '1')) % (pc + 1);
    pp[i + 1] = pp[i] * 2 % (pc + 1);
    if (pc > 1)
      m = (m + mm[i] * (S[N - 1 - i] == '1')) % (pc - 1);
    if (pc > 1)
      mm[i + 1] = mm[i] * 2 % (pc - 1);
  }
  rep(i, N) {
    int tmp = 0;
    if (S[i] == '1') {
      if (pc > 1)
        tmp = (m - mm[N - 1 - i] + (pc - 1)) % (pc - 1);
      else {
        cout << "0\\n";
        continue;
      }
    } else {
      tmp = (p + pp[N - 1 - i]) % (pc + 1);
    }
    cout << solve(tmp) + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a) for (int i = 0; i < (a); i++)
const ll MOD = 1000000007;
// const ll MOD=998244353;

ll pp[202020], mm[202020];

int solve(int X) {
  if (X == 0)
    return 0;
  int pc = __builtin_popcount(X);
  return (solve(X % pc) + 1);
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int pc = 0;
  rep(i, N) pc += (S[i] == '1');
  ll m = 0, p = 0;
  pp[0] = 1;
  mm[0] = 1;
  rep(i, N) {
    p = (p + pp[i] * (S[N - 1 - i] == '1')) % (pc + 1);
    pp[i + 1] = pp[i] * 2 % (pc + 1);
    if (pc > 1)
      m = (m + mm[i] * (S[N - 1 - i] == '1')) % (pc - 1);
    if (pc > 1)
      mm[i + 1] = mm[i] * 2 % (pc - 1);
  }
  rep(i, N) {
    int tmp;
    if (S[i] == '1') {
      tmp = (m - mm[N - 1 - i] + (pc - 1)) % (pc - 1);
    } else {
      tmp = (p + pp[N - 1 - i]) % (pc + 1);
    }
    cout << solve(tmp) + 1 << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;
int n;
char s[nax];
int f[nax];
int p2[nax][2];
vector<int> ans;

int main() {
  scanf("%d", &n);
  scanf("%s", s);
  reverse(s, s + n);

  for (int i = 0; i < nax; ++i) {
    int x = i;
    while (x > 0) {
      int p = __builtin_popcount(x);
      x %= p;
      ++f[i];
    }
  }

  int first = 0;

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

  ++first;
  int second = first - 2;

  p2[0][0] = p2[0][1] = 1;

  for (int i = 1; i < nax; ++i) {
    p2[i][0] = p2[i - 1][0] * 2 % first;
    if (second > 0)
      p2[i][1] = p2[i - 1][1] * 2 % second;
  }

  int first_sum = 0;
  int second_sum = 0;

  for (int i = 0; i < nax; ++i) {
    if (s[i] == '1') {
      first_sum += p2[i][0];
      second_sum += p2[i][1];
      first_sum %= first;
      if (second != 0)
        second_sum %= second;
    }
  }

  for (int i = 0; i < n; ++i) {
    int x;
    if (s[i] == '0') {
      x = (first_sum + p2[i][0]) % first;
      ans.push_back(f[x] + 1);
    } else {
      if (second == 0) {
        ans.push_back(0);
      } else {
        x = ((second_sum - p2[i][1]) % second + second) % second;
        ans.push_back(f[x] + 1);
      }
    }
  }

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

  for (int i : ans)
    printf("%d\\n", i);
}
/*
    Good Luck
        -Lucina
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;
int n;
char s[nax];
int f[nax];
int p2[nax][2];
vector<int> ans;

int main() {
  scanf("%d", &n);
  scanf("%s", s);
  reverse(s, s + n);

  for (int i = 0; i < nax; ++i) {
    int x = i;
    while (x > 0) {
      int p = __builtin_popcount(x);
      x %= p;
      ++f[i];
    }
  }

  int first = 0;

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

  ++first;
  int second = first - 2;

  p2[0][0] = p2[0][1] = 1;

  for (int i = 1; i < nax; ++i) {
    p2[i][0] = p2[i - 1][0] * 2 % first;
    if (second > 0)
      p2[i][1] = p2[i - 1][1] * 2 % second;
  }

  int first_sum = 0;
  int second_sum = 0;

  for (int i = 0; i < nax; ++i) {
    if (s[i] == '1') {
      first_sum += p2[i][0];
      second_sum += p2[i][1];
      first_sum %= first;
      if (second > 0)
        second_sum %= second;
    }
  }

  for (int i = 0; i < n; ++i) {
    int x;
    if (s[i] == '0') {
      x = (first_sum + p2[i][0]) % first;
      ans.push_back(f[x] + 1);
    } else {
      x = ((second_sum - p2[i][1]) % second + second) % second;
      ans.push_back(f[x] + 1);
    }
    //     printf("%d ", x);
  }
  // puts("");

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

  for (int i : ans)
    printf("%d\\n", i);
}
/*
    Good Luck
        -Lucina
*/
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef long long li;
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}
// Container コンテナ型, map以外
template <template <class...> class Ctn, class T>
std::ostream &operator<<(std::ostream &os, const Ctn<T> &v) {
  auto itr = v.begin();
  while (itr != v.end()) {
    if (itr != v.begin())
      cout << " ";
    cout << *(itr++);
  }
  return os;
}

int rec(li x) { return x % __builtin_popcount(x); }

int f(li x) {
  static vector<int> ans(x + 2, -1);
  if (x == 0)
    return 0;
  return f(rec(x)) + 1;
}

li div(string s, int a) {
  int n = s.size(), r = 0;
  rep(i, n) {
    r *= 2;
    r += (s[i] - '0');
    r %= a;
  }
  return r;
}

int main() {

  int n;
  cin >> n;
  string x;
  cin >> x;
  int ppc = 0;
  rep(i, n) if (x[i] == '1') ppc++;
  int flag = -1;
  if (ppc == 1)
    rep(i, n) if (x[i] == '1') flag = i;
  vector<int> pp(n, 1), p(n, 1), pm(n, 1);
  if (ppc - 1 == 1)
    pm[0] = 0;
  repa(i, 1, n) {
    pp[i] = pp[i - 1] * 2 % (ppc + 1);
    if (ppc - 1)
      pm[i] = pm[i - 1] * 2 % (ppc - 1);
  }
  int sp = 0, s = 0, sm = 0;
  rep(i, n) {
    if (x[i] == '1') {
      (sp += pp[n - 1 - i]) %= ppc + 1;
      if (ppc - 1)
        (sm += pm[n - 1 - i]) %= ppc - 1;
    }
  }
  if (df)
    print(ppc + 1, ":", pp);
  if (df)
    print(ppc - 1, ":", pm);
  if (df)
    print(sp, sm);
  rep(i, n) {
    if (flag == i) {
      print(0);
      continue;
    }
    int t = x[i] - '0';
    x[i] = t ^ 1 + '0';
    li a;
    if (t == 0) {
      a = (sp + pp[n - i - 1]) % (ppc + 1);
    } else {
      a = (sm - pm[n - 1 - i] + ppc - 1) % (ppc - 1);
    }
    if (df)
      print("a", a);
    print(1 + f(a));
    x[i] = t + '0';
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long li;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

int rec(li x) { return x % __builtin_popcount(x); }

int f(li x) {
  if (x == 0)
    return 0;
  return f(rec(x)) + 1;
}

li div(string s, int a) {
  int n = s.size(), r = 0;
  rep(i, n) {
    r *= 2;
    r += (s[i] - '0');
    r %= a;
  }
  return r;
}

int main() {

  // rep(i,40){
  //   print(i,f(i));
  // }
  // return 0;

  int n;
  cin >> n;
  string x;
  cin >> x;
  int ppc = 0;
  rep(i, n) if (x[i] == '1') ppc++;
  int flag = -1;
  if (ppc == 1)
    rep(i, n) if (x[i] == '1') flag = i;
  rep(i, n) {
    if (flag == i) {
      print(0);
      continue;
    }
    int t = x[i] - '0';
    x[i] = t ^ 1 + '0';
    li a = div(x, ppc + 1 - 2 * t);
    print(1 + f(a));
    x[i] = t + '0';
  }
}