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

#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int n, a[N], cnt[2], pcnt[N], mrk[2][N];
void init(void) {
  for (int i = 1; i < N; ++i) {
    for (int j = i; j >= 1; j -= j & (-j))
      pcnt[i]++;
  }
}
int main(void) {
  IOS;
  init();
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); ++i) {
    a[n - i - 1] = s[i] - '0';
    cnt[a[n - i - 1]]++;
  }
  int mod1 = cnt[1] + 1, mod2 = cnt[1] - 1, ans1 = 0, ans2 = 0;
  for (int i = n - 1; i >= 0; --i) {
    ans1 = (ans1 * 2 + a[i]) % mod1;
  }
  for (int i = mrk[0][0] = 1; i < n; ++i)
    mrk[0][i] = mrk[0][i - 1] * 2 % mod1;
  if (cnt[1] > 1) {
    for (int i = n - 1; i >= 0; --i) {
      ans2 = (ans2 * 2 + a[i]) % mod2;
    }
    for (int i = mrk[1][0] = 1; i < n; ++i)
      mrk[1][i] = mrk[1][i - 1] * 2 % mod2;
  }
  for (int i = n - 1; i >= 0; --i) {
    int ans;
    if (a[i] == 0) {
      ans = (ans1 + mrk[0][i]) % mod1;
    } else {
      if (mod2 == 0) {
        cout << 0 << endl;
        continue;
      }
      if (mod2)
        ans = ((ans2 - mrk[1][i]) % mod2 + mod2) % mod2;
      else
        ans = 0;
    }
    if (!ans) {
      cout << 1 << endl;
    } else {
      int tot = 1;
      while (ans) {
        ans = ans % pcnt[ans];
        tot++;
      }
      cout << tot << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int n, a[N], cnt[2], pcnt[N];
void init(void) {
  for (int i = 1; i < N; ++i) {
    for (int j = i; j >= 1; j -= j & (-j))
      pcnt[i]++;
  }
}
int main(void) {
  IOS;
  init();
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); ++i) {
    a[n - i - 1] = s[i] - '0';
    cnt[a[n - i - 1]]++;
  }
  int mod1 = cnt[1] + 1, mod2 = cnt[1] - 1, ans1 = 0, ans2 = 0;
  for (int i = n - 1; i >= 0; --i) {
    ans1 = (ans1 * 2 + a[i]) % mod1;
  }
  if (cnt[1] > 1) {
    for (int i = n - 1; i >= 0; --i) {
      ans2 = (ans2 * 2 + a[i]) % mod2;
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    int ans;
    if (a[i] == 0) {
      ans = (ans1 + (1 << i)) % mod1;
    } else {
      if (mod2 == 0) {
        cout << 0 << endl;
        continue;
      }
      if (mod2)
        ans = ((ans2 - (1 << i)) % mod2 + mod2) % mod2;
      else
        ans = 0;
    }
    if (!ans) {
      cout << 1 << endl;
    } else {
      int tot = 1;
      while (ans) {
        ans = ans % pcnt[ans];
        tot++;
      }
      cout << tot << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define int long long

using namespace std;

int pw[200005];

int ciclos(int x) {
  if (x == 0)
    return 0;
  int bits = __builtin_popcount(x);
  return 1 + ciclos(x % bits);
}

signed main() {
  int n;
  string s;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  int unos = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      unos++;
  }
  int mod = unos + 1;
  if (unos - 1 > 0)
    mod *= (unos - 1);
  pw[0] = 1;
  for (int i = 1; i < n; i++)
    pw[i] = (pw[i - 1] * 2) % mod;
  int tot = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      tot = (tot + pw[i]) % mod;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (unos == 1) {
        cout << 0 << '\\n';
      } else {
        int queda = (tot - pw[i] + mod) % (unos - 1);
        cout << 1 + ciclos(queda) << '\\n';
      }
    } else {
      int queda = (tot + pw[i] + mod) % (unos + 1);
      cout << 1 + ciclos(queda) << '\\n';
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int power(int k, int mod) {
  int res = 1, n = 2;
  while (k) {
    if (k % 2)
      res = (res * n) % mod;
    n = (n * n) % mod;
    k /= 2;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int unos = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      unos++;
  }
  int bajo_mod = unos - 1, alto_mod = unos + 1;
  int tot_bajo = 0, tot_alto = 0, multi_alt = 1, multi_baj = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      tot_bajo = (tot_bajo + multi_baj) % bajo_mod;
      tot_alto = (tot_alto + multi_alt) % alto_mod;
    }
    multi_alt = (multi_alt * 2) % alto_mod;
    multi_baj = (multi_baj * 2) % bajo_mod;
  }
  for (int i = 0; i < n; i++) {
    int sol = 1;
    int res;
    if (s[i] == '1') {
      res = (((tot_bajo - power(n - i - 1, bajo_mod)) % bajo_mod) + bajo_mod) %
            bajo_mod;
    } else {
      res = (tot_alto + power(n - i - 1, alto_mod)) % alto_mod;
    }
    while (res != 0) {
      int contar = __builtin_popcount(res);
      res %= contar;
      sol++;
    }
    cout << sol << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int64_t popcount(int64_t n) {
  int64_t ret = 0;
  while (n != 0) {
    ret++;
    bitset<25> a(n);
    int C = a.count();
    n = n % C;
  }
  return ret;
}

// 繰り返し二乗法
int64_t powmod(int64_t x, int64_t n, int64_t mod) {
  if (mod == 0) {
    return 0;
  }
  int64_t ans = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ans = ans * x;
      ans %= mod;
    }
    x = x * x; // 一周する度にx, x^2, x^4, x^8となる
    x %= mod;
    n >>= 1; // 桁をずらす n = n >> 1
  }
  return ans % mod;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      cnt++;
    }
  }

  int64_t amari_plus = 0;
  int64_t amari_minus = 0;

  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      amari_plus += powmod(2, N - i - 1, cnt + 1);
      if (cnt != 0) {
        amari_minus += powmod(2, N - i - 1, cnt - 1);
      }
    }
  }

  amari_plus %= cnt + 1;
  if (cnt > 1) {
    amari_minus %= cnt - 1;
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      int64_t x = powmod(2, N - i - 1, cnt - 1);
      x = amari_minus - x;
      if (x < 0) {
        x += cnt - 1;
      }
      cout << popcount(x) + 1 << endl;
    } else {
      int64_t x = powmod(2, N - i - 1, cnt + 1);
      x = amari_plus + x;
      x %= cnt + 1;
      cout << popcount(x) + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

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

int64_t popcount(int64_t n) {
  int64_t ret = 0;
  while (n != 0) {
    ret++;
    bitset<25> a(n);
    int C = a.count();
    n = n % C;
  }
  return ret;
}

// 繰り返し二乗法
int64_t powmod(int64_t x, int64_t n, int64_t mod) {
  if (mod == 0) {
    return 0;
  }
  int64_t ans = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ans = ans * x;
      ans %= mod;
    }
    x = x * x; // 一周する度にx, x^2, x^4, x^8となる
    x %= mod;
    n >>= 1; // 桁をずらす n = n >> 1
  }
  return ans % mod;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      cnt++;
    }
  }

  int64_t amari_plus = 0;
  int64_t amari_minus = 0;

  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      amari_plus += powmod(2, N - i - 1, cnt + 1);
      if (cnt != 0) {
        amari_minus += powmod(2, N - i - 1, cnt - 1);
      }
    }
  }

  amari_plus %= cnt + 1;
  if (cnt != 0) {
    amari_minus %= cnt - 1;
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      int64_t x = powmod(2, N - i - 1, cnt - 1);
      x = amari_minus - x;
      if (x < 0) {
        x += cnt - 1;
      }
      cout << popcount(x) + 1 << endl;
    } else {
      int64_t x = powmod(2, N - i - 1, cnt + 1);
      x = amari_plus + x;
      x %= cnt + 1;
      cout << popcount(x) + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define PB push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second

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

const int MAX = 10000007;

int solve(string s, int MOD) {
  if (MOD <= 0)
    return 0;
  int p = 1;
  reverse(all(s));
  int x = 0;
  FOR(i, s.size()) {
    x = (x + p) % MOD;
    p = 2LL * p % MOD;
  }

  return x;
}

int computa(int n) {
  int ans = 0;
  while (n) {
    ans++;
    n %= __builtin_popcount(n);
  }
  return ans;
}

int p21[200007], p20[200007];

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

  int n;
  cin >> n;
  string s;
  cin >> s;

  int cnt = 0;
  FOR(i, n) cnt += (s[i] == '1');

  p21[0] = p20[0] = 1;
  for (int i = 1; i < 200007; i++) {
    p21[i] = 2LL * p21[i - 1] % (cnt + 1);
    if (cnt > 1)
      p20[i] = 2LL * p20[i - 1] % (cnt - 1);
  }

  int x1 = 0, x0 = 0;
  FOR(i, n) {
    if (s[n - i - 1] == '1') {
      x1 = (x1 + p21[i]) % (cnt + 1);
      if (cnt > 1)
        x0 = (x0 + p20[i]) % (cnt - 1);
    }
  }

  FOR(i, n) {
    if (s[i] == '1') {
      if (cnt == 1)
        cout << "0\\n";
      else {
        int aux = (x0 - p20[n - i - 1] + cnt - 1) % (cnt - 1);
        cout << computa(aux) + 1 << '\\n';
      }
    } else {
      int aux = (x1 + p21[n - i - 1]) % (cnt + 1);
      cout << computa(aux) + 1 << '\\n';
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define PB push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second

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

const int MAX = 10000007;

int solve(string s, int MOD) {
  if (MOD <= 0)
    return 0;
  int p = 1;
  reverse(all(s));
  int x = 0;
  FOR(i, s.size()) {
    x = (x + p) % MOD;
    p = 2LL * p % MOD;
  }

  return x;
}

int computa(int n) {
  int ans = 0;
  while (n) {
    ans++;
    n %= __builtin_popcount(n);
  }
  return ans;
}

int p21[100007], p20[100007];

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

  int n;
  cin >> n;
  string s;
  cin >> s;

  int cnt = 0;
  FOR(i, n) cnt += (s[i] == '1');

  p21[0] = p20[0] = 1;
  for (int i = 1; i < 100007; i++) {
    p21[i] = 2LL * p21[i - 1] % (cnt + 1);
    if (cnt > 1)
      p20[i] = 2LL * p20[i - 1] % (cnt - 1);
  }

  int x1 = 0, x0 = 0;
  FOR(i, n) {
    if (s[n - i - 1] == '1') {
      x1 = (x1 + p21[i]) % (cnt + 1);
      if (cnt > 1)
        x0 = (x0 + p20[i]) % (cnt - 1);
    }
  }

  FOR(i, n) {
    if (s[i] == '1') {
      if (cnt == 1)
        cout << "0\\n";
      else {
        int aux = (x0 - p20[n - i - 1] + cnt - 1) % (cnt - 1);
        cout << computa(aux) + 1 << '\\n';
      }
    } else {
      int aux = (x1 + p21[n - i - 1]) % (cnt + 1);
      cout << computa(aux) + 1 << '\\n';
    }
  }

  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

long long f(long long x) {
  int cnt = 0;
  for (int i = 0; i < 20; i++) {
    cnt += (x >> i) & 1;
  }
  return x % cnt;
}

long long modpow(int p, long long m) {
  long long ret = 1;
  long long b = 2;
  for (int i = 0; i < 20; i++) {
    if ((p >> i) & 1)
      ret = ret * b % m;
    b = b * b % m;
  }
  return ret;
}

int main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  std::vector<long long> m(3), r(3);
  for (int i = 0; i < n; i++)
    m[1] += s[i] == '1';
  m[0] = m[1] - 1;
  m[2] = m[1] + 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (m[j] == 0)
        continue;
      r[j] = (r[j] * 2 + (s[i] == '1')) % m[j];
    }
  }
  for (int i = 0; i < n; i++) {
    int id = s[i] == '1' ? 0 : 2;
    if (m[id] == 0) {
      std::cout << 0 << std::endl;
      continue;
    }
    int ans = 1;
    long long q =
        (r[id] + m[id] + (id - 1) * modpow(n - 1 - i, m[id]) % m[id]) % m[id];
    while (q != 0) {
      q = f(q);
      ans++;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

long long f(long long x) {
  int cnt = 0;
  for (int i = 0; i < 20; i++) {
    cnt += (x >> i) & 1;
  }
  return x % cnt;
}

long long modpow(int p, long long m) {
  long long ret = 1;
  for (int i = 0; i < p; i++) {
    ret = ret * 2 % m;
  }
  return ret;
}

int main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  std::vector<long long> m(3), r(3);
  for (int i = 0; i < n; i++)
    m[1] += s[i] == '1';
  m[0] = m[1] - 1;
  m[2] = m[1] + 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (m[j] == 0)
        continue;
      r[j] = (r[j] * 2 + (s[i] == '1')) % m[j];
    }
  }
  for (int i = 0; i < n; i++) {
    int id = s[i] == '1' ? 0 : 2;
    if (m[id] == 0) {
      std::cout << 0 << std::endl;
      continue;
    }
    int ans = 1;
    long long q =
        (r[id] + m[id] + (id - 1) * modpow(n - 1 - i, m[id]) % m[id]) % m[id];
    while (q != 0) {
      q = f(q);
      ans++;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

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

vector<ll> ans(2e5 + 10, 0);

int main() {
  for (int i = 0; i < ans.size(); ++i) {
    ll tmp = 0;
    ll j = i;
    while (j != 0) {
      j %= __builtin_popcount(j);
      tmp++;
    }
    ans[i] = tmp;
  }

  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll pop_x = 0;
  for_each(x.begin(), x.end(), [&](auto e) {
    if (e == '1')
      pop_x++;
  });
  ll pop_one = pop_x - 1, pop_zero = pop_x + 1;
  bool flag = false;
  if (pop_one == 0) {
    flag = true;
    pop_one = 1;
  }
  ll num_x_one = 0, num_x_zero = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '0')
      continue;
    num_x_one += bin_pow(2, n - 1 - i, pop_one);
    num_x_one %= pop_one;
    num_x_zero += bin_pow(2, n - 1 - i, pop_zero);
    num_x_zero %= pop_zero;
  }
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (flag) {
        cout << 0 << endl;
        continue;
      }
      ll idx = (num_x_one + pop_one - bin_pow(2, n - i - 1, pop_one)) % pop_one;
      cout << 1 + ans[idx] << endl;
    } else {
      ll idx = (num_x_zero + 2 * pop_zero + bin_pow(2, n - i - 1, pop_zero)) %
               pop_zero;
      cout << 1 + ans[idx] << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

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

vector<ll> ans(2e5 + 10, 0);

int main() {
  for (int i = 0; i < ans.size(); ++i) {
    ll tmp = 0;
    ll j = i;
    while (j != 0) {
      j %= __builtin_popcount(j);
      tmp++;
    }
    ans[i] = tmp;
  }

  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll pop_x = 0;
  for_each(x.begin(), x.end(), [&](auto e) {
    if (e == '1')
      pop_x++;
  });
  ll pop_one = pop_x - 1, pop_zero = pop_x + 1;
  bool flag = false;
  if (pop_one == 0)
    flag = true;
  ll num_x_one = 0, num_x_zero = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '0')
      continue;
    num_x_one += bin_pow(2, n - 1 - i, pop_one);
    num_x_one %= pop_one;
    num_x_zero += bin_pow(2, n - 1 - i, pop_zero);
    num_x_zero %= pop_zero;
  }
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (flag) {
        cout << 0 << endl;
        continue;
      }
      ll idx = (num_x_one + pop_one - bin_pow(2, n - i - 1, pop_one)) % pop_one;
      cout << 1 + ans[idx] << endl;
    } else {
      ll idx = (num_x_zero + 2 * pop_zero + bin_pow(2, n - i - 1, pop_zero)) %
               pop_zero;
      cout << 1 + ans[idx] << endl;
    }
  }
}
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;
using P = pair<int, int>;

int main() {
  int N;
  string X;
  cin >> N >> X;
  bitset<200000> x(X);

  int cnt = x.count();
  int hp = cnt + 1;
  int lp = max((int)(cnt - 1), 1);
  int hr = 0;
  int lr = 0;
  vector<int> H(N, (1 % hp));
  vector<int> L(N, (1 % lp));
  rep(i, N) {
    if (i != 0) {
      H.at(i) = H.at(i - 1) * 2 % hp;
      L.at(i) = L.at(i - 1) * 2 % lp;
    }
    if (!x.test(i))
      continue;
    hr += H.at(i);
    hr %= hp;
    lr += L.at(i);
    lr %= lp;
  }

  rep(i, N) {
    int p = cnt + (x.test(N - 1 - i) ? -1 : 1);
    if (p == 0) {
      cout << 0 << endl;
      continue;
    }

    int r = 0;
    if (p == hp) {
      r = hr;
      r += H.at(N - 1 - i);
    } else if (p == lp) {
      r = lr;
      r -= L.at(N - 1 - i);
      if (r < 0)
        r += p;
    }
    r %= p;

    int ans = 1;
    while (r != 0) {
      r %= bitset<20>(r).count();
      // r %= __builtin_popcount(r);
      ans++;
    }
    cout << ans << 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;
  bitset<200000> x(X);

  int hp = x.count() + 1;
  int lp = max((int)(x.count() - 1), 1);
  int hr = 0;
  int lr = 0;
  vector<int> H(N, (1 % hp));
  vector<int> L(N, (1 % lp));
  rep(i, N) {
    if (i != 0) {
      H.at(i) = H.at(i - 1) * 2 % hp;
      L.at(i) = L.at(i - 1) * 2 % lp;
    }
    if (!x.test(i))
      continue;
    hr += H.at(i);
    hr %= hp;
    lr += L.at(i);
    lr %= lp;
  }

  rep(i, N) {
    // bitset n = x;
    // x.flip(N - 1 - i);

    // if (x == 0) {
    //   cout << 0 << endl;
    //   continue;
    // }

    int p = x.count() + (x.test(N - 1 - i) ? -1 : 1);
    if (p == 0) {
      cout << 0 << endl;
      continue;
    }

    int r = 0;
    if (p == hp) {
      r = hr;
      r += H.at(N - 1 - i);
    } else if (p == lp) {
      r = lr;
      r -= L.at(N - 1 - i);
      if (r < 0)
        r += p;
    }
    r %= p;

    int ans = 1;
    while (r != 0) {
      r %= bitset<20>(r).count();
      ans++;
    }
    cout << ans << endl;
    // x.flip(N - 1 - i);
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Jul 11 21:13:30 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

Int solve(Int x) {
  Int ret = 0;
  while (x) {
    ret++;
    x %= __builtin_popcountll(x);
  }
  return ret;
}
signed main() {
  Int n;
  std::string s;
  std::cin >> n >> s;
  Int cnt = std::count(s.begin(), s.end(), '1');
  auto pows = make_vec<Int>(n, 3, 1);
  std::vector<Int> rem(3);
  REP(dif, 3) {
    Int acc = 0;
    Int mod = cnt + dif - 1;
    if (mod <= 0)
      continue;
    for (Int i = n - 1; i >= 0; i--) {
      if (i < n - 1)
        pows[i][dif] = pows[i + 1][dif] * 2 % mod;
      acc += pows[i][dif] * (s[i] == '1');
      acc %= mod;
    }
    rem[dif] = acc;
  }
  REP(i, n) {
    Int x = s[i] == '1' ? rem[0] - pows[i][0] : rem[2] + pows[i][2];
    Int mod = s[i] == '1' ? cnt - 1 : cnt + 1;
    if (mod == 0) {
      std::cout << 0 << "\\n";
    } else {
      x += mod;
      x %= mod;
      std::cout << 1 + solve(x) << "\\n";
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Jul 11 21:13:30 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

Int solve(Int x) {
  Int ret = 0;
  while (x) {
    ret++;
    x %= __builtin_popcountll(x);
  }
  return ret;
}
signed main() {
  Int n;
  std::string s;
  std::cin >> n >> s;
  Int cnt = std::count(s.begin(), s.end(), '1');
  auto pows = make_vec<Int>(n, 3, 1);
  std::vector<Int> rem(3);
  REP(dif, 3) {
    Int acc = 0;
    Int mod = cnt + dif - 1;
    for (Int i = n - 1; i >= 0; i--) {
      if (i < n - 1)
        pows[i][dif] = pows[i + 1][dif] * 2 % mod;
      acc += pows[i][dif] * (s[i] == '1');
      acc %= mod;
    }
    rem[dif] = acc;
  }
  REP(i, n) {
    Int x = s[i] == '1' ? rem[0] - pows[i][0] : rem[2] + pows[i][2];
    Int mod = s[i] == '1' ? cnt - 1 : cnt + 1;
    x += mod;
    x %= mod;
    std::cout << 1 + solve(x) << "\\n";
  }
}