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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int cnt = 0;
  rep(i, n) {
    if (x[i] == '1')
      cnt++;
  }
  reverse(ALL(x));
  int t[2] = {};
  int base[2];
  base[0] = 1;
  base[1] = 1;
  rep(i, n) {
    if (x[i] == '1') {
      t[0] += base[0];
      t[0] %= cnt + 1;
      if (cnt - 1 > 0)
        t[1] += base[1];
      if (cnt - 1 > 0)
        t[1] %= cnt - 1;
    }
    base[0] *= 2;
    base[0] %= cnt + 1;
    if (cnt - 1 > 0)
      base[1] *= 2;
    if (cnt - 1 > 0)
      base[1] %= cnt - 1;
  }
  base[0] = 1;
  base[1] = 1;
  int ans[n];
  rep(i, n) {
    if (x[i] == '1' && cnt == 1) {
      ans[i] = 0;
      continue;
    }
    if (x[i] == '1') {
      int nxt;
      int cur = (t[1] - base[1] + cnt - 1) % (cnt - 1);
      int anss = 1;
      while (cur > 0) {
        nxt = cur % __builtin_popcount(cur);
        cur = nxt;
        anss++;
      }
      ans[i] = anss;
    } else {
      int nxt;
      int cur = (t[0] + base[0]) % (cnt + 1);
      int anss = 1;
      while (cur > 0) {
        nxt = cur % __builtin_popcount(cur);
        cur = nxt;
        anss++;
      }
      ans[i] = anss;
    }
    base[0] *= 2;
    base[0] %= cnt + 1;
    if (cnt - 1 > 0)
      base[1] *= 2;
    if (cnt - 1 > 0)
      base[1] %= cnt - 1;
  }
  reverse(ans, ans + n);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int cnt = 0;
  rep(i, n) {
    if (x[i] == '1')
      cnt++;
  }
  reverse(ALL(x));
  int t[2] = {};
  int base[2];
  base[0] = 1;
  base[1] = 1;
  rep(i, n) {
    if (x[i] == '1') {
      t[0] += base[0];
      t[0] %= cnt + 1;
      t[1] += base[1];
      t[1] %= cnt - 1;
    }
    base[0] *= 2;
    base[0] %= cnt + 1;
    base[1] *= 2;
    base[1] %= cnt - 1;
  }
  base[0] = 1;
  base[1] = 1;
  int ans[n];
  rep(i, n) {
    if (x[i] == '1') {
      int nxt;
      int cur = (t[1] - base[1] + cnt - 1) % (cnt - 1);
      int anss = 1;
      while (cur > 0) {
        nxt = cur % __builtin_popcount(cur);
        cur = nxt;
        anss++;
      }
      ans[i] = anss;
    } else {
      int nxt;
      int cur = (t[0] + base[0]) % (cnt + 1);
      int anss = 1;
      while (cur > 0) {
        nxt = cur % __builtin_popcount(cur);
        cur = nxt;
        anss++;
      }
      ans[i] = anss;
    }
    base[0] *= 2;
    base[0] %= cnt + 1;
    base[1] *= 2;
    base[1] %= cnt - 1;
  }
  reverse(ans, ans + n);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int a[210000], n, sum, f[210000], b1[210000], b2[210000], ans[210000], s1, s2;

int getach() {
  char ch = getchar();
  while (ch != '0' && ch != '1')
    ch = getchar();
  return ch - '0';
}

int work(int x) {
  int temp = x, sum = 0;
  while (temp) {
    if (temp & 1)
      sum++;
    temp >>= 1;
  }
  return x % sum;
}

int main() {
  // freopen("test.in","r",stdin);
  // freopen("test.out","w",stdout);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    a[i] = getach();
  for (int i = 1; i <= n; i++)
    sum += a[i];
  for (int i = 1; i <= sum; i++) {
    f[i] = f[work(i)] + 1;
  }
  if (sum > 1)
    for (int i = 1, temp = 1; i <= n; i++, temp = temp * 2 % (sum - 1))
      b1[i] = temp;
  for (int i = 1, temp = 1; i <= n; i++, temp = temp * 2 % (sum + 1))
    b2[i] = temp;
  for (int i = 1; i <= n; i++)
    if (a[i]) {
      if (sum > 1)
        s1 = (s1 + b1[n - i + 1]) % (sum - 1);
      s2 = (s2 + b2[n - i + 1]) % (sum + 1);
    }
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      if (sum <= 1)
        continue;
      int temp = (s1 - b1[n - i + 1] + sum - 1) % (sum - 1);
      ans[i] = f[temp] + 1;
    } else {
      int temp = (s2 + b2[n - i + 1]) % (sum + 1);
      ans[i] = f[temp] + 1;
    }
  }
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

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

int a[210000], n, sum, f[210000], b1[210000], b2[210000], ans[210000], s1, s2;

int getach() {
  char ch = getchar();
  while (ch != '0' && ch != '1')
    ch = getchar();
  return ch - '0';
}

int work(int x) {
  int temp = x, sum = 0;
  while (temp) {
    if (temp & 1)
      sum++;
    temp >>= 1;
  }
  return x % sum;
}

int main() {
  // freopen("test.in","r",stdin);
  // freopen("test.out","w",stdout);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    a[i] = getach();
  for (int i = 1; i <= n; i++)
    sum += a[i];
  for (int i = 1; i <= sum; i++) {
    f[i] = f[work(i)] + 1;
  }
  if (sum <= 1)
    for (int i = 1, temp = 1; i <= n; i++, temp = temp * 2 % (sum - 1))
      b1[i] = temp;
  for (int i = 1, temp = 1; i <= n; i++, temp = temp * 2 % (sum + 1))
    b2[i] = temp;
  for (int i = 1; i <= n; i++)
    if (a[i]) {
      if (sum <= 1)
        s1 = (s1 + b1[n - i + 1]) % (sum - 1);
      s2 = (s2 + b2[n - i + 1]) % (sum + 1);
    }
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      if (sum <= 1)
        continue;
      int temp = (s1 - b1[n - i + 1] + sum - 1) % (sum - 1);
      ans[i] = f[temp] + 1;
    } else {
      int temp = (s2 + b2[n - i + 1]) % (sum + 1);
      ans[i] = f[temp] + 1;
    }
  }
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;
const int INF = 1e9 + 7;

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

  int rp = 0, rm = 0;
  rep(i, n) {
    rp = (rp * 2 + (s[i] - '0')) % (m + 1);
    rm = (rm * 2 + (s[i] - '0')) % (m - 1);
  }

  vec ans(n, 1);
  int pp = 1, pm = 1;
  rep(i, n) {
    int r;
    if (s[n - i - 1] == '1') {
      r = rm - pm;
      if (r < 0)
        r += m - 1;
    } else {
      r = (rp + pp) % (m + 1);
      if (r > m + 1)
        r -= m + 1;
    }
    while (r > 0) {
      r %= __builtin_popcount(r);
      ans[n - i - 1]++;
    }

    pp = pp * 2 % (m + 1);
    pm = pm * 2 % (m - 1);
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;
const int INF = 1e9 + 7;

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

  int rp = 0, rm = 0;
  rep(i, n) {
    rp = (rp * 2 + (s[i] - '0')) % (m + 1);
    rm = (rm * 2 + (s[i] - '0')) % (m - 1);
  }

  vec ans(n, 1);
  int pp = 1, pm = 1;
  rep(i, n) {
    int r;
    if (s[n - i - 1] == '1') {
      r = rm - pm;
      if (r < 0)
        r += m - 1;
    } else {
      r = (rp + pp) % (m + 1);
      if (r > m + 1)
        r -= m + 1;
    }
    while (r > 0) {
      r %= __builtin_popcount(r);
      ans[n - i - 1]++;
    }

    pp = pp * 2 % (m + 1);
    pm = pm * 2 % (m - 1);
  }

  rep(i, n) { cout << ans[i] << endl; }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
int N;
string s;
int MODplus1[200005];
int MODminus1[200005];

int f(int x) {
  if (x == 0)
    return 0;
  int cnt = 0;
  rep(i, 20) {
    if ((1 << i) & x)
      cnt++;
  }
  return f(x % cnt) + 1;
}

int calc_mod(int x, int _mod[200005]) {
  if (x == 0)
    return 0;
  _mod[0] = 1;
  rep(i, N) {
    if (i == 0)
      continue;
    _mod[i] = 2 * _mod[i - 1] % x;
  }
  int sum = 0;
  rep(i, N) {
    if (s[i] == '1')
      sum += _mod[N - 1 - i];
    sum %= x;
  }
  return sum;
}

int main() {
  INCANT;
  cin >> N >> s;
  int cnt = 0;
  rep(i, N) if (s[i] == '1') cnt++;
  int sum1 = calc_mod(cnt + 1, MODplus1);
  int sum2 = calc_mod(cnt - 1, MODminus1);
  rep(i, N) {
    int sum;
    if (s[i] == '1') { // 1→0
      if (cnt - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      sum = sum2;
      sum -= MODminus1[N - 1 - i];
      sum += (cnt - 1);
      sum %= (cnt - 1);
    } else { // 0→1
      sum = sum1;
      sum += MODplus1[N - 1 - i];
      sum %= (cnt + 1);
    }
    cout << f(sum) + 1 << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
int N;
string s;
int MODplus1[200005];
int MODminus1[200005];

int f(int x) {
  if (x == 0)
    return 0;
  int cnt = 0;
  rep(i, 20) {
    if ((1 << i) & x)
      cnt++;
  }
  return f(x % cnt) + 1;
}

int calc_mod(int x, int _mod[200005]) {
  _mod[0] = 1;
  rep(i, N) {
    if (i == 0)
      continue;
    _mod[i] = 2 * _mod[i - 1] % x;
  }
  int sum = 0;
  rep(i, N) {
    if (s[i] == '1')
      sum += _mod[N - 1 - i];
    sum %= x;
  }
  return sum;
}

int main() {
  INCANT;
  cin >> N >> s;
  int cnt = 0;
  rep(i, N) if (s[i] == '1') cnt++;
  int sum1 = calc_mod(cnt + 1, MODplus1);
  int sum2 = calc_mod(cnt - 1, MODminus1);
  rep(i, N) {
    int sum;
    if (s[i] == '1') { // 1→0
      if (cnt - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      sum = sum2;
      sum -= MODminus1[N - 1 - i];
      sum += (cnt - 1);
      sum %= (cnt - 1);
    } else { // 0→1
      sum = sum1;
      sum += MODplus1[N - 1 - i];
      sum %= (cnt + 1);
    }
    cout << f(sum) + 1 << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  string s;
  cin >> n >> s;

  int one = count(ALL(s), '1');
  int num1 = 0;
  REP(i, n) num1 = (num1 * 2 + (s[i] - '0')) % (one + 1);

  int num2 = 0;
  if (one > 1) {
    REP(i, n) num2 = (num2 * 2 + (s[i] - '0')) % (one - 1);
  }

  REP(i, n) {
    int tmp = 0;
    if (s[i] == '0')
      tmp = (num1 + modpow(2, n - i - 1, one + 1)) % (one + 1);
    else {
      if (one > 1)
        tmp = (num2 - modpow(2, n - i - 1, one - 1) + one - 1) % (one - 1);
    }

    if (one == 1 && s[i] == '1') {
      cout << 0 << '\\n';
      continue;
    }

    int cnt = 1;
    while (tmp > 0) {
      int cp = tmp;
      int one2 = 0;
      while (cp > 0) {
        if (cp & 1)
          one2++;
        cp >>= 1;
      }
      tmp %= one2;
      cnt++;
    }
    cout << cnt << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  string s;
  cin >> n >> s;

  int one = count(ALL(s), '1');
  int num1 = 0;
  REP(i, n) num1 = (num1 * 2 + (s[i] - '0')) % (one + 1);

  int num2 = 0;
  if (one > 0) {
    REP(i, n) num2 = (num2 * 2 + (s[i] - '0')) % (one - 1);
  }

  REP(i, n) {
    int tmp = 0;
    if (s[i] == '0')
      tmp = (num1 + modpow(2, n - i - 1, one + 1)) % (one + 1);
    else {
      if (one > 0)
        tmp = (num2 - modpow(2, n - i - 1, one - 1) + one - 1) % (one - 1);
    }

    int cnt = 1;
    while (tmp > 0) {
      int cp = tmp;
      int one2 = 0;
      while (cp > 0) {
        if (cp & 1)
          one2++;
        cp >>= 1;
      }
      tmp %= one2;
      cnt++;
    }
    cout << cnt << '\\n';
  }

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

#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(false)
#define itt cin.tie(0)
#define now cout.tie(0)
#define N 200010
using namespace std;
ll n, nn[2], md[N][2];
string s;
ll f(ll v, ll nd, ll sd, ll sx) {
  ll ret = 1, x = (nn[nd] + sd + sx * md[v][nd]) % sd;
  while (x != 0) {
    x = x % __builtin_popcount(x);
    ret++;
  }
  return ret;
}
int main() {
  ll i, bb = 0;
  fast;
  itt;
  now;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      bb++;
    }
  }
  md[0][0] = 1 % (bb + 1);
  if (bb > 1) {
    md[0][1] = 1 % (bb - 1);
  }
  for (i = 1; i <= n; i++) {
    md[i][0] = (md[i - 1][0] * 2) % (bb + 1);
    if (bb > 1) {
      md[i][1] = (md[i - 1][1] * 2) % (bb - 1);
    }
  }
  nn[0] = nn[1] = 0;
  for (i = 0; i < n; i++) {
    nn[0] = (nn[0] * 2 + (s[i] - '0')) % (bb + 1);
    if (bb > 1) {
      nn[1] = (nn[1] * 2 + (s[i] - '0')) % (bb - 1);
    }
  }
  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      cout << f(n - i - 1, 0, bb + 1, 1) << endl;
    } else if (bb > 1) {
      cout << f(n - i - 1, 1, bb - 1, -1) << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(false)
#define itt cin.tie(0)
#define now cout.tie(0)
#define N 200010
using namespace std;
ll n, nn[2], md[N][2];
string s;
ll f(ll v, ll nd, ll sd, ll sx) {
  ll ret = 1, x = (nn[nd] + sd + sx * md[v][nd]) % sd;
  while (x != 0) {
    x = x % __builtin_popcount(x);
    ret++;
  }
  return ret;
}
int main() {
  ll i, bb = 0;
  fast;
  itt;
  now;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      bb++;
    }
  }
  md[0][0] = 1 % (bb + 1);
  if (bb != 0) {
    md[0][1] = 1 % (bb - 1);
  }
  for (i = 1; i <= n; i++) {
    md[i][0] = (md[i - 1][0] * 2) % (bb + 1);
    if (bb != 0) {
      md[i][1] = (md[i - 1][1] * 2) % (bb - 1);
    }
  }
  nn[0] = nn[1] = 0;
  for (i = 0; i < n; i++) {
    nn[0] = (nn[0] * 2 + (s[i] - '0')) % (bb + 1);
    if (bb != 0) {
      nn[1] = (nn[1] * 2 + (s[i] - '0')) % (bb - 1);
    }
  }
  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      cout << f(n - i - 1, 0, bb + 1, 1) << endl;
    } else {
      cout << f(n - i - 1, 1, bb - 1, -1) << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

// Use valgrind once if the submission doesn't go through. It can detect
// uninitialized variables and see where they could be an issue.
#include <bits/stdc++.h>
using namespace std;
int pCount(int n) {
  int c = 0;
  while (n) {
    if (n & 1)
      c++;
    n >>= 1;
  }
  return c;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int N, p, p2 = 1, rp{0}, rm{0};
  string s;
  cin >> N >> s;
  vector<int> rem2p, rem2m, dp, ans;
  p = count(s.begin(), s.end(), '1');
  dp.resize(p + 1);
  dp[0] = 0;
  if (dp.size() > 1)
    dp[1] = 1;
  for (int i = 2; i < dp.size(); i++) {
    dp[i] = 1 + dp[i % pCount(i)];
  }
  for (auto i = s.rbegin(); i != s.rend(); i++) {
    *i -= '0';
    rem2p.push_back(p2);
    if (*i) {
      rp += p2;
      rp %= p + 1;
    }
    p2 <<= 1;
    p2 %= p + 1;
  }
  if (p > 1) {
    p2 = 1;
    for (auto i = s.rbegin(); i != s.rend(); i++) {
      rem2m.push_back(p2);
      if (*i) {
        rm += p2;
        rm %= p - 1;
      }
      p2 <<= 1;
      p2 %= p - 1;
    }
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[s.size() - 1 - i] && p != 1) {
      int t = rm - rem2m[i];
      if (t < 0) {
        t += p - 1;
      }
      ans.push_back(1 + dp[t]);
    } else if (s[s.size() - 1 - i] && p == 1) {
      ans.push_back(0);
    } else {
      int t = rp + rem2p[i];
      if (t > p) {
        t -= p + 1;
      }
      ans.push_back(1 + dp[t]);
    }
  }
  for (auto i = ans.rbegin(); i != ans.rend(); i++) {
    cout << *i << '\\n';
  }
}


Error: 0

Fixed submission:

// Use valgrind once if the submission doesn't go through. It can detect
// uninitialized variables and see where they could be an issue.
#include <bits/stdc++.h>
using namespace std;
int pCount(int n) {
  int c = 0;
  while (n) {
    if (n & 1)
      c++;
    n >>= 1;
  }
  return c;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int N, p, p2 = 1, rp{0}, rm{0};
  string s;
  cin >> N >> s;
  vector<int> rem2p, rem2m, dp, ans;
  p = count(s.begin(), s.end(), '1');
  dp.resize(p + 1);
  dp[0] = 0;
  dp[1] = 1;
  for (int i = 2; i < dp.size(); i++) {
    dp[i] = 1 + dp[i % pCount(i)];
  }
  for (auto i = s.rbegin(); i != s.rend(); i++) {
    *i -= '0';
    rem2p.push_back(p2);
    if (*i) {
      rp += p2;
      rp %= p + 1;
    }
    p2 <<= 1;
    p2 %= p + 1;
  }
  if (p > 1) {
    p2 = 1;
    for (auto i = s.rbegin(); i != s.rend(); i++) {
      rem2m.push_back(p2);
      if (*i) {
        rm += p2;
        rm %= p - 1;
      }
      p2 <<= 1;
      p2 %= p - 1;
    }
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[s.size() - 1 - i]) {
      int t = rm - rem2m[i];
      if (t < 0) {
        t += p - 1;
      }
      ans.push_back(1 + dp[t]);
    } else {
      int t = rp + rem2p[i];
      if (t > p) {
        t -= p + 1;
      }
      ans.push_back(1 + dp[t]);
    }
  }
  for (auto i = ans.rbegin(); i != ans.rend(); i++) {
    cout << *i << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 100;
int mem[N], num1, pown[N], pown1[N];
char s[N];
int f(int x) {
  if (x == 0)
    return 0;
  if (mem[x])
    return mem[x];
  int t = x, cnt = 0;
  while (t)
    cnt += (t & 1), t >>= 1;
  return mem[x] = f(x % cnt) + 1;
}
int main() {
  int n, modn = 0, modn1 = 0;
  scanf("%d%s", &n, s);
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      num1++;
  pown[0] = 1 % (num1 + 1);
  if (num1 - 1)
    pown1[0] = 1 % (num1 - 1);
  for (int i = 1; i <= n; i++) {
    pown[i] = pown[i - 1] * 2 % (num1 + 1);
    if (num1 - 1)
      pown1[i] = pown1[i - 1] * 2 % (num1 - 1);
  }
  for (int i = 0; i < n; i++) {
    modn = (modn * 2 + s[i] - '0') % (num1 + 1);
    if (num1 - 1)
      modn1 = (modn1 * 2 + s[i] - '0') % (num1 - 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int t = (modn + pown[n - i - 1]) % (num1 + 1);
      printf("%d\\n", f(t) + 1);
    } else {
      if (num1 == 1)
        printf("0\\n");
      else {
        int t = (modn1 - pown1[n - i - 1] + num1 - 1) % (num1 - 1);
        printf("%d\\n", f(t) + 1);
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int mem[N], num1;
char s[N];
int qpow(int a, int n, int p) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = res * a % p;
    a = a * a % p, n >>= 1;
  }
  return res;
}
int f(int x) {
  if (x == 0)
    return 0;
  if (mem[x])
    return mem[x];
  int t = x, cnt = 0;
  while (t)
    cnt += (t & 1), t >>= 1;
  return mem[x] = f(x % cnt) + 1;
}
int main() {
  int n, modn = 0, modn1 = 0;
  scanf("%d%s", &n, s);
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      num1++;
  for (int i = 0; i < n; i++) {
    modn = (modn * 2 + s[i] - '0') % (num1 + 1);
    if (num1 - 1)
      modn1 = (modn1 * 2 + s[i] - '0') % (num1 - 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int t = (modn + qpow(2, n - i - 1, num1 + 1)) % (num1 + 1);
      printf("%d\\n", f(t) + 1);
    } else {
      if (num1 == 1)
        printf("0\\n");
      else {
        int t = (modn1 - qpow(2, n - i - 1, num1 - 1) + num1 - 1) % (num1 - 1);
        printf("%d\\n", f(t) + 1);
      }
    }
  }
  return 0;
}