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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

inline int popcount(int x) {
  int ret{};
  while (x)
    ret += x % 2, x >>= 1;
  return ret;
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int p = count(begin(X), end(X), '1') - 1, r = p + 2;

  if (p == -1) {
    for (int i = 0; i != N; ++i) {
      cout << 1 << endl;
    }
    return 0;
  } else if (!p) {
    // only a bit is 1
    int f = X.find('1');
    if (f == N - 1) {
      for (int i = 0; i != N; ++i) {
        cout << (i != f) << endl;
      }
    } else {
      for (int i = 0; i != N; ++i) {
        if (i == f)
          cout << 0 << endl;
        else if (i == N - 1) {
          cout << 2 << endl;
        } else {
          cout << 1 << endl;
        }
      }
    }
    return 0;
  }

  vector<int> a(N), b(N);
  a[0] = b[0] = 1;
  for (int i = 1; i < N; ++i) {
    a[i] = (2 * a[i - 1]) % p;
    b[i] = (2 * b[i - 1]) % r;
  }

  int x{}, y{};
  for (int i = 0; i != N; ++i) {
    x = (x + a[N - 1 - i] * (X[i] - '0')) % p;
    y = (y + b[N - 1 - i] * (X[i] - '0')) % r;
  }

  for (int i = 0; i != N; ++i) {
    if (X[i] == '0') {
      int w = (y + b[N - 1 - i]) % r;
      int ans{1};
      while (w)
        w %= popcount(w), ++ans;
      cout << ans << endl;
    } else {
      int w = (x + p - a[N - 1 - i]) % p;
      int ans{1};
      while (w)
        w %= popcount(w), ++ans;
      cout << ans << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

inline int popcount(int x) {
  int ret{};
  while (x)
    ret += x % 2, x >>= 1;
  return ret;
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int q = count(begin(X), end(X), '1'), p = q - 1, r = q + 1;

  vector<int> a, b;
  int d{1}, e{1};
  for (int i = 0; i < N; ++i) {
    a.push_back(d);
    b.push_back(e);
    d = (2 * d) % p;
    e = (2 * e) % r;
  }

  int x{}, y{};
  for (int i = 0; i != N; ++i) {
    x += a[N - 1 - i] * (X[i] - '0');
    y += b[N - 1 - i] * (X[i] - '0');
  }

  for (int i = 0; i != N; ++i) {
    if (X[i] == '0') {
      int w = (y + b[N - 1 - i]) % r;
      int ans{1};
      while (w)
        w %= popcount(w), ++ans;
      cout << ans << endl;
    } else {
      int w = (x + p - a[N - 1 - i]) % p;
      int ans{1};
      while (w)
        w %= popcount(w), ++ans;
      cout << ans << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int arr[200007], brr[200007], crr[200007], drr[200007], pre[200007],
    suf[200007];
int f(int x) {
  //  assert(x<0);
  if (!x)
    return 0;
  int one = __builtin_popcount(x);
  x %= one;
  return f(x) + 1;
}
int main() {
  string s;
  int n;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (s[i] == '1');
  }
  int ans = 1, ans2 = 1;
  for (int i = 0; i < n; i++) {
    if (cnt > 1)
      crr[i] = ans % (cnt - 1);
    drr[i] = ans2 % (cnt + 1);
    ans *= 2;
    ans2 *= 2;
    if (cnt > 1)
      ans %= cnt - 1;
    else
      ans = 0;
    ans2 %= cnt + 1;

    if (s[i] == '1') {
      arr[i] = crr[i];
      brr[i] = drr[i];
      pre[i] = crr[i];
      suf[i] = drr[i];
    }
  }

  for (int i = 1; i < n; i++) {
    arr[i] += arr[i - 1];
    brr[i] += brr[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    pre[i] += pre[i + 1];
    suf[i] += suf[i + 1];
    if (cnt > 1)
      pre[i] %= (cnt - 1);
    suf[i] %= (cnt + 1);
  }

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int ans = drr[i];
      ans = (suf[0] + ans) % (cnt + 1);
      cout << f(ans) + 1 << endl;
    } else {
      ans = pre[0];
      if (cnt == 1) {
        cout << 0 << endl;
      } else {
        ans -= crr[i];
        ans %= (cnt - 1);
        ans += (cnt - 1);
        ans %= (cnt - 1);
        cout << f(ans) + 1 << endl;
      }
    }
  }

  return 0;
}


Error: -6
3062438f-04e9-41bf-937e-4d8672ac60b7.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02609/C++/s693823689.cpp:5: int f(int): Assertion `x<0' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int arr[200007], brr[200007], crr[200007], drr[200007], pre[200007],
    suf[200007];
int f(int x) {
  assert(x < 0);
  if (!x)
    return 0;
  int one = __builtin_popcount(x);
  x %= one;
  return f(x) + 1;
}
int main() {
  string s;
  int n;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (s[i] == '1');
  }
  int ans = 1, ans2 = 1;
  for (int i = 0; i < n; i++) {
    if (cnt > 1)
      crr[i] = ans % (cnt - 1);
    drr[i] = ans2 % (cnt + 1);
    ans *= 2;
    ans2 *= 2;
    if (cnt > 1)
      ans %= cnt - 1;
    else
      ans = 0;
    ans2 %= cnt + 1;

    if (s[i] == '1') {
      arr[i] = crr[i];
      brr[i] = drr[i];
      pre[i] = crr[i];
      suf[i] = drr[i];
    }
  }

  for (int i = 1; i < n; i++) {
    arr[i] += arr[i - 1];
    brr[i] += brr[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    pre[i] += pre[i + 1];
    suf[i] += suf[i + 1];
  }

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int ans = drr[i];
      ans = (suf[0] + ans) % (cnt + 1);
      cout << f(ans) + 1 << endl;
    } else {
      ans = pre[0];
      if (cnt == 1) {
        cout << 0 << endl;
      } else {
        ans -= crr[i];
        ans %= (cnt - 1);
        ans += (cnt - 1);
        ans %= (cnt - 1);
        cout << f(ans) + 1 << endl;
      }
    }
  }

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

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

#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

long rec(long x) {
  if (x == 0)
    return 0;
  long p = popcountll(x);
  return rec(x % p) + 1;
}

void Main() {
  long n;
  string xs;
  cin >> n >> xs;
  vector<int> xi(n);
  long xp = 0;
  for (int i = 0; i < n; i++) {
    int a = (xs[n - 1 - i] == '1' ? 1 : 0);
    xi[i] = a;
    xp += a;
  }

  if (xp != 1) {
    vector<long> m1(n), m2(n);
    m1[0] = 1 % (xp - 1);
    m2[0] = 1 % (xp + 1);
    for (int i = 1; i < n; i++) {
      m1[i] = (m1[i - 1] << 1) % (xp - 1);
      m2[i] = (m2[i - 1] << 1) % (xp + 1);
    }
    long s1 = 0, s2 = 0;
    for (int i = 0; i < n; i++) {
      if (xi[i]) {
        s1 = (s1 + m1[i]) % (xp - 1);
        s2 = (s2 + m2[i]) % (xp + 1);
      }
    }

    for (int i = 0; i < n; i++) {
      long xt;
      int idx = n - 1 - i;
      if (xi[idx]) {
        xt = (s1 - m1[idx]) % (xp - 1);
        xt = (xt + xp - 1) % (xp - 1);
      } else {
        xt = (s2 + m2[idx]) % (xp + 1);
      }
      cout << rec(xt) + 1 << endl;
    }
  } else {
    vector<long> m1(n), m2(n);
    m2[0] = 1 % (xp + 1);
    for (int i = 1; i < n; i++) {
      m2[i] = (m2[i - 1] << 1) % (xp + 1);
    }
    long s1 = 0, s2 = 0;
    for (int i = 0; i < n; i++) {
      if (xi[i]) {
        s2 = (s2 + m2[i]) % (xp + 1);
      }
    }

    for (int i = 0; i < n; i++) {
      long xt;
      int idx = n - 1 - i;
      if (xi[idx]) {
        cout << 0 << endl;
      } else {
        xt = (s2 + m2[idx]) % (xp + 1);
        cout << rec(xt) + 1 << endl;
      }
    }
  }
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}


Error: 0

Fixed submission:

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

#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

long rec(long x) {
  if (x == 0)
    return 0;
  long p = popcountll(x);
  return rec(x % p) + 1;
}

void Main() {
  long n;
  string xs;
  cin >> n >> xs;
  vector<int> xi(n);
  long xp = 0;
  for (int i = 0; i < n; i++) {
    int a = (xs[n - 1 - i] == '1' ? 1 : 0);
    xi[i] = a;
    xp += a;
  }

  vector<long> m1(n), m2(n);
  m1[0] = 1 % (xp - 1);
  m2[0] = 1 % (xp + 1);
  for (int i = 1; i < n; i++) {
    m1[i] = (m1[i - 1] << 1) % (xp - 1);
    m2[i] = (m2[i - 1] << 1) % (xp + 1);
  }
  long s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) {
    if (xi[i]) {
      s1 = (s1 + m1[i]) % (xp - 1);
      s2 = (s2 + m2[i]) % (xp + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    long xt;
    int idx = n - 1 - i;
    if (xi[idx]) {
      xt = (s1 - m1[idx]) % (xp - 1);
      xt = (xt + xp - 1) % (xp - 1);
    } else {
      xt = (s2 + m2[idx]) % (xp + 1);
    }
    cout << rec(xt) + 1 << endl;
  }
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

vector<int64_t> memo;
int64_t f(int64_t n) {
  if (n == 0) {
    return 0;
  }
  if (memo[n] != -1) {
    return memo[n];
  }

  return memo[n] = (f(n % __builtin_popcountll(n)) + 1);
}

// a^b % modを求める
int64_t MODpow(int64_t a, int64_t b, int64_t mod) {
  int64_t result = 1;
  while (b) {
    if (b % 2 == 1) {
      result *= a;
      result %= mod;
    }

    b /= 2;
    a *= a;
    a %= mod;
  }

  return result;
}

int main() {
  int64_t N;
  cin >> N;

  memo.resize(N + 2, -1);

  string X;
  cin >> X;

  int64_t b = count(X.begin(), X.end(), '1');
  // ビット数はb or (b + 1)になる

  if (b == 1) {
    // b - 1が0になって終わるので適宜計算
    int64_t one_index = find(X.begin(), X.end(), '1') - X.begin();

    for (int64_t i = 0; i < N; i++) {
      if (i == one_index) {
        // 0になるので0
        cout << 0 << endl;
      } else {
        // こことone_indexだけが立つ
        int64_t v =
            (MODpow(2, N - 1 - i, 2) + MODpow(2, N - 1 - one_index, 2)) % 2;
        cout << f(v) + 1 << endl;
      }
    }

    return 0;
  }

  int64_t case_b_minus_1 = 0, case_b_plus_1 = 0;
  for (int64_t i = 0; i < N; i++) {
    if (X[i] == '1') {
      (case_b_minus_1 += MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
      (case_b_plus_1 += MODpow(2, N - 1 - i, b + 1)) %= (b + 1);
    }
  }

  for (int64_t i = 0; i < N; i++) {
    // iを反転
    if (X[i] == '0') {
      // 1になる
      int64_t v = (case_b_plus_1 + MODpow(2, N - 1 - i, b + 1)) % (b + 1);
      cout << f(v) + 1 << endl;
    } else {
      // 0になる
      int64_t v = case_b_minus_1;
      // 自分の影響を除く
      (v += b - 1 - MODpow(2, N - 1 - i, b - 1)) %= (b - 1);

      cout << f(v) + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

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

vector<int64_t> memo;
int64_t f(int64_t n) {
  if (n == 0) {
    return 0;
  }
  if (memo[n] != -1) {
    return memo[n];
  }

  return memo[n] = (f(n % __builtin_popcountll(n)) + 1);
}

// a^b % modを求める
int64_t MODpow(int64_t a, int64_t b, int64_t mod) {
  int64_t result = 1;
  while (b) {
    if (b % 2 == 1) {
      result *= a;
      result %= mod;
    }

    b /= 2;
    a *= a;
    a %= mod;
  }

  return result;
}

int main() {
  int64_t N;
  cin >> N;

  memo.resize(N + 2, -1);

  string X;
  cin >> X;

  int64_t b = count(X.begin(), X.end(), '1');
  // ビット数はb or (b + 1)になる

  int64_t case_b_minus_1 = 0, case_b_plus_1 = 0;
  for (int64_t i = 0; i < N; i++) {
    if (X[i] == '1') {
      (case_b_minus_1 += MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
      (case_b_plus_1 += MODpow(2, N - 1 - i, b + 1)) %= (b + 1);
    }
  }

  for (int64_t i = 0; i < N; i++) {
    // iを反転
    if (X[i] == '0') {
      // 1になる
      int64_t v = (case_b_plus_1 + MODpow(2, N - 1 - i, b + 1)) % (b + 1);
      cout << f(v) + 1 << endl;
    } else {
      // 0になる
      int64_t v = case_b_minus_1;
      // 自分の影響を除く
      (v += b - 1 - MODpow(2, N - 1 - i, b - 1)) %= (b - 1);

      cout << f(v) + 1 << 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;
typedef pair<ll, ll> pll;

ll N;
ll M = 0;
ll R_1 = 0, R1 = 0;
string X;
ll mp[200005];

ll mpow(ll a, ll n, ll MOD) {
  if (MOD == 0)
    return 0;
  if (n == 0)
    return 1;
  ll r = mpow(a, n / 2, MOD);
  r = r * r;
  if (n % 2)
    r *= a;
  r %= MOD;
  return r;
}

ll caln1(ll a) {
  int r = 0;
  while (a > 0) {
    r += a % 2;
    a = a >> 1;
  }
  return r;
}

ll solve() {
  reverse(X.begin(), X.end());

  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      M++;
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      R_1 += mpow(2, i, M - 1);
      R1 += mpow(2, i, M + 1);
    }
  }

  mp[0] = 0;
  for (int a = 1; a <= 2e5; a++) {
    ll b = a;
    ll r = 0;
    while (b > 0) {
      r++;
      ll n1 = caln1(b);
      b = b % n1;
    }
    mp[a] = r;
  }

  vector<ll> res;
  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll r = (R1 + mpow(2, i, M + 1)) % (M + 1);
      res.push_back(mp[r] + 1);

    } else {
      if (M - 1 == 0) {
        res.push_back(0);
        continue;
      }
      ll r = ((R_1 - mpow(2, i, M - 1)) % (M - 1) + (M - 1)) % (M - 1);
      res.push_back(mp[r] + 1);
    }
  }

  reverse(res.begin(), res.end());
  for (int r : res) {
    cout << r << endl;
  }
  return 0;
}

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

  cin >> N >> X;
  solve();
  return 0;
}


Error: 0

Fixed submission:

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

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

ll N;
ll M = 0;
ll R_1 = 0, R1 = 0;
string X;
ll mp[200005];

ll mpow(ll a, ll n, ll MOD) {
  if (n == 0)
    return 1;
  ll r = mpow(a, n / 2, MOD);
  r = r * r;
  if (n % 2)
    r *= a;
  r %= MOD;
  return r;
}

ll caln1(ll a) {
  int r = 0;
  while (a > 0) {
    r += a % 2;
    a = a >> 1;
  }
  return r;
}

ll solve() {
  reverse(X.begin(), X.end());

  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      M++;
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      R_1 += mpow(2, i, M - 1);
      R1 += mpow(2, i, M + 1);
    }
  }

  for (int a = 1; a <= 2e5; a++) {
    ll b = a;
    ll r = 0;
    while (b > 0) {
      r++;
      ll n1 = caln1(b);
      b = a % n1;
    }
    mp[a] = r;
  }

  vector<ll> res;
  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll r = (R1 + mpow(2, i, M + 1)) % (M + 1);
      res.push_back(mp[r] + 1);

    } else {
      ll r = ((R_1 - mpow(2, i, M - 1)) % (M - 1) + (M - 1)) % (M - 1);
      res.push_back(mp[r] + 1);
    }
  }

  reverse(res.begin(), res.end());
  for (int r : res) {
    cout << r << endl;
  }
  return 0;
}

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

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

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

long long Calc(long long P) {
  if (P == 0)
    return 0;
  long long COUNT = 0;
  long long Q = P;
  while (Q > 0) {
    COUNT += Q % 2;
    Q /= 2;
  }
  return Calc(P % COUNT) + 1;
}

long long Pow(long long A, long long B, long long M) {
  if (B == 0)
    return 1 % M;
  if (B % 2 == 0) {
    long long C = Pow(A, B / 2, M);
    return (C * C) % M;
  }
  return (A * Pow(A, B - 1, M)) % M;
}

int main() {
  long long N;
  string S;
  cin >> N >> S;
  long long X = 0;
  for (int i = 0; i < N; i++)
    X += (S[i] - '0');
  long long MOD1 = 0, MOD2 = 0, Y = X - 1, Z = X + 1;
  if (Y != 0) {
    for (int i = 0; i < N; i++) {
      MOD1 *= 2;
      MOD1 += S[i] - '0';
      MOD1 %= Y;
    }
  }
  for (int i = 0; i < N; i++) {
    MOD2 *= 2;
    MOD2 += S[i] - '0';
    MOD2 %= Z;
  }
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      long long NEXT = (MOD2 + Pow(2, N - 1 - i, Z)) % Z;
      cout << Calc(NEXT) + 1 << endl;
    } else {
      if (Y == 0)
        cout << 0 << endl;
      else {
        long long NEXT = (MOD1 - Pow(2, N - 1 - i, Y) + Y) % Y;
        cout << Calc(NEXT) + 1 << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

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

long long Calc(long long P) {
  if (P == 0)
    return 0;
  long long COUNT = 0;
  long long Q = P;
  while (Q > 0) {
    COUNT += Q % 2;
    Q /= 2;
  }
  return Calc(P % COUNT) + 1;
}

long long Pow(long long A, long long B, long long M) {
  if (B == 0)
    return 1 % M;
  if (B % 2 == 0) {
    long long C = Pow(A, B / 2, M);
    return (C * C) % M;
  }
  return (A * Pow(A, B - 1, M)) % M;
}

int main() {
  long long N;
  string S;
  cin >> N >> S;
  long long X = 0;
  for (int i = 0; i < N; i++)
    X += (S[i] - '0');
  long long MOD1 = 0, MOD2 = 0, Y = X - 1, Z = X + 1;
  if (Y != 0) {
    for (int i = 0; i < N; i++) {
      MOD1 *= 2;
      MOD1 += S[i] - '0';
      MOD1 %= Y;
    }
  }
  for (int i = 0; i < N; i++) {
    MOD2 *= 2;
    MOD2 += S[i] - '0';
    MOD2 %= Z;
  }
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      long long NEXT = (MOD2 + Pow(2, N - 1 - i, Z)) % Z;
      cout << Calc(NEXT) + 1 << endl;
    } else {
      if (Y == 0)
        cout << 0 << endl;
      long long NEXT = (MOD1 - Pow(2, N - 1 - i, Y) + Y) % Y;
      cout << Calc(NEXT) + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(9), 0);
// int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv
// ./prog.exe") >> 8), 0);
using Int = long long;
int A[3][200010], B[3];
int f(int x) {
  int ans = 0;
  while (x != 0) {
    x %= __builtin_popcount(x);
    ans++;
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  for (auto c : X)
    if (c == '1')
      cnt++;
  A[0][0] = A[2][0] = 1;
  for (int i = 1; i < N; i++) {
    if (cnt >= 2)
      A[0][i] = A[0][i - 1] * 2 % (cnt - 1);
    A[2][i] = A[2][i - 1] * 2 % (cnt + 1);
  }
  for (int i = 0; i < N; i++) {
    if (X[N - 1 - i] == '0')
      continue;
    if (cnt >= 2)
      B[0] = (B[0] + A[0][i]) % (cnt - 1);
    B[2] = (B[2] + A[2][i]) % (cnt + 1);
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      int a = (B[2] + A[2][N - 1 - i]) % (cnt + 1);
      cout << f(a) + 1 << '\\n';
    } else if (cnt == 1) {
      cout << 0 << '\\n';
    } else {
      int a = (B[0] - A[0][N - 1 - i] + cnt - 1) % (cnt - 1);
      cout << f(a) + 1 << '\\n';
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(9), 0);
// int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv
// ./prog.exe") >> 8), 0);
using Int = long long;
int A[3][200010], B[3];
int f(int x) {
  int ans = 0;
  while (x != 0) {
    x %= __builtin_popcount(x);
    ans++;
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  for (auto c : X)
    if (c == '1')
      cnt++;
  A[0][0] = A[1][0] = A[2][0] = 1;
  for (int i = 1; i < N; i++) {
    if (cnt != 0)
      A[0][i] = A[0][i - 1] * 2 % (cnt - 1);
    A[1][i] = A[1][i - 1] * 2 % cnt;
    A[2][i] = A[2][i - 1] * 2 % (cnt + 1);
  }
  for (int i = 0; i < N; i++) {
    if (X[N - 1 - i] == '0')
      continue;
    if (cnt != 0)
      B[0] = (B[0] + A[0][i]) % (cnt - 1);
    B[1] = (B[1] + A[1][i]) % cnt;
    B[2] = (B[2] + A[2][i]) % (cnt + 1);
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      int a = (B[2] + A[2][N - 1 - i]) % (cnt + 1);
      cout << f(a) + 1 << '\\n';
    } else {
      int a = (B[0] - A[0][N - 1 - i] + cnt - 1) % (cnt - 1);
      cout << f(a) + 1 << '\\n';
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int MAXN = 200045;
char s[MAXN * 10 + 10];
int quickmod(int a, int b, int mod) {
  int dd = 1;
  while (b) {
    if (b & 1)
      dd = 1ll * dd * a % mod;
    a = 1ll * a * a % mod;
    b /= 2;
  }
  return dd % mod;
}
int dfs(int num) {
  if (num == 0)
    return 0;
  int cnt = 0;
  int temp = num;
  while (temp) {
    if (temp & 1)
      cnt++;
    temp /= 2;
  }
  return 1 + dfs(num % cnt);
}
int main() {
  // cout << quickmod(2,2,14) << endl;
  // cout << 1%-1 << endl;
  int n;
  cin >> n;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  int num1 = 0;
  int num2 = 0;
  for (int i = 0; i < n; i++) {
    if (cnt - 1 != 0) {
      num1 = (num1 * 2 + s[i] - '0') % (cnt - 1);
    }
    num2 = (num2 * 2 + s[i] - '0') % (cnt + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt - 1 == 0) {
        cout << 0 << endl;
      } else
        cout << 1 + dfs(((num1 - quickmod(2, n - i - 1, cnt - 1)) % (cnt - 1) +
                         cnt - 1) %
                        (cnt - 1))
             << endl;
    } else
      cout << 1 + dfs((num2 + (quickmod(2, n - i - 1, cnt + 1)) % (cnt + 1)) %
                      (cnt + 1))
           << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int MAXN = 200045;
char s[MAXN * 10 + 10];
int quickmod(int a, int b, int mod) {
  int dd = 1;
  while (b) {
    if (b & 1)
      dd = 1ll * dd * a % mod;
    a = 1ll * a * a % mod;
    b /= 2;
  }
  return dd % mod;
}
int dfs(int num) {
  if (num == 0)
    return 0;
  int cnt = 0;
  int temp = num;
  while (temp) {
    if (temp & 1)
      cnt++;
    temp /= 2;
  }
  return 1 + dfs(num % cnt);
}
int main() {
  // cout << quickmod(2,2,14) << endl;
  // cout << 1%-1 << endl;
  int n;
  cin >> n;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  int num1 = 0;
  int num2 = 0;
  for (int i = 0; i < n; i++) {
    num1 = (num1 * 2 + s[i] - '0') % (cnt - 1);
    num2 = (num2 * 2 + s[i] - '0') % (cnt + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cout << 1 + dfs(((num1 - quickmod(2, n - i - 1, cnt - 1)) % (cnt - 1) +
                       cnt - 1) %
                      (cnt - 1))
           << endl;
    else
      cout << 1 + dfs((num2 + (quickmod(2, n - i - 1, cnt + 1)) % (cnt + 1)) %
                      (cnt + 1))
           << endl;
  }
  return 0;
}