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

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Function to return the value of (str % k)
int getMod(string str, int n, int k) {

  // pwrTwo[i] will store ((2^i) % k)
  int pwrTwo[n];
  pwrTwo[0] = 1 % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }

  // To store the result
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (str[j] == '1') {

      // Add the current power of 2
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}

int main() {
  int n, c;
  string str;
  cin >> n;
  cin >> str;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1') {
      cnt++;
    }
  }
  int pwrTwo[n], pwrTwo1[n];
  int k = cnt + 1;
  pwrTwo[0] = 1 % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }

  // To store the result
  int res = 0, res1 = 0;
  int i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (str[j] == '1') {

      // Add the current power of 2
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }

  if (cnt > 1) {
    k = cnt - 1;
    pwrTwo1[0] = 1 % k;
    for (int i = 1; i < n; i++) {
      pwrTwo1[i] = pwrTwo1[i - 1] * (2 % k);
      pwrTwo1[i] %= k;
    }

    int i = 0;
    int j = n - 1;
    while (i < n) {

      // If current bit is 1
      if (str[j] == '1') {

        // Add the current power of 2
        res1 += (pwrTwo1[i]);
        res1 %= k;
      }
      i++;
      j--;
    }
  }
  // cout<<"res "<<res<<"res1 "<<res1<<endl;
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    int r = 0;
    int ct = 0;
    // cout<<endl;
    if (str[i] == '0') {
      // cout<<"res "<<res<<"pwrtwo "<<pwrTwo[j]<<endl;
      r = res + (pwrTwo[j]);
      // cout<<"before "<<r<<endl;
      r %= (cnt + 1);
      // cout<<"after "<<r<<endl;
      ct++;
    } else {
      if (cnt > 1) {
        // cout<<"res1 "<<res1<<"pwrtwo "<<pwrTwo1[j]<<endl;
        r = res1 - (pwrTwo1[j]) + (cnt - 1);
        // cout<<"before "<<r<<endl;
        r %= (cnt - 1);
        // cout<<"after "<<r<<endl;
        ct++;
      }
    }

    // cout<<r<<endl;
    while (r != 0) {
      // cout<<"here"<<endl;
      int res = __builtin_popcount(r);
      r = r % res;
      ct++;
    }
    cout << ct << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n, c;
  string str;
  cin >> n;
  cin >> str;
  int r = stoi(str, 0, 2);
  for (int i = 0; i < n; i++) {
    char ch = str[i];
    if (str[i] == '0') {
      str[i] = '1';
    } else {
      str[i] = '0';
    }
    int r = stoi(str, 0, 2);
    int cnt = 0;
    while (1) {
      int res = __builtin_popcount(r);
      if (res == 0)
        break;
      int g = r % res;
      cnt++;
      if (g == 0)
        break;

      r = g;
    }
    cout << cnt << endl;
    str[i] = ch;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;
map<int, int> m;
string s;
int n;
int culc(int num) {
  if (num == 0)
    return 0;
  if (num == 1)
    return 1;
  if (m.count(num) == 1)
    return m[num];
  int c = __builtin_popcount(num);
  return m[num] = culc(num % c) + 1;
}
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> n;
  int count = 0;
  cin >> s;
  vector<vector<int>> cu(2, vector<int>(n + 2));
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      count++;
  }
  cu[0][0] = 1;
  cu[1][0] = 1;
  for (int i = 0; i < n; ++i) {
    cu[0][i + 1] = cu[0][i] * 2 % (count + 1);
    if (count > 1)
      cu[1][i + 1] = cu[1][i] * 2 % (count - 1);
  }
  vector<int> sum(2);
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      (sum[0] += cu[0][n - i - 1]) %= (count + 1);
      if (count > 1)
        (sum[1] += cu[1][n - i - 1]) %= (count - 1);
    }
  }
  vector<int> ret(n);
  for (int i = 0; i < n; ++i) {
    int tmp = 0;
    if (s[i] == '0')
      tmp = (sum[0] + cu[0][n - i - 1]) % (count + 1);
    else {
      if (count == 1)
        continue;
      tmp =
          ((sum[1] - cu[1][n - i - 1]) % (count - 1) + count - 1) % (count - 1);
    }
    ret[i] = culc(tmp) + 1;
  }
  for (int i = 0; i < n; ++i)
    cout << ret[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;
map<int, int> m;
string s;
int n;
int culc(int num) {
  if (num == 0)
    return 0;
  if (num == 1)
    return 1;
  if (m.count(num) == 1)
    return m[num];
  int c = __builtin_popcount(num);
  return m[num] = culc(num % c) + 1;
}
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> n;
  int count = 0;
  cin >> s;
  vector<vector<int>> cu(2, vector<int>(n + 2));
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      count++;
  }
  cu[0][0] = 1;
  cu[1][0] = 1;
  for (int i = 0; i < n; ++i) {
    cu[0][i + 1] = cu[0][i] * 2 % (count + 1);
    if (count > 1)
      cu[1][i + 1] = cu[1][i] * 2 % (count - 1);
  }
  vector<int> sum(2);
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      (sum[0] += cu[0][n - i - 1]) %= (count + 1);
      (sum[1] += cu[1][n - i - 1]) %= (count - 1);
    }
  }
  vector<int> ret(n);
  for (int i = 0; i < n; ++i) {
    int tmp = 0;
    if (s[i] == '0')
      tmp = (sum[0] + cu[0][n - i - 1]) % (count + 1);
    else {
      if (count == 1)
        continue;
      tmp =
          ((sum[1] - cu[1][n - i - 1]) % (count - 1) + count - 1) % (count - 1);
    }
    ret[i] = culc(tmp) + 1;
  }
  for (int i = 0; i < n; ++i)
    cout << ret[i] << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int popcnt(unsigned x) {
  int ret = 0;
  for (int i = 0; i < 32; i++) {
    ret += x & 1;
    x >>= 1;
  }
  return ret;
}

int f(unsigned x) {
  int ret = 0;
  while (x != 0) {
    x %= popcnt(x);
    ret++;
  }
  return ret;
}

int str_mod(const string &x, int m) {
  int ret = 0;
  if (m == 0)
    return ret;
  for (char c : x) {
    ret <<= 1;
    ret += c - '0';
    ret %= m;
  }
  return ret;
}

vector<int> tpow_mod(int n, int m) {
  if (m == 0)
    return vector<int>();
  vector<int> ret(n, 1 % m);
  for (int i = 1; i < n; i++)
    ret[i] = ret[i - 1] * 2 % m;
  return ret;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  int pc = count(x.begin(), x.end(), '1');
  int xmp = str_mod(x, pc + 1);
  int xmm = str_mod(x, pc - 1);
  vector<int> tmp = tpow_mod(n, pc + 1);
  vector<int> tmm = tpow_mod(n, pc - 1);
  reverse(x.begin(), x.end());
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      if (pc == 1) {
        cout << 0 << '\\n';
      } else {
        int m = pc - 1;
        cout << (1 + f((xmm + m - tmm[i]) % m)) << '\\n';
      }
    } else {
      int m = pc + 1;
      cout << (1 + f((xmp + tmp[i]) % m)) << '\\n';
    }
  }
}

Error: 0

Fixed submission:

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

int f(unsigned x) {
  int ret = 0;
  while (x != 0) {
    x %= __builtin_popcount(x);
    ret++;
  }
  return ret;
}

int str_mod(string &x, int m) {
  int ret = 0;
  if (m == 0)
    return ret;
  for (int i = x.length() - 1; i >= 0; i--) {
    ret <<= 1;
    ret += x[i] - '0';
    ret %= m;
  }
  return ret;
}

vector<int> tpow_mod(int n, int m) {
  vector<int> ret(n, 1 % m);
  if (m == 0)
    return ret;
  for (int i = 1; i < n; i++) {
    ret[i] = ret[i - 1] << 1;
    ret[i] %= m;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int pc = count(x.begin(), x.end(), '1');
  int xmp = str_mod(x, pc + 1);
  int xmm = str_mod(x, pc - 1);
  vector<int> tpp = tpow_mod(n, pc + 1);
  vector<int> tpm = tpow_mod(n, pc - 1);
  for (int i = 0; i < n; i++) {
    int d = n - i - 1;
    int m, y;
    if (x[i] == '1') {
      if (pc == 1) {
        cout << 0 << '\\n';
        continue;
      } else {
        m = pc - 1;
        y = xmm - tpm[d];
      }
    } else {
      m = pc + 1;
      y = xmp + tpp[d];
    }
    y = (y % m + m) % m;
    cout << f(y) + 1 << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
using llong = long long;
#define inv(X) ((X) == '1' ? '0' : '1')

llong n;
string x;

llong bitcnt(llong a) {
  llong ret = 0;
  while (a > 0) {
    if (a & 1)
      ret++;
    a >>= 1;
  }
  return ret;
}

llong f(llong x) {
  llong cnt = 0;
  while (bitcnt(x) > 0) {
    x %= bitcnt(x);
    cnt++;
  }
  return cnt;
}

llong to_int(string &s, llong mod) {
  llong ret = 0;
  for (auto ch : s) {
    ret *= 2;
    if (ch == '1')
      ret |= 1;
    ret %= mod;
  }
  return ret;
}

llong pow_mod(llong n, llong m, llong mod) {
  llong ret = 1;
  while (m > 0) {
    if (m & 1) {
      ret *= n;
      ret %= mod;
    }
    n *= n;
    n %= mod;
    m >>= 1;
  }
  return ret;
}

int main() {
  cin >> n >> x;

  llong cnt = count(x.begin(), x.end(), '1');
  llong x1 = to_int(x, cnt + 1);
  llong x2;
  if (cnt > 1)
    x2 = to_int(x, cnt - 1);

  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      llong s = x1 + pow_mod(2, n - i - 1, cnt + 1);
      s %= cnt + 1;
      cout << f(s) + 1 << '\\n';
    } else {
      if (cnt - 1 == 0) {
        cout << 0 << '\\n';
        continue;
      }
      llong s = x2 - pow_mod(2, n - i - 1, cnt - 1);
      s %= cnt - 1;
      s += cnt - 1;
      s %= cnt - 1;
      cout << f(s) + 1 << '\\n';
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
using llong = long long;
#define inv(X) ((X) == '1' ? '0' : '1')

llong n;
string x;

llong bitcnt(llong a) {
  llong ret = 0;
  while (a > 0) {
    if (a & 1)
      ret++;
    a >>= 1;
  }
  return ret;
}

llong f(llong x) {
  llong cnt = 0;
  while (bitcnt(x) > 0) {
    x %= bitcnt(x);
    cnt++;
  }
  return cnt;
}

llong to_int(string &s, llong mod) {
  llong ret = 0;
  for (auto ch : s) {
    ret *= 2;
    if (ch == '1')
      ret |= 1;
    ret %= mod;
  }
  return ret;
}

llong pow_mod(llong n, llong m, llong mod) {
  llong ret = 1;
  while (m > 0) {
    if (m & 1) {
      ret *= n;
      ret %= mod;
    }
    n *= n;
    n %= mod;
    m >>= 1;
  }
  return ret;
}

int main() {
  cin >> n >> x;

  llong cnt = count(x.begin(), x.end(), '1');
  llong x1 = to_int(x, cnt + 1);
  llong x2 = to_int(x, cnt - 1);

  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      llong s = x1 + pow_mod(2, n - i - 1, cnt + 1);
      s %= cnt + 1;
      cout << f(s) + 1 << '\\n';
    } else {
      if (cnt - 1 == 0) {
        cout << 0 << '\\n';
        continue;
      }
      llong s = x2 - pow_mod(2, n - i - 1, cnt - 1);
      s %= cnt - 1;
      s += cnt - 1;
      s %= cnt - 1;
      cout << f(s) + 1 << '\\n';
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 2e5 + 7;
typedef long long LL;

int n, sum;
string s;

int quick(int a, int b, int mod) {
  LL ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = (LL)a * a % mod;
    b >>= 1;
  }
  return ans;
}

int fun(int n) {
  int ans = 0;
  while (n) {
    ans++;
    n = n & (n - 1);
  }
  return ans;
}

int main() {
  cin >> n >> s;
  for (auto c : s)
    if (c == '1')
      sum++;
  int cnt1 = 0, cnt2 = 0, p1 = sum + 1, p2 = sum - 1;

  for (int i = 0; i < n; i++)
    if (s[i] == '1') {
      cnt1 = (cnt1 + quick(2, n - i - 1, p1)) % p1;
      if (p2 != 0)
        cnt2 = (cnt2 + quick(2, n - i - 1, p2)) % p2;
    }
  int t, ans = 0;
  for (int i = 0; i < n; i++) {
    ans = 1;
    if (s[i] == '0')
      t = (cnt1 + quick(2, n - i - 1, p1) + p1) % p1;
    else if (p2 == 0) {
      cout << 0 << "\\n";
      continue;
    } else
      t = (cnt2 - quick(2, n - i - 1, p2) + p2) % p2;
    if (!t)
      cout << ans << "\\n";
    else {
      while (t)
        ans++, t = t % fun(t);
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int N = 2e5 + 7;
typedef long long LL;

int n, sum;
string s;

int quick(int a, int b, int mod) {
  LL ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = (LL)a * a % mod;
    b >>= 1;
  }
  return ans;
}

int fun(int n) {
  int ans = 0;
  while (n) {
    ans++;
    n = n & (n - 1);
  }
  return ans;
}

int main() {
  cin >> n >> s;
  for (auto c : s)
    if (c == '1')
      sum++;
  int cnt1 = 0, cnt2 = 0, p1 = sum + 1, p2 = sum - 1;
  for (int i = 0; i < n; i++)
    if (s[i] == '1') {
      cnt1 = (cnt1 + quick(2, n - i - 1, p1)) % p1;
      cnt2 = (cnt2 + quick(2, n - i - 1, p2)) % p2;
    }
  int t, ans = 0;
  for (int i = 0; i < n; i++) {
    ans = 1;
    if (s[i] == '1')
      t = (cnt2 - quick(2, n - i - 1, p2) + p2) % p2;
    else
      t = (cnt1 + quick(2, n - i - 1, p1) + p1) % p1;
    if (!t)
      cout << ans << "\\n";
    else {
      while (t)
        ans++, t = t % fun(t);
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
  int N;
  std::string X;
  std::cin >> N >> X;
  const int popcount = std::count(X.begin(), X.end(), '1');
  std::map<int, int> x_map;
  std::vector<std::map<int, int>> digit_map(N);
  for (int mod = std::max(1, popcount - 1); mod <= popcount + 1; ++mod) {
    int remainder = 0;
    for (const char digit : X) {
      remainder = (remainder * 2 + digit - '0') % mod;
    }
    x_map[mod] = remainder;
    int digit = 1;
    for (int i = 0; i < N; ++i) {
      digit_map[N - i - 1][mod] = digit;
      digit = digit * 2 % mod;
    }
  }
  for (int i = 0; i < N; ++i) {
    const int mod = popcount + (X[i] == '0' ? 1 : -1);
    int f = 0;
    if (mod > 0) {
      f = 1;
      int n = x_map[mod];
      if (X[i] == '0') {
        n = (n + digit_map[i][mod]) % mod;
      } else {
        n = (n - digit_map[i][mod] + mod) % mod;
      }
      while (n > 0) {
        ++f;
        n = n % __builtin_popcount(n);
      }
    }
    std::cout << f << std::endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
  int N;
  std::string X;
  std::cin >> N >> X;
  const int popcount = std::count(X.begin(), X.end(), '1');
  std::map<int, int> x_map;
  std::vector<std::map<int, int>> digit_map(N);
  for (int mod = std::max(1, popcount - 1); mod <= popcount + 1; ++mod) {
    int remainder = 0;
    for (const char digit : X) {
      remainder = (remainder * 2 + digit - '0') % mod;
    }
    x_map[mod] = remainder;
    int digit = 1;
    for (int i = 0; i < N; ++i) {
      digit_map[N - i - 1][mod] = digit;
      digit = digit * 2 % mod;
    }
  }
  for (int i = 0; i < N; ++i) {
    const int mod = popcount + (X[i] == '0' ? 1 : -1);
    int f = 0;
    if (popcount > 0) {
      f = 1;
      int n = x_map[mod];
      if (X[i] == '0') {
        n = (n + digit_map[i][mod]) % mod;
      } else {
        n = (n - digit_map[i][mod] + mod) % mod;
      }
      while (n > 0) {
        ++f;
        n = n % __builtin_popcount(n);
      }
    }
    std::cout << f << std::endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> x(n), ans(n);
  rep(i, n) x[i] = s[i] - '0';
  int pc = 0;
  rep(i, n) pc += x[i];

  rep(b, 2) {
    int npc = pc;
    if (b == 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) {
      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;
    }
  }

  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
  ll n, ans = 0, cnt;
  string s, t;
  cin >> n >> s;
  vector<ll> v(n);
  unordered_map<string, ll> mp;

  rep(i, n) {
    t = s;
    if (t[i] == '0')
      t[i] = '1';
    else
      t[i] = '0';
    // cout << "t " << t << endl;

    // cnt = 0;
    // rep(j, n) if (t[i] == '1') cnt++;

    // ll x = stoll(t);
    // cout << "x " << x << endl;
    ll x = 0, m = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (t[i] == '1')
        x += m;
      m *= 2;
    }
    cnt = __builtin_popcount(x);
    ans = 0;
    ll o = x;

    while (x) {
      x = x % cnt, cnt = __builtin_popcount(x), ans++;
      if (v[x]) {
        v[o] = ans + v[x];
        ans = v[o];
        break;
      }
    }
    cout << ans << endl;
  }

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

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

long long f(long long a) {
  if (a == 0) {
    return 0;
  }
  bitset<60> b(a);
  return 1 + f(a % b.count());
}

int main() {
  size_t N;
  string X;
  cin >> N >> X;

  size_t count = 0;
  for (char x : X) {
    if (x == '1') {
      count++;
    }
  }

  reverse(X.begin(), X.end());
  long long digit_minus = 1;
  long long minus = 0;
  vector<long long> m;

  long long digit_plus = 1;
  long long plus = 0;
  vector<long long> p;

  for (char x : X) {
    if (x == '1') {
      minus += digit_minus;
      plus += digit_plus;
    }

    m.push_back(digit_minus);
    p.push_back(digit_plus);

    if (count > 1) {
      minus %= count - 1;
    }
    digit_minus *= 2;
    if (count > 1) {
      digit_minus %= count - 1;
    }

    plus %= count + 1;
    digit_plus *= 2;
    digit_plus %= count + 1;
  }

  vector<long long> ans(N, 0);
  for (size_t i = 0; i < N; i++) {
    long long a;
    if (X[i] == '1') {
      a = minus;
      a -= m[i];
      a += count - 1;
      a += count - 1;
      if (count > 1) {
        a %= count - 1;
      }
    } else {
      a = plus;
      a += p[i];
      a %= count + 1;
    }
    if (count == 1 && X[i] == '1') {
      ans[i] = 0;
    } else {
      ans[i] = 1 + f(a);
    }
  }
  reverse(ans.begin(), ans.end());
  for (long long a : ans) {
    cout << a << endl;
  }
}


Error: 0

Fixed submission:

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

long long f(long long a) {
  if (a == 0) {
    return 0;
  }
  bitset<60> b(a);
  return 1 + f(a % b.count());
}

int main() {
  size_t N;
  string X;
  cin >> N >> X;

  size_t count = 0;
  for (char x : X) {
    if (x == '1') {
      count++;
    }
  }

  reverse(X.begin(), X.end());
  long long digit_minus = 1;
  long long minus = 0;
  vector<long long> m;

  long long digit_zero = 1;
  long long zero = 0;
  vector<long long> z;

  long long digit_plus = 1;
  long long plus = 0;
  vector<long long> p;

  for (char x : X) {
    if (x == '1') {
      minus += digit_minus;
      zero += digit_zero;
      plus += digit_plus;
    }

    m.push_back(digit_minus);
    z.push_back(digit_zero);
    p.push_back(digit_plus);

    minus %= count - 1;
    zero %= count;
    plus %= count + 1;

    digit_minus *= 2;
    digit_minus %= count - 1;

    digit_zero *= 2;
    digit_zero %= count;

    digit_plus *= 2;
    digit_plus %= count + 1;
  }

  vector<long long> ans(N, 0);
  for (size_t i = 0; i < N; i++) {
    long long a;
    if (X[i] == '1') {
      a = minus;
      a -= m[i];
      a %= count - 1;
    } else {
      a = plus;
      a += p[i];
      a %= count + 1;
    }
    if (count == 1 && X[i] == '1') {
      ans[i] = 0;
    } else {
      ans[i] = 1 + f(a);
    }
  }
  reverse(ans.begin(), ans.end());
  for (long long a : ans) {
    cout << a << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int N, ans;
string X;

int main(void) {
  cin >> N >> X;
  bitset<2 * 100000> b(X);
  int p = b.count();

  int mod[2][N + 10], t = 1;
  for (int i = 0; i < N + 10; i++) {
    mod[0][i] = t;
    t <<= 1;
    t %= p + 1;
  }
  t = 1;
  if (p != 1) {
    for (int i = 0; i < N + 10; i++) {
      mod[1][i] = t;
      t <<= 1;
      t %= p - 1;
    }
  }

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

  int mp = 0, mm = 0;
  for (int i = 0; i < N; i++) {
    // cout<<X[i]<<endl;
    if (X[i] == '1')
      mp += mod[0][i];
    mp %= p + 1;
  }
  if (p != 1) {
    for (int i = 0; i < N; i++) {
      // cout<<X[i]<<endl;
      if (X[i] == '1')
        mm += mod[1][i];
      mm %= p - 1;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    int tmp;
    if (X[i] == '0') {
      tmp = mp;
      tmp += mod[0][i];
      tmp %= p + 1;
    } else {
      if (p == 1) {
        cout << 0 << endl;
        continue;
      }
      tmp = mm;
      tmp -= mod[1][i];
      while (tmp < 0)
        tmp += (p - 1);
      tmp %= p - 1;
    }
    int ret;
    for (ret = 1; tmp != 0; ret++) {
      tmp = tmp % (__builtin_popcount(tmp));
    }
    cout << ret << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, ans;
string X;

int main(void) {
  cin >> N >> X;
  bitset<2 * 100000> b(X);
  int p = b.count();

  int mod[2][N + 10], t = 1;
  for (int i = 0; i < N + 10; i++) {
    mod[0][i] = t;
    t <<= 1;
    t %= p + 1;
  }
  t = 1;
  for (int i = 0; i < N + 10; i++) {
    mod[1][i] = t;
    t <<= 1;
    t %= p - 1;
  }
  // cout<<"mod"<<endl;
  // for(int i=0;i<N;i++)cout<<mod[i]<<endl;
  // cout<<":mod"<<endl;

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

  int mp = 0, mm = 0;
  for (int i = 0; i < N; i++) {
    // cout<<X[i]<<endl;
    if (X[i] == '1')
      mp += mod[0][i];
    mp %= p + 1;
  }
  for (int i = 0; i < N; i++) {
    // cout<<X[i]<<endl;
    if (X[i] == '1')
      mm += mod[1][i];
    mm %= p - 1;
  }

  for (int i = N - 1; i >= 0; i--) {
    int tmp;
    if (X[i] == '0') {
      tmp = mp;
      tmp += mod[0][i];
      tmp %= p + 1;
    } else {
      tmp = mm;
      tmp -= mod[1][i];
      while (tmp < 0)
        tmp += (p - 1);
      tmp %= p - 1;
    }
    int ret;
    for (ret = 1; tmp != 0; ret++) {
      tmp = tmp % (__builtin_popcount(tmp));
    }
    cout << ret << endl;
  }
  return 0;
}