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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ve vector
#define chmin(x, y) x = min(x, y)

int dfs(ve<int> &f, int i) {
  if (f[i] != -1)
    return f[i];
  int pc = __builtin_popcount(i);
  int nex = i % pc;
  return f[i] = dfs(f, nex) + 1;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }
  if (cnt == 1) {
    if (s[n - 1] == '1') {
      rep(i, -1) { cout << 2 << endl; }
      cout << 0 << endl;
      return 0;
    } else {
      rep(i, n - 1) {
        if (s[i] == '1') {
          cout << 0 << endl;
        } else {
          cout << 1 << endl;
        }
      }
      cout << 2 << endl;
      return 0;
    }
  }

  reverse(s.begin(), s.end());
  ve<ve<int>> mk(2, ve<int>(n));
  mk[0][0] = 1 % (cnt - 1);
  mk[1][0] = 1 % (cnt + 1);
  rep(i, n - 1) {
    (mk[0][i + 1] = mk[0][i] * 2) %= cnt - 1;
    (mk[1][i + 1] = mk[1][i] * 2) %= cnt + 1;
  }
  ve<int> su(2);
  rep(i, n) {
    if (s[i] == '1') {
      rep(j, 2) { su[j] += mk[j][i]; }
      su[0] %= cnt - 1;
      su[1] %= cnt + 1;
    }
  }

  ve<int> f(500010, -1);

  f[0] = 0;
  f[1] = 1;

  ve<int> ans(n);
  rep(i, n) {
    int nex;
    if (s[i] == '1') {
      nex = su[0] - mk[0][i];
      while (nex < 0)
        nex += cnt - 1;
    } else {
      nex = su[1] + mk[1][i];
      nex %= cnt + 1;
    }
    ans[i] = dfs(f, nex) + 1;
  }
  reverse(ans.begin(), ans.end());

  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<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ve vector
#define chmin(x, y) x = min(x, y)

int dfs(ve<int> &f, int i) {
  if (f[i] != -1)
    return f[i];
  int pc = __builtin_popcount(i);
  int nex = i % pc;
  return f[i] = f[nex] + 1;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }
  if (cnt == 1) {
    rep(i, n) {
      if (s[i] == '1')
        cout << 0 << endl;
      else
        cout << 1 << endl;
    }
    return 0;
  }

  reverse(s.begin(), s.end());
  ve<ve<int>> mk(2, ve<int>(n));
  mk[0][0] = 1 % (cnt - 1);
  mk[1][0] = 1 % (cnt + 1);
  rep(i, n - 1) {
    (mk[0][i + 1] = mk[0][i] * 2) %= cnt - 1;
    (mk[1][i + 1] = mk[1][i] * 2) %= cnt + 1;
  }
  ve<int> su(2);
  rep(i, n) {
    if (s[i] == '1') {
      rep(j, 2) { su[j] += mk[j][i]; }
    }
  }
  su[0] %= cnt - 1;
  su[1] %= cnt + 1;

  ve<int> f(200010, -1);

  f[0] = 0;
  f[1] = 1;

  ve<int> ans(n);
  rep(i, n) {
    int nex;
    if (s[i] == '1') {
      nex = su[0] - mk[0][i];
      if (nex < 0)
        nex += cnt - 1;
    } else {
      nex = su[1] + mk[1][i];
      nex %= cnt + 1;
    }
    ans[i] = dfs(f, nex) + 1;
  }
  reverse(ans.begin(), ans.end());

  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using vi = vector<int>;
using pii = 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;
  vi x(n);
  rep(i, n) { x[i] = s[i] - '0'; }
  int pc = 0;
  rep(i, n) { pc += x[i]; }
  vi ans(n);
  rep(bit, 2) {
    int npc = pc;
    if (bit == 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--) {
      int r = r0;
      if (x[i] == bit) {
        if (bit == 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: TLE

Fixed submission:

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

int popcount(int n) {
  int c = 0;
  while (n >= 1) {
    if (n % 2 == 1)
      c++;
    n /= 2;
  }
  return c;
}

int f(string s) {
  int n = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      n += (int)pow(2, s.size() - (i + 1));
  }
  // cout << n << endl;
  int ans = 0;
  while (n != 0) {
    n %= popcount(n);
    ans++;
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  string t = s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      s[i] = '1';
    else
      s[i] = '0';
    cout << f(s) << endl;
    s = t;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

UL N;
UL P;
char X[200001];
UL M[200001];
UL D[200001];
UL ans[200000] = {};
UL B;

UL popcount(UL x) {
  x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f);
  x = (x & 0x00ff00ff) + ((x >> 8) & 0x00ff00ff);
  x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff);
  return x;
}

void Calc(UL m) {
  M[0] = 1;
  for (UL i = 1; i <= N; i++)
    M[i] = M[i - 1] * 2 % m;
  D[0] = 0;
  for (UL i = 1; i <= N; i++) {
    D[i] = D[i % popcount(i)] + 1;
  }
  B = 0;
  for (UL i = N - 1; i != ~0u; i--) {
    B = B * 2 + (X[i] - '0');
    B %= m;
  }
}

int main() {
  scanf("%u", &N);
  scanf("%s", X);
  reverse(X, X + N);
  P = 0;
  rep(i, N) if (X[i] == '1') P++;

  Calc(P + 1);
  rep(i, N) {
    if (X[i] == '0') {
      UL b = (B + M[i]) % (P + 1);
      ans[N - 1 - i] = D[b] + 1;
    }
  }
  if (P != 1) {
    Calc(P - 1);
    rep(i, N) {
      if (X[i] == '1') {
        UL b = (B + (P - 1) - M[i]) % (P - 1);
        ans[N - 1 - i] = D[b] + 1;
      }
    }
  }

  rep(i, N) printf("%u\\n", ans[i]);

  return 0;
}


Error: 0

Fixed submission:

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

UL N;
UL P;
char X[200001];
UL M[200001];
UL D[200001];
UL ans[200000];
UL B;

UL popcount(UL x) {
  x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f);
  x = (x & 0x00ff00ff) + ((x >> 8) & 0x00ff00ff);
  x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff);
  return x;
}

void Calc(UL m) {
  M[0] = 1;
  for (UL i = 1; i <= N; i++)
    M[i] = M[i - 1] * 2 % m;
  D[0] = 0;
  for (UL i = 1; i <= N; i++) {
    D[i] = D[i % popcount(i)] + 1;
  }
  B = 0;
  for (UL i = N - 1; i != ~0u; i--) {
    B = B * 2 + (X[i] - '0');
    B %= m;
  }
}

int main() {
  scanf("%u", &N);
  scanf("%s", X);
  reverse(X, X + N);
  P = 0;
  rep(i, N) if (X[i] == '1') P++;

  Calc(P + 1);
  rep(i, N) {
    if (X[i] == '0') {
      UL b = (B + M[i]) % (P + 1);
      ans[N - 1 - i] = D[b] + 1;
    }
  }
  if (P != 0) {
    Calc(P - 1);
    rep(i, N) {
      if (X[i] == '1') {
        UL b = (B + (P - 1) - M[i]) % (P - 1);
        ans[N - 1 - i] = D[b] + 1;
      }
    }
  }

  rep(i, N) printf("%u\\n", ans[i]);

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

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

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

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

  const int N = 2e5 + 5;

  vector<int> dp(N, 0);
  dp[0] = 0;

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

  int total = 0;
  int n;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      ++total;
    }
  }

  if (total == 0) {
    for (int i = 0; i < n; ++i) {
      cout << 1 << '\\n';
    }
    return 0;
  }

  vector<ll> mods(2, 0);
  for (int i = 0; i < n; ++i) {
    int dig = s[i] - '0';
    mods[1] *= 2;
    mods[0] %= (total + 1);
    mods[1] += dig;
    mods[1] %= (total + 1);
    if (total == 1) {
      continue;
    }
    mods[0] *= 2;
    mods[0] %= (total - 1);
    mods[0] += dig;
    mods[0] %= (total - 1);
  }

  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      ll val = (mods[1] + binpow(2, n - i - 1, total + 1)) % (total + 1);
      cout << 1 + dp[val] << endl;
    } else {
      if (total == 1) {
        cout << 0 << endl;
        continue;
      }
      ll val =
          (mods[0] - binpow(2, n - i - 1, total - 1) + total - 1) % (total - 1);
      if (val < 0) {
        val += (total - 1);
      }
      val %= (total - 1);
      cout << 1 + dp[val] << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

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

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

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

  const int N = 2e5 + 5;

  vector<int> dp(N, 0);
  dp[0] = 0;

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

  int total = 0;
  int n;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      ++total;
    }
  }

  if (total == 0) {
    for (int i = 0; i < n; ++i) {
      cout << 1 << '\\n';
    }
    return 0;
  }

  vector<ll> mods(2, 0);
  for (int i = 0; i < n; ++i) {
    int dig = s[i] - '0';
    mods[0] *= 2;
    mods[0] %= (total - 1);
    mods[0] += dig;
    mods[0] %= (total - 1);
    mods[1] *= 2;
    mods[0] %= (total + 1);
    mods[1] += dig;
    mods[1] %= (total + 1);
  }
  mods[0] %= (total - 1);
  mods[1] %= (total + 1);

  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      ll val = (mods[1] + binpow(2, n - i - 1, total + 1)) % (total + 1);
      cout << 1 + dp[val] << endl;
    } else {
      ll val =
          (mods[0] - binpow(2, n - i - 1, total - 1) + total - 1) % (total - 1);
      if (val < 0) {
        val += (total - 1);
      }
      val %= (total - 1);
      cout << 1 + dp[val] << endl;
    }
  }

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

#include <bits/stdc++.h>
#define ll long long int
#define loop(i, a, b) for (i = a; i < b; i++)
#define mod 100000007
#define INF 1000000000000
#define pb push_back
#define lim 200001
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, n, cnt = 0;

  cin >> n;
  vector<ll> ans(n, 1);
  char x[n + 1];

  cin >> x;
  ll power1[lim + 1], power2[lim + 1];

  power1[0] = 1;
  power2[0] = 1;
  loop(i, 0, n) if (x[i] == '1') cnt++;

  loop(i, 1, lim + 1) {
    if (cnt > 1)
      power1[i] = (power1[i - 1] * 2) % (cnt - 1);
    power2[i] = (power2[i - 1] * 2) % (cnt + 1);
  }

  ll val1 = 0, val2 = 0;
  loop(i, 0, n) {
    if (cnt > 1)
      val1 = (val1 + (x[i] - 48) * power1[n - i - 1]) % (cnt - 1);
    val2 = (val2 + (x[i] - 48) * power2[n - i - 1]) % (cnt + 1);
  }
  // cout<<val1<<" "<<val2<<endl;

  loop(i, 0, n) {
    if (x[i] == '1') {

      if (cnt > 1) {
        ll curr =
            ((val1 - power1[n - 1 - i]) % (cnt - 1) + (cnt - 1)) % (cnt - 1);
        while (curr) {
          curr = curr % __builtin_popcountll(curr);
          ans[i]++;
        }
      } else
        ans[i] = 0;
    }

    else {
      ll curr = (val2 + power2[n - 1 - i]) % (cnt + 1);
      while (curr) {
        curr = curr % __builtin_popcountll(curr);
        ans[i]++;
      }
    }
  }

  loop(i, 0, n) cout << ans[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll unsigned long long int
#define loop(i, a, b) for (i = a; i < b; i++)
#define mod 100000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, n, cnt = 0;

  cin >> n;
  vector<ll> ans(n, 1);
  char x[n + 1];

  cin >> x;
  ll power1[30], power2[30];

  power1[0] = 1;
  power2[0] = 1;
  loop(i, 0, n) if (x[i] == '1') cnt++;

  loop(i, 1, 30) {
    if (cnt > 1)
      power1[i] = (power1[i - 1] * 2) % (cnt - 1);
    power2[i] = (power2[i - 1] * 2) % (cnt + 1);
  }

  ll val1 = 0, val2 = 0;
  loop(i, 0, n) {
    if (cnt > 1)
      val1 = (val1 + (x[i] - 48) * power1[n - i - 1]) % (cnt - 1);
    val2 = (val2 + (x[i] - 48) * power2[n - i - 1]) % (cnt + 1);
  }
  // cout<<val1<<" "<<val2<<endl;

  loop(i, 0, n) {
    if (x[i] == '1') {

      if (cnt > 1) {
        ll curr =
            ((val1 - power1[n - 1 - i]) % (cnt - 1) + (cnt - 1)) % (cnt - 1);
        while (curr) {
          curr = curr % __builtin_popcountll(curr);
          ans[i]++;
        }
      } else
        ans[i] = 0;
    }

    else {
      ll curr = (val2 + power2[n - 1 - i]) % (cnt + 1);
      while (curr) {
        curr = curr % __builtin_popcountll(curr);
        ans[i]++;
      }
    }
  }

  loop(i, 0, n) cout << ans[i] << endl;

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <unordered_map>

using namespace std;
typedef long long ll;
const ll INF = 1000000007;
const ll mod = 998244353;

vector<ll> dp(2 * 100000 + 1, -1);
vector<ll> popcount(2 * 100000 + 1, 0);

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() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> x(n);
  // 0,1変換
  for (ll i = 0; i < n; i++) {
    x[i] = s[i] - '0';
  }
  // xのpopcount
  ll pc = 0;
  for (ll i = 0; i < n; i++) {
    pc += x[i];
  }
  vector<ll> ans(n);
  for (ll b = 0; b < 2; b++) {
    ll npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    if (npc <= 0)
      continue;
    // xのnpc
    ll r0 = 0;
    for (ll i = 0; i < n; i++) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    ll k = 1;
    for (ll 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;
    }
  }

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


Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <unordered_map>

using namespace std;
typedef long long ll;
const ll INF = 1000000007;
const ll mod = 998244353;

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() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> x(n);
  for (ll i = 0; i < n; i++) {
    x[i] = s[i] - '0';
  }
  ll pc = 0;
  for (ll i = 0; i < n; i++) {
    pc += x[i];
  }
  vector<ll> ans(n);
  for (ll b = 0; b < 2; b++) {
    ll npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    if (npc <= 0)
      continue;
    ll r0 = 0;
    for (ll i = 0; i < n; i++) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    ll k = 1;
    for (ll i = n - 1; n >= 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;
    }
  }
  for (ll i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define dbg(x) cout << #x << ": " << (x) << '\\n'
#define ff first
#define ss second
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const ll inf = 2e5 + 10;

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

  ll n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  vector<ll> lo(inf + 10, 1), hi(inf + 10, 1);

  ll x = count(s.begin(), s.end(), '1');

  if (x == 2)
    lo[0] = 0;

  for (ll i = 1; i <= inf; i++) {
    if (x != 1)
      lo[i] = (lo[i - 1] * 2) % (x - 1);
    hi[i] = (hi[i - 1] * 2) % (x + 1);
  }

  ll up = 0, down = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      up = (hi[i] + up) % (x + 1);
  }

  if (x != 1)
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1')
        down = (lo[i] + down) % (x - 1);
    }

  for (ll i = n - 1; i >= 0; i--) {
    ll ans = 1, y = 0;
    if (s[i] == '0') {
      y = (up + hi[i]) % (x + 1);
      while (y != 0) {
        y %= __builtin_popcount(y);
        ans++;
      }
    } else if (s[i] == '1') {
      if (x == 1) {
        cout << 0 << '\\n';
        continue;
      }
      y = (down - lo[i] + (x - 1)) % (x - 1);
      while (y != 0) {
        y %= __builtin_popcount(y);
        ans++;
      }
    }
    cout << ans << '\\n';
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define dbg(x) cout << #x << ": " << (x) << '\\n'
#define ff first
#define ss second
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const ll inf = 2e5 + 10;

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

  ll n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  vector<ll> lo(inf, 1), hi(inf, 1);

  ll x = count(s.begin(), s.end(), '1');

  if (x == 2)
    lo[0] = 0;

  for (ll i = 1; i <= inf; i++) {
    if (x != 1)
      lo[i] = (lo[i - 1] * 2) % (x - 1);
    hi[i] = (hi[i - 1] * 2) % (x + 1);
  }

  for (ll i = n - 1; i >= 0; i--) {
    ll ans = 1, y = 0;
    if (s[i] == '0') {
      y = (y + hi[i]) % (x + 1);
      for (ll j = 0; j < n; j++) {
        if (s[j] == '1')
          y = (y + hi[j]) % (x + 1);
      }
      while (y != 0) {
        y %= __builtin_popcount(y);
        ans++;
      }
    } else if (s[i] == '1') {
      if (x == 1) {
        cout << 0 << '\\n';
        continue;
      }
      for (ll j = 0; j < n; j++) {
        if (i == j)
          continue;
        if (s[j] == '1')
          y = (y + lo[j]) % (x - 1);
      }
      while (y != 0) {
        y %= __builtin_popcount(y);
        ans++;
      }
    }
    cout << ans << '\\n';
  }

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

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

#define int long long
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>

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

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int mod1 = 0, mod0 = 0;
  int bitset = 0;
  int p0[n + 1]{};
  int p1[n + 1]{};

  for (auto x : s) {
    bitset += (x == '1');
  }
  reverse(s.begin(), s.end());
  if (bitset != n) {
    int p2 = 1;
    int mod = bitset + 1;
    for (int i = 0; i < n; i++) {
      p1[i] = p2;
      mod1 += (p2 * (s[i] - '0')) % mod;
      p2 *= 2;
      p2 %= mod;
      mod1 %= mod;
    }
  }

  if (bitset > 1) {
    int p2 = 1;
    int mod = bitset - 1;
    for (int i = 0; i < n; i++) {
      p0[i] = p2;
      mod0 += (p2 * (s[i] - '0')) % mod;
      p2 *= 2;
      p2 %= mod;
      mod0 %= mod;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int ans = mod1;
      ans += (p1[i] % (bitset + 1));
      ans %= (bitset + 1);
      cout << 1 + f(ans) << endl;
    } else {
      int ans = mod0;
      int mod = bitset - 1;
      if (mod == 0) {
        ans = 0;
        cout << 0 << endl;
        continue;
      }
      ans -= (p0[i] % (bitset - 1));
      ans += (bitset - 1);
      ans %= (bitset - 1);
      cout << 1 + f(ans) << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define int long long
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>

char inv(char a) {
  if (a == '0') {
    return '1';
  }
  return '0';
}
int f(string s) {
  int pCount = 0;
  for (auto x : s) {
    pCount += (x == '1');
  }
  reverse(s.begin(), s.end());
  int newVal = 0;
  int p2 = 1;
  for (int i = 0; i < s.size(); i++) {
    newVal += (s[i] == '1') * p2;
    p2 = p2 * 2;
    p2 %= pCount;
    newVal %= pCount;
  }
  string newMod = "";
  while (newVal > 0) {
    if (newVal & 1) {
      newMod += "1";
    } else {
      newMod += "0";
    }
    newVal /= 2;
  }
  reverse(newMod.begin(), newMod.end());
  if (newMod.size() > 0) {
    return 1 + f(newMod);
  }
  return 1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    s[i] = inv(s[i]);
    cout << f(s) << endl;
    s[i] = inv(s[i]);
  }

  return 0;
}