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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[200001];
ll power(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  ll q = power(x, y / 2, mod) % mod;
  if (y % 2 == 0)
    return (q * q) % mod;
  else
    return ((q * q) % mod * x) % mod;
}
ll func(ll x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return 1;
  if (dp[x] != -1)
    return dp[x];
  ll ret = func(x % __builtin_popcountll(x)) + 1;
  return dp[x] = ret;
}
ll modulo(string s, ll x) {
  ll r = 0;
  for (ll i = 0; i < s.length(); i++) {
    {
      r = r * 2 + s[i] - '0';
      r %= x;
    }
  }
  return r;
}
int main() {
  ll l, r, i, j, k, n, m, x;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (i = 0; i <= 200000; i++) {
    if (dp[i] == -1)
      dp[i] = func(i);
  }
  //	for(i=0;i<=10;i++)
  //	cout<<dp[i]<<' ';
  string s;
  cin >> s;
  ll ctr = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1')
      ctr++;
  ll a = modulo(s, ctr + 1);
  ll b = 0;
  if (ctr > 1)
    b = modulo(s, ctr - 1);
  // cout<<ctr<<endl;
  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll mod = ctr + 1;
      ll q = power(2, n - 1 - i, mod);
      s[i] = '1';
      s[i] = '0';
      q %= mod;
      cout << dp[(a + q) % mod] + 1 << endl;
    } else {
      ll mod = ctr - 1;
      if (mod == 0) {
        cout << 0 << endl;
        continue;
      }
      ll q = power(2, n - 1 - i, mod);
      q %= mod;
      q = b - q;
      if (q < 0)
        q += mod;
      else
        q %= mod;
      cout << dp[q] + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[200001];
ll power(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  ll q = power(x, y / 2, mod) % mod;
  if (y % 2 == 0)
    return (q * q) % mod;
  else
    return ((q * q) % mod * x) % mod;
}
ll func(ll x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return 1;
  if (dp[x] != -1)
    return dp[x];
  ll ret = func(x % __builtin_popcountll(x)) + 1;
  return dp[x] = ret;
}
ll modulo(string s, ll x) {
  ll r = 0;
  for (ll i = 0; i < s.length(); i++) {
    {
      r = r * 2 + s[i] - '0';
      r %= x;
    }
  }
  return r;
}
int main() {
  ll l, r, i, j, k, n, m, x;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (i = 0; i <= 200000; i++) {
    if (dp[i] == -1)
      dp[i] = func(i);
  }
  //	for(i=0;i<=10;i++)
  //	cout<<dp[i]<<' ';
  string s;
  cin >> s;
  ll ctr = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1')
      ctr++;
  ll a = modulo(s, ctr + 1);
  ll b = modulo(s, ctr - 1);
  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll mod = ctr + 1;
      ll q = power(2, n - 1 - i, mod);
      s[i] = '1';
      s[i] = '0';
      q %= mod;
      cout << dp[(a + q) % mod] + 1 << endl;
    } else {
      ll mod = ctr - 1;
      if (ctr == 0) {
        cout << 0 << endl;
        continue;
      }
      ll q = power(2, n - 1 - i, mod);
      q %= mod;
      q = b - q;
      if (q < 0)
        q += mod;
      else
        q %= mod;
      cout << dp[q] + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, popc, cpop, rmd1, rmd2, res[200100], ff[200100];
string s;
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> s;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1')
      popc++;
  if (popc != 1) {
    rmd1 = 0;
    for (int i = 0; i < s.size(); i++)
      rmd1 = (rmd1 * 2 + s[i] - '0') % (popc - 1);
  }
  rmd2 = 0;
  for (int i = 0; i < s.size(); i++)
    rmd2 = (rmd2 * 2 + s[i] - '0') % (popc + 1);
  int x1 = 1, x2 = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == '0') {
      res[i] = (rmd2 + x2 + popc + 1) % (popc + 1);
    }
    if (s[i] == '1') {
      if (popc != 1)
        res[i] = (rmd1 - x1 + popc - 1) % (popc - 1);
      else
        res[i] = -1;
    }
    if (popc != 1)
      x1 = (x1 * 2) % (popc - 1);
    x2 = (x2 * 2) % (popc + 1);
  }
  int cc = *max_element(res, res + s.size());
  ff[0] = 0;
  for (int i = 1; i <= cc; i++)
    ff[i] = ff[i % __builtin_popcount(i)] + 1;
  for (int i = 0; i < s.size(); i++)
    if (res[i] != -1)
      cout << ff[res[i]] + 1 << endl;
    else
      cout << 0 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, popc, cpop, rmd1, rmd2, res[200100], ff[200100];
string s;
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> s;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1')
      popc++;
  if (popc != 1) {
    rmd1 = 0;
    for (int i = 0; i < s.size(); i++)
      rmd1 = (rmd1 * 2 + s[i] - '0') % (popc - 1);
  }
  rmd2 = 0;
  for (int i = 0; i < s.size(); i++)
    rmd2 = (rmd2 * 2 + s[i] - '0') % (popc + 1);
  int x1 = 1, x2 = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == '0') {
      res[i] = (rmd2 + x2 + popc + 1) % (popc + 1);
    }
    if (s[i] == '1') {
      res[i] = (rmd1 - x1 + popc - 1) % (popc - 1);
    }
    if (popc != 1)
      x1 = (x1 * 2) % (popc - 1);
    x2 = (x2 * 2) % (popc + 1);
  }
  ff[0] = 0;
  for (int i = 1; i <= popc; i++)
    ff[i] = ff[i % __builtin_popcount(i)] + 1;
  for (int i = 0; i < s.size(); i++)
    cout << ff[res[i]] + 1 << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);

  vector<int> ans(n);
  for (int i = 1; i < n; i++) {
    int b = 0, t = i;
    while (t) {
      b += (t & 1);
      t >>= 1;
    }

    if (i % b == 0)
      ans[i] = 1;
    else
      ans[i] = ans[i % b] + 1;
  }

  vector<int> a(n);
  int s = 0;
  for (int i = 0; i < n; i++) {
    char c;
    scanf(" %c", &c);
    a[i] = c - '0';
    s += a[i];
  }

  vector<vector<int>> two(2, vector<int>(n));
  vector<int> x(2);
  for (int i = 0; i < 2; i++) {
    int m = i * 2 - 1;
    if (s + m > 0) {
      two[i][0] = 1 % (s + m);
      x[i] = a[0] % (s + m);
      for (int j = 1; j < n; j++) {
        two[i][j] = two[i][j - 1] * 2 % (s + m);
        x[i] = (x[i] * 2 + a[j]) % (s + m);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    int u = s - 2 * a[i] + 1;
    if (u == 0)
      printf("0\\n");
    else {
      int v =
          (x[1 - a[i]] + (-2 * a[i] + 1) * two[1 - a[i]][n - i - 1] + u) % u;
      printf("%d\\n", ans[v] + 1);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);

  vector<int> ans(n);
  for (int i = 1; i < n; i++) {
    int b = 0, t = i;
    while (t) {
      b += (t & 1);
      t >>= 1;
    }

    if (i % b == 0)
      ans[i] = 1;
    else
      ans[i] = ans[i % b] + 1;
  }

  vector<int> a(n);
  int s = 0;
  for (int i = 0; i < n; i++) {
    char c;
    scanf(" %c", &c);
    a[i] = c - '0';
    s += a[i];
  }

  vector<vector<int>> two(2, vector<int>(n));
  vector<int> x(2);
  for (int i = 0; i < 2; i++) {
    int m = i * 2 - 1;

    two[i][0] = 1 % (s + m);
    x[i] = a[0] % (s + m);
    for (int j = 1; j < n; j++) {
      two[i][j] = two[i][j - 1] * 2 % (s + m);
      x[i] = (x[i] * 2 + a[j]) % (s + m);
    }
  }

  for (int i = 0; i < n; i++) {
    int u = s - 2 * a[i] + 1;
    if (u == 0)
      printf("0\\n");
    else {
      int v =
          (x[1 - a[i]] + (-2 * a[i] + 1) * two[1 - a[i]][n - i - 1] + u) % u;
      printf("%d\\n", ans[v] + 1);
    }
  }

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

#include <bits/stdc++.h>
#define inf 1000000007
#define pb push_back
#define sz(v) (v).size()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int n, mod, p1, p2, num1, num2, a[200005];
int qpow(const int &x, int y) {
  if (y == 0)
    return 1;
  ll res = qpow(x, y / 2);
  ll ret = (res * res) % mod;
  if (y % 2)
    ret += ret, ret %= mod;
  return ret;
}

int solve(int number) {
  if (number == 0)
    return 0;
  int tot = __builtin_popcount(number);
  return solve(number % tot) + 1;
}

int calc(int ind) {
  if (a[ind] == 1) {
    mod = p1;
    if (mod == 0)
      return -1;
    return solve((num1 + mod - qpow(2, n - ind - 1)) % mod);
  } else {
    mod = p2;
    return solve((num2 + qpow(2, n - ind - 1)) % mod);
  }
}

string s;
int main() {
  cin >> n >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      cnt++;
  p1 = cnt - 1, p2 = cnt + 1;
  for (int i = 0; i < n; i++) {
    num1 *= 2;
    num1 += s[i] - '0';
    if (p1 > 0)
      num1 %= p1;
    num2 *= 2;
    num2 += s[i] - '0';
    num2 %= p2;
    a[i] = s[i] - '0';
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", calc(i) + 1);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define inf 1000000007
#define pb push_back
#define sz(v) (v).size()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int n, mod, p, num, a[200005];
int qpow(const int &x, int y) {
  if (y == 0)
    return 1;
  ll res = qpow(x, y / 2);
  ll ret = (res * res) % mod;
  if (y % 2)
    ret += ret, ret %= mod;
  return ret;
}

int solve(int number) {
  if (number == 0)
    return 0;
  int tot = __builtin_popcount(number);
  return solve(number % tot) + 1;
}

int calc(int ind) {
  if (a[ind] == 1) {
    mod = p - 1;
    return solve((num + mod - qpow(2, n - ind - 1)) % mod);
  } else {
    mod = p + 1;
    return solve((num + qpow(2, n - ind - 1)) % mod);
  }
}

string s;
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    num *= 2;
    num += s[i] - '0';
    a[i] = s[i] - '0';
    if (a[i])
      p++;
  }
  for (int i = 0; i < n; i++) {
    printf("%d\\n", calc(i) + 1);
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  bitset<200000> X;
  cin >> N >> X;
  int M = X.count(), now1 = 0, now2 = 0;
  if (M >= 2) {
    for (int i = N - 1; i >= 0; i--)
      now1 = (now1 * 2 + X[i]) % (M + 1), now2 = (now2 * 2 + X[i]) % (M - 1);
    vector<int> v1(N), v2(N);
    v1[0] = 1, v2[0] = 1;
    for (int i = 1; i < N; i++) {
      v1[i] = v1[i - 1] * 2 % (M + 1);
      v2[i] = v2[i - 1] * 2 % (M - 1);
    }
    for (int i = 0; i < N; i++) {
      int n;
      if (X[N - 1 - i] == 1)
        n = (now2 - v2[N - 1 - i] + (M - 1)) % (M - 1);
      else
        n = (now1 + v1[N - 1 - i] + (M + 1)) % (M + 1);
      int ans = 1;
      for (int j = n; j > 0; j %= __builtin_popcount(j))
        ans++;
      cout << ans << endl;
    }
  } else if (M == 1) {
    for (int i = N - 1; i >= 0; i--)
      now1 = (now1 * 2 + X[i]) % (M + 1);
    vector<int> v1(N);
    v1[0] = 1;
    for (int i = 1; i < N; i++)
      v1[i] = v1[i - 1] * 2 % (M + 1);
    for (int i = 0; i < N; i++) {
      if (X[N - 1 - i] == 0) {
        int n = (now1 + v1[N - 1 - i]) % (M + 1);
        bitset<200000> A(n);
        int ans = 1;
        for (int j = n; j > 0; j %= __builtin_popcount(j))
          ans++;
        cout << ans << endl;
      } else
        cout << 0 << endl;
    }
  } else
    for (int i = 0; i < N; i++)
      cout << 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  bitset<200000> X;
  cin >> N >> X;
  int M = X.count(), now1 = 0, now2 = 0;
  for (int i = N - 1; i >= 0; i--)
    now1 = (now1 * 2 + X[i]) % (M + 1), now2 = (now2 * 2 + X[i]) % (M - 1);
  vector<int> v1(N), v2(N);
  v1[0] = 1, v2[0] = 1;
  for (int i = 1; i < N; i++) {
    v1[i] = v1[i - 1] * 2 % (M + 1);
    v2[i] = v2[i - 1] * 2 % (M - 1);
  }
  for (int i = 0; i < N; i++) {
    int n;
    if (X[N - 1 - i] == 1) {
      n = (now2 - v2[N - 1 - i] + (M - 1)) % (M - 1);
    } else {
      n = (now1 + v1[N - 1 - i] + (M + 1)) % (M + 1);
    }
    bitset<200000> A(n);
    int ans = 2;
    if (A.count() == 0) {
      cout << 1 << endl;
      continue;
    }
    for (int i = (A.to_ullong()) % (A.count()); i > 0;
         i %= __builtin_popcount(i)) {
      ans++;
      A = i;
    }
    cout << ans << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long N;
  cin >> N;
  long a[200001];
  a[0] = 0;
  for (long i = 1; i <= 200000; i++) {
    a[i] = 1 + a[i % bitset<32>(i).count()];
  }
  string X;
  cin >> X;
  long minus_one_sum = 0;
  long plus_one_sum = 0;
  long base_ones = 0;
  for (long i = 0; i < N; i++) {
    if (X[i] == '1') {
      base_ones += 1;
    }
  }
  long m_prev[N];
  long p_prev[N];
  m_prev[0] = 1;
  p_prev[0] = 1;
  for (long i = 1; i < N; i++) {
    if (base_ones == 1) {
      m_prev[i] = 0;
    } else {
      m_prev[i] = (m_prev[i - 1] * 2) % (base_ones - 1);
    }
    p_prev[i] = (p_prev[i - 1] * 2) % (base_ones + 1);
  }
  for (long i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      if (base_ones != 1)
        minus_one_sum += m_prev[N - 1 - i] % (base_ones - 1);
      plus_one_sum += p_prev[N - 1 - i] % (base_ones + 1);
    }
  }
  // cout << base_ones << endl;
  // cout << base_sum << endl;

  for (long i = 0; i < X.length(); i++) {
    if (X[i] == '1') {
      if (base_ones == 1) {
        cout << 0 << endl;
        continue;
      }
      long ones = base_ones - 1;
      long sum = minus_one_sum - m_prev[N - 1 - i];
      if (sum < 0) {
        sum += ones;
      }
      cout << (a[sum % ones] + 1) << endl;
    } else {
      long ones = base_ones + 1;
      long sum = plus_one_sum + p_prev[N - 1 - i];
      cout << (a[sum % ones] + 1) << endl;
    }
    // cout << sum << " " << ones << endl;
  }
}

Error: 0

Fixed submission:

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

int numofbits5(long bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int main() {
  int N;
  cin >> N;
  uint a[200000];
  a[0] = 0;
  for (uint i = 1; i < 200000; i++) {
    a[i] = 1 + a[i % numofbits5(i)];
  }
  string X;
  cin >> X;
  int minus_one_sum = 0;
  int plus_one_sum = 0;
  int base_ones = 0;
  for (int i = X.length() - 1; i >= 0; i--) {
    if (X[i] == '1') {
      base_ones += 1;
    }
  }
  int m_prev[N];
  int p_prev[N];
  m_prev[0] = 1;
  p_prev[0] = 1;
  for (int i = 1; i < N; i++) {
    m_prev[i] = (m_prev[i - 1] * 2) % (base_ones - 1);
    p_prev[i] = (p_prev[i - 1] * 2) % (base_ones + 1);
  }
  for (int i = X.length() - 1; i >= 0; i--) {
    if (X[i] == '1') {
      minus_one_sum += m_prev[X.length() - 1 - i];
      plus_one_sum += p_prev[X.length() - 1 - i];
    }
  }
  // cout << base_ones << endl;
  // cout << base_sum << endl;

  for (int i = 0; i < X.length(); i++) {
    if (X[i] == '1') {
      int ones = base_ones - 1;
      int sum = minus_one_sum - m_prev[i];
      sum = sum % ones;
      cout << (a[sum % ones] + 1) << endl;
    } else {
      int ones = base_ones + 1;
      int sum = plus_one_sum + p_prev[i];
      sum = sum % ones;
      cout << (a[sum % ones] + 1) << endl;
    }
    // cout << sum << " " << ones << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
#define MAXN 200005
char s[MAXN];
int cnt = 0;
int mo1 = 0, mo2 = 0;
inline int qpow(int a, int b, int p) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      ans = (long long)ans * a % p;
    a = (long long)a * a % p;
  }
  return ans;
}

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  for (int i = 0; i < n; i++) {
    mo1 = (mo1 * 2 + (s[i] - '0')) % (cnt + 1);
  }
  if (cnt > 1) {
    for (int i = 0; i < n; i++)
      mo2 = (mo2 * 2 + (s[i] - '0')) % (cnt - 1);
  }
  // cout << mo1 << "," << mo2 << endl;

  for (int i = 0; i < n; i++) {
    int now;
    if (s[i] == '0') {
      now = (mo1 + qpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
    } else {
      if (cnt == 1) {
        puts("0");
        continue;
      }
      now = (mo2 - qpow(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
    }
    int f = 1;
    while (now > 0) {
      now = now % __builtin_popcount(now);
      f++;
    }
    printf("%d\\n", f);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  std::string str;
  cin >> str;
  int i = std::stoi(str, 0, 2);
  // cout << i << endl << endl;

  /*
  int dp[18+1];
  for(int xx=1; xx<=18; xx++){
      int num = xx;

  }
  */
  for (int xx = n - 1; xx >= 0; xx--) {
    // cout << n-xx-1 << endl;
    std::bitset<23> bb(str);
    if (bb[xx] == 1)
      bb[xx] = 0;
    else
      bb[xx] = 1;
    unsigned long long int x = 0;
    cout << bb << ":" << bb.count() << endl;
    bb = bb >> bb.count();
    cout << bb << ":" << bb.count() << endl;

    cout << "---" << endl;
    int cnt = 0;
    for (int i = 0; i < 3; i++) {
      // cout << bb << ":" << bb.count() << endl;
      bb = bb >> bb.count();
      cnt++;
      if (bb == 0)
        break;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

  int N;
  string X;
  cin >> N >> X;

  reverse(X.begin(), X.end());
  vector<int> cnt(N + 5);
  cnt[0] = 0;
  for (int i = 1; i < N + 5; ++i) {
    int nxt = i % __builtin_popcount(i);
    cnt[i] = cnt[nxt] + 1;
  }

  int pcnt = 0;
  for (int i = 0; i < N; ++i)
    pcnt += (X[i] == '1');

  static ll modx[2];
  vector<vector<ll>> a(N + 5, vector<ll>(2));
  {
    a[0][0] = a[0][1] = 1;
    for (int i = 0; i < N; ++i) {
      if (X[i] == '1') {
        if (pcnt - 1)
          (modx[0] += a[i][0]) %= (pcnt - 1);
        (modx[1] += a[i][1]) %= (pcnt + 1);
      }
      if (pcnt - 1)
        a[i + 1][0] = (a[i][0] * 2) % (pcnt - 1);
      a[i + 1][1] = (a[i][1] * 2) % (pcnt + 1);
    }
  }

  for (int i = N - 1; i >= 0; --i) {
    if (pcnt == 1 && X[i] == '1') {
      cout << 0 << '\\n';
      continue;
    }

    int tmp = 0;
    if (X[i] == '1') {
      tmp = (modx[0] - a[i][0]) % (pcnt - 1);
      if (tmp < 0)
        tmp += (pcnt - 1);
    } else
      tmp = (modx[1] + a[i][1]) % (pcnt + 1);

    if (X[i] == '1')
      cout << cnt[tmp] + 1 << '\\n';
    else
      cout << cnt[tmp] + 1 << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

  int N;
  string X;
  cin >> N >> X;
  reverse(X.begin(), X.end());
  vector<int> cnt(N + 5);
  cnt[0] = 0;
  for (int i = 1; i < N + 5; ++i) {
    int nxt = i % __builtin_popcount(i);
    cnt[i] = cnt[nxt] + 1;
  }

  int pcnt = 0;
  for (int i = 0; i < N; ++i)
    pcnt += (X[i] == '1');

  static ll modx[2];
  vector<vector<ll>> a(N + 5, vector<ll>(2));
  {
    a[0][0] = a[0][1] = 1;
    for (int i = 0; i < N; ++i) {
      if (X[i] == '1') {
        (modx[0] += a[i][0]) %= (pcnt - 1);
        (modx[1] += a[i][1]) %= (pcnt + 1);
      }
      a[i + 1][0] = (a[i][0] * 2) % (pcnt - 1);
      a[i + 1][1] = (a[i][1] * 2) % (pcnt + 1);
    }
  }

  for (int i = N - 1; i >= 0; --i) {
    int tmp = 0;
    if (X[i] == '1') {
      tmp = (modx[0] - a[i][0]) % (pcnt - 1);
      if (tmp < 0)
        tmp += (pcnt - 1);
    } else
      tmp = (modx[1] + a[i][1]) % (pcnt + 1);

    if (X[i] == '1')
      cout << cnt[tmp] + 1 << '\\n';
    else
      cout << cnt[tmp] + 1 << '\\n';
  }

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

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N;
  cin >> N;
  vector<unsigned long> memo(N);
  memo[0] = 1;
  for (unsigned long i{1}; i < N; ++i)
    memo[i] = 1 + memo[i % __builtin_popcountl(i)];
  string S;
  cin >> S;
  unsigned long popcnt{
      static_cast<unsigned long>(count(begin(S), end(S), '1'))};
  if (popcnt == 0) {
    for (unsigned long i{0}; i < N; ++i)
      puts("1");
    return 0;
  }
  if (popcnt == 1) {
    if (S.back() == '1') {
      for (unsigned long i{1}; i < N; ++i)
        puts("2");
      puts("0");
    } else {
      for (unsigned long i{0}; i + 1 < N; ++i)
        puts(S[i] == '1' ? "0" : "1");
      puts("2");
    }
    return 0;
  }
  unsigned long ps{popcnt - 1}, pl{popcnt + 1};
  reverse(begin(S), end(S));
  vector<unsigned long> diff;
  unsigned long xs{0}, xl{0};
  for (unsigned long i{0}, j{1}, k{1}; i < N; ++i) {
    (xs += j * (S[i] & 1)) %= ps;
    (xl += k * (S[i] & 1)) %= pl;
    diff.push_back(S[i] == '1' ? ps - j : k);
    (j <<= 1) %= ps;
    (k <<= 1) %= pl;
  }
  reverse(begin(S), end(S));
  reverse(begin(diff), end(diff));
  for (unsigned long i{0}; i < N; ++i)
    cout
        << (S[i] == '1' && ps == 0
                ? 0
                : memo[S[i] == '1' ? (xs + diff[i]) % ps : (xl + diff[i]) % pl])
        << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N;
  cin >> N;
  vector<unsigned long> memo(N);
  memo[0] = 1;
  for (unsigned long i{1}; i < N; ++i)
    memo[i] = 1 + memo[i % __builtin_popcountl(i)];
  string S;
  cin >> S;
  unsigned long popcnt{
      static_cast<unsigned long>(count(begin(S), end(S), '1'))};
  unsigned long ps{popcnt - 1}, pl{popcnt + 1};
  reverse(begin(S), end(S));
  vector<unsigned long> diff;
  unsigned long xs{0}, xl{0};
  for (unsigned long i{0}, j{1}, k{1}; i < N; ++i) {
    (xs += j * (S[i] & 1)) %= ps;
    (xl += k * (S[i] & 1)) %= pl;
    diff.push_back(S[i] == '1' ? ps - j : k);
    (j <<= 1) %= ps;
    (k <<= 1) %= pl;
  }
  reverse(begin(S), end(S));
  reverse(begin(diff), end(diff));
  for (unsigned long i{0}; i < N; ++i)
    cout << memo[S[i] == '1' ? (xs + diff[i]) % ps : (xl + diff[i]) % pl]
         << endl;
  return 0;
}