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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const ll MOD = 1e9 + 7;
const int INF = 2e9;
const double PI = acos(-1);

const int MAX = 200010;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  vector<ll> b1(n), b2(n);
  b1[0] = 1, b2[0] = 1;
  ll s1 = 0, s2 = 0;
  if (cnt - 1 > 0) {
    rep(i, n - 1) b1[i + 1] = (2 * b1[i]) % (cnt - 1);
    rep(i, n) {
      if (s[i] == '1')
        s1 = (s1 + b1[i]) % (cnt - 1);
    }
  }
  rep(i, n - 1) b2[i + 1] = (2 * b2[i]) % (cnt + 1);
  rep(i, n) {
    if (s[i] == '1')
      s2 = (s2 + b2[i]) % (cnt + 1);
  }
  vector<int> p(200010);
  repr(i, 1, 200010) {
    int j = i, m = 0;
    while (j > 0) {
      if (j % 2 == 1)
        ++m;
      j /= 2;
    }
    p[i] = p[i % m] + 1;
  }
  invrep(i, n) {
    int ans = 0;
    if (s[i] == '1') {
      if (cnt > 1) {
        ll tmp = (s1 + (cnt - 1) - b1[i]) % (cnt - 1);
        ans = p[tmp] + 1;
      }
    } else {
      ll tmp = (s2 + b2[i]) % (cnt + 1);
      ans = p[tmp] + 1;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const ll MOD = 1e9 + 7;
const int INF = 2e9;
const double PI = acos(-1);

const int MAX = 200010;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  vector<ll> b1(n), b2(n);
  b1[0] = 1, b2[0] = 1;
  ll s1 = 0, s2 = 0;
  if (cnt - 1 > 0) {
    rep(i, n - 1) b1[i + 1] = (2 * b1[i]) % (cnt - 1);
    rep(i, n) {
      if (s[i] == '1')
        s1 = (s1 + b1[i]) % (cnt - 1);
    }
  }
  rep(i, n - 1) b2[i + 1] = (2 * b2[i]) % (cnt + 1);
  rep(i, n) {
    if (s[i] == '1')
      s2 = (s2 + b2[i]) % (cnt + 1);
  }
  vector<int> p(60);
  repr(i, 1, 60) {
    int j = i, m = 0;
    while (j > 0) {
      if (j % 2 == 1)
        ++m;
      j /= 2;
    }
    p[i] = p[i % m] + 1;
  }
  invrep(i, n) {
    int ans = 0;
    if (s[i] == '1') {
      if (cnt > 1) {
        ll tmp = (s1 + (cnt - 1) - b1[i]) % (cnt - 1);
        ans = p[tmp] + 1;
      }
    } else {
      ll tmp = (s2 + b2[i]) % (cnt + 1);
      ans = p[tmp] + 1;
    }
    cout << ans << endl;
  }

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

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;

int main() {
  ll n;
  std::cin >> n;
  string s;
  std::cin >> s;
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      std::cout << 1 << std::endl;
    }
    return 0;
  }

  ll def0 = 0;
  ll def2 = 0;
  vector<ll> ima0(n, 1);
  vector<ll> ima2(n, 1);

  for (int i = 0; i < n; i++) {
    if (s[n - 1 - i] == '1') {
      def0 += ima0[i];
      if (cnt != 1)
        def0 %= cnt - 1;
      def2 += ima2[i];
      def2 %= cnt + 1;
    }
    if (cnt != 1)
      if (i < n - 1)
        ima0[i + 1] = ima0[i] * 2 % (cnt - 1);
    if (i < n - 1)
      ima2[i + 1] = ima2[i] * 2 % (cnt + 1);
  }

  for (int i = 0; i < n; i++) {
    ll osae = 0;
    if (s[i] == '1') {
      if (cnt != 1)
        osae = (def0 - ima0[n - 1 - i] + cnt - 1) % (cnt - 1);
      if (cnt == 1) {
        std::cout << 0 << std::endl;
        continue;
      }
    } else {
      osae = (def2 + ima2[n - 1 - i] + cnt + 1) % (cnt + 1);
    }

    ll ans = 1;
    while (osae != 0) {
      ans++;
      osae = osae % __builtin_popcount(osae);
    }
    std::cout << ans << std::endl;
  }
}

Error: 0

Fixed submission:

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;

int main() {
  ll n;
  std::cin >> n;
  string s;
  std::cin >> s;
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      std::cout << 1 << std::endl;
    }
    return 0;
  }

  ll def0 = 0;
  ll def2 = 0;
  vector<ll> ima0(n, 1);
  vector<ll> ima2(n, 1);
  for (int i = 0; i < n; i++) {
    if (s[n - 1 - i] == '1') {
      def0 += ima0[i];
      def0 %= cnt - 1;
      def2 += ima2[i];
      def2 %= cnt + 1;
    }
    if (i < n - 1)
      ima0[i + 1] = ima0[i] * 2 % (cnt - 1);
    if (i < n - 1)
      ima2[i + 1] = ima2[i] * 2 % (cnt + 1);
  }

  for (int i = 0; i < n; i++) {
    ll osae = 0;
    if (s[i] == '1') {
      osae = (def0 - ima0[n - 1 - i] + cnt - 1) % (cnt - 1);
    } else {
      osae = (def2 + ima2[n - 1 - i] + cnt + 1) % (cnt + 1);
    }

    ll ans = 1;
    while (osae != 0) {
      ans++;
      bitset<30> bs(osae);
      osae = osae % bs.count();
    }
    std::cout << ans << std::endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;

const int maxn = 2e5 + 100;
const int mo = 998244353;
int a[maxn], cnt[maxn], res1[maxn], res2[maxn], r[maxn], rr[maxn];

inline int work(int k) {
  int ans = 0;
  while (k) {
    int cnt = 0, kk = k;
    while (kk) {
      if (kk & 1)
        ++cnt;
      kk >>= 1;
    }
    k %= cnt;
    ++ans;
  }
  return ans;
}

int main() {
  //    freopen("in.txt","r",stdin);
  int n;
  scanf("%d", &n);
  int ct = 0;
  char c = getchar();
  rep(i, 1, n) {
    c = getchar();
    a[i] = c - '0';
    if (a[i])
      ++ct;
  }
  rep(i, 1, n) cnt[i] = (a[i] == 0) ? ct + 1 : ct - 1;

  int r1 = ct - 1, r2 = ct + 1;

  res1[n] = (r1 <= 1) ? 0 : 1;
  res2[n] = (r2 <= 1) ? 0 : 1;
  per(i, n - 1, 1) {
    res1[i] = (r1 > 0) ? (res1[i + 1] * 2) % r1 : 0;
    res2[i] = (res2[i + 1] * 2 > r2) ? res2[i + 1] * 2 - r2 : res2[i + 1] * 2;
  }

  ll ans1 = 0, ans2 = 0;
  rep(i, 1, n) {
    if (a[i])
      ans1 += res1[i], ans2 += res2[i];
  }
  rep(i, 1, n) {
    if (a[i] & 1) {
      if (ct == 1)
        r[i] = 0;
      else
        rr[i] = (cnt[i] <= 1) ? 0 : (ans1 - res1[i] + cnt[i]) % cnt[i],
        r[i] = 1;
    } else
      rr[i] = (ans2 + res2[i]) % cnt[i], r[i] = 1;
  }

  rep(i, 1, n) {
    if (rr[i]) {
      r[i] += work(rr[i]);
    }
    printf("%d\\n", r[i]);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;

const int maxn = 2e5 + 100;
const int mo = 998244353;
inline int work(int k) {
  int ans = 0;
  while (k) {
    int cnt = 0, kk = k;
    while (kk) {
      if (kk & 1)
        ++cnt;
      kk >>= 1;
    }
    k %= cnt;
    ++ans;
  }
  return ans;
}

int a[maxn], cnt[maxn], res1[maxn], res2[maxn], r[maxn], rr[maxn];
int main() {
  //    freopen("in.txt","r",stdin);
  int n;
  scanf("%d", &n);
  int ct = 0;
  char c = getchar();
  rep(i, 1, n) {
    c = getchar();
    a[i] = c - '0';
    if (a[i])
      ++ct;
  }
  rep(i, 1, n) cnt[i] = (a[i] == 0) ? ct + 1 : ct - 1;

  int r1 = ct - 1, r2 = ct + 1;

  res1[n] = (r1 <= 1) ? 0 : 1;
  res2[n] = (r2 <= 1) ? 0 : 1;
  if (r1 < 0)
    r1 = 0;
  per(i, n - 1, 1) {
    res1[i] = (res1[i + 1] * 2) % r1;
    if (r1 > 0)
      res2[i] = (res2[i + 1] * 2) % r2;
    else
      res2[i] = 0;
  }

  int ans1 = 0, ans2 = 0;
  rep(i, 1, n) {
    if (a[i])
      ans1 += res1[i], ans2 += res2[i];
  }
  rep(i, 1, n) {
    if (a[i] & 1) {
      if (ct == 1)
        r[i] = 0;
      else
        rr[i] = (cnt[i] <= 1) ? 0 : (ans1 - res1[i] + cnt[i]) % cnt[i],
        r[i] = 1;
    } else
      rr[i] = (ans2 + res2[i]) % cnt[i], r[i] = 1;
  }

  rep(i, 1, n) {
    if (rr[i]) {
      r[i] += work(rr[i]);
    }
    printf("%d\\n", r[i]);
  }

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

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

int g(vector<int> a, int b) {
  if (b == 0)
    return 0;
  long long ret = 0;
  long long pow = 1;
  for (int i = a.size() - 1; i >= 0; i--) {
    if (a[i]) {
      ret = (ret + pow) % b;
    }
    pow = pow * 2 % b;
  }
  return ret;
}
int f(int n) {
  int ret = 0;
  while (n) {
    int c = __builtin_popcount(n);
    n %= c;
    ret++;
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  string Xs;
  cin >> Xs;
  int pcnt = 0;
  vector<int> X(N);
  for (int i = 0; i < N; i++) {
    X[i] = Xs[i] - '0';
    pcnt += X[i];
  }
  int n1, n2;
  if (pcnt - 1 > 0)
    n1 = g(X, pcnt - 1);
  n2 = g(X, pcnt + 1);
  vector<int> pow1(N, 1), pow2(N, 1);
  for (int i = 1; i < N; i++) {
    if (pcnt - 1 > 0)
      pow1[i] = pow1[i - 1] * 2 % (pcnt - 1);
    pow2[i] = pow2[i - 1] * 2 % (pcnt + 1);
  }
  for (int i = 0; i < N; i++) {
    int p = X[i] ^ 1 ? pcnt + 1 : pcnt - 1;
    int n;
    if (X[i] ^ 1) {
      n = (n2 + pow2[N - 1 - i]) % p;
    } else {
      if (p == 0) {
        cout << 0 << endl;
        continue;
      }
      n = (n1 - pow1[N - 1 - i] + p) % p;
    }
    cout << f(n) + 1 << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

int g(vector<int> a, int b) {
  long long ret = 0;
  long long pow = 1;
  for (int i = a.size() - 1; i >= 0; i--) {
    if (a[i]) {
      ret = (ret + pow) % b;
    }
    pow = pow * 2 % b;
  }
  return ret;
}
long long f(int n) {
  int ret = 0;
  while (n) {
    int c = __builtin_popcount(n);
    n %= c;
    ret++;
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  string Xs;
  cin >> Xs;
  int pcnt = 0;
  vector<int> X(N);
  for (int i = 0; i < N; i++) {
    X[i] = Xs[i] - '0';
    pcnt += X[i];
  }
  int n1 = g(X, pcnt - 1);
  int n2 = g(X, pcnt + 1);
  vector<int> pow1(N, 1), pow2(N, 1);
  for (int i = 1; i < N; i++) {
    pow1[i] = pow1[i - 1] * 2 % (pcnt - 1);
    pow2[i] = pow2[i - 1] * 2 % (pcnt + 1);
  }
  for (int i = 0; i < N; i++) {
    int p = X[i] ^ 1 ? pcnt + 1 : pcnt - 1;
    int n;
    if (X[i] ^ 1) {
      n = (n2 + pow2[N - 1 - i]) % p;
    } else {
      n = (n1 - pow1[N - 1 - i] + p) % p;
    }
    cout << f(n) + 1 << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll dp[200005];
ll ksm(ll a, ll b, ll mod) {
  ll ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}
int main() {
  dp[1] = 1;
  dp[2] = 1;
  for (int i = 3; i < 200001; i++) {
    ll t = 0;
    ll n = i;
    for (; n; n >>= 1)
      if (n & 1)
        t++;
    dp[i] = dp[i % t] + 1;
  }
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll mod = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      mod++;
  }
  ll sum1 = 0;
  ll sum2 = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      sum1 = (sum1 + ksm(2, s.size() - i - 1, mod + 1)) % (mod + 1);
      if (mod > 1)
        sum2 = (sum2 + ksm(2, s.size() - i - 1, mod - 1)) % (mod - 1);
    }
  }
  for (int i = 0; i < s.size(); i++) {
    if (mod == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    } else if (s[i] == '1') {
      ll t =
          ((sum2 + mod - 1) - (ksm(2, s.size() - i - 1, mod - 1))) % (mod - 1);
      cout << dp[t] + 1 << endl;
    } else {
      ll t = (sum1 + ksm(2, s.size() - i - 1, mod + 1)) % (mod + 1);
      cout << dp[t] + 1 << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int dp[200005];
ll ksm(int a, int b, int mod) {
  ll ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}
int main() {
  dp[1] = 1;
  dp[2] = 1;
  for (int i = 3; i < 200001; i++) {
    int t = 0;
    int n = i;
    for (; n; n >>= 1)
      if (n & 1)
        t++;
    dp[i] = (i % t == 0 ? 1 : dp[i % t] + 1);
  }
  int n;
  cin >> n;
  string s;
  cin >> s;
  int mod = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      mod++;
  }
  ll sum1 = 0;
  ll sum2 = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      sum1 = (sum1 + ksm(2, s.size() - i - 1, mod + 1)) % (mod + 1);
      sum2 = (sum2 + ksm(2, s.size() - i - 1, mod - 1)) % (mod - 1);
    }
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      int t =
          ((sum2 + mod - 1) - (ksm(2, s.size() - i - 1, mod - 1))) % (mod - 1);
      cout << dp[t] + 1 << endl;
    } else {
      int t = (sum1 + ksm(2, s.size() - i - 1, mod + 1)) % (mod + 1);
      cout << dp[t] + 1 << 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 pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
using pis = pair<int, string>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

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

  int n;
  cin >> n;
  string x;
  cin >> x;
  int ones = 0;
  for (int i = 0; i < n; i++)
    if (x[i] == '1')
      ones++;

  // ones++
  int t = ones + 1;
  int dt = 0;
  for (int i = 0; i < n; i++) {
    dt = (2 * dt + (x[i] == '1')) % t;
  }

  // ones--
  int s = ones - 1;
  int ds;
  if (s == 0)
    ds = 0;
  else {
    ds = 0;
    for (int i = 0; i < n; i++) {
      ds = (2 * ds + (x[i] == '1')) % s;
    }
  }

  vector<int> twos(n, 1);
  if (s != 0)
    for (int i = 1; i < n; i++)
      twos[i] = (twos[i - 1] * 2) % s;
  vector<int> twot(n, 1);
  for (int i = 1; i < n; i++)
    twot[i] = (twot[i - 1] * 2) % t;

  // cout << dt << " " << ds << endl;

  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (s == 0) {
        cout << 0 << endl;
        continue;
      }
      int nx = (ds - twos[n - 1 - i] + s) % s, cnt = 1;
      while (nx != 0) {
        nx %= bitset<64>(nx).count();
        cnt++;
      }
      cout << cnt << endl;
    } else {
      int nx = (dt + twot[n - 1 - i]) % t, cnt = 1;
      while (nx != 0) {
        nx %= bitset<64>(nx).count();
        cnt++;
      }
      cout << cnt << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
using pis = pair<int, string>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

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

  int n;
  cin >> n;
  string x;
  cin >> x;
  int ones = 0;
  for (int i = 0; i < n; i++)
    if (x[i] == '1')
      ones++;

  // ones++
  int t = ones + 1;
  int dt = 0;
  for (int i = 0; i < n; i++) {
    dt = (2 * dt + (x[i] == '1')) % t;
  }

  // ones--
  int s = ones - 1;
  int ds = 0;
  for (int i = 0; i < n; i++) {
    ds = (2 * ds + (x[i] == '1')) % s;
  }

  vector<int> twos(n, 1);
  for (int i = 1; i < n; i++)
    twos[i] = (twos[i - 1] * 2) % s;
  vector<int> twot(n, 1);
  for (int i = 1; i < n; i++)
    twot[i] = (twot[i - 1] * 2) % t;

  // cout << dt << " " << ds << endl;

  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      int nx = (ds - twos[n - 1 - i] + s) % s, cnt = 1;
      while (nx != 0) {
        nx %= bitset<64>(nx).count();
        cnt++;
      }
      cout << cnt << endl;
    } else {
      int nx = (dt + twot[n - 1 - i]) % t, cnt = 1;
      while (nx != 0) {
        nx %= bitset<64>(nx).count();
        cnt++;
      }
      cout << cnt << endl;
    }
  }
}
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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;

  int p = 0;
  REP(i, N) {
    if (S[i] == '1')
      p++;
  }

  reverse(ALL(S));

  vector<int> amari0(N);
  vector<int> amari1(N);

  int num0 = 1;
  int num1 = 1;

  ll sum0 = 0, sum1 = 0;

  REP(i, N) {
    num0 %= p + 1;
    amari0[i] = num0;

    if (S[i] == '1') {
      sum0 += num0;
      sum0 %= p + 1;
    }
    num0 *= 2;
  }

  if (p != 1) {
    REP(i, N) {
      num1 %= p - 1;
      amari1[i] = num1;

      if (S[i] == '1') {
        sum1 += num1;
        sum1 %= p - 1;
      }
      num1 *= 2;
    }
  }

  // REPR(i,N-1){
  //     if(S[i] == '0'){
  //         cout << (sum0 + amari0[i])%(p+1) << endl;
  //     }else{
  //         cout << (sum1 - amari1[i] + p-1)%(p-1) << endl;
  //     }
  // }

  vector<int> table(200100, 0);
  FOR(i, 1, 200010) {
    int pc = __builtin_popcount(i);
    if (i % pc == 0)
      table[i] = 1;
    else
      table[i] = table[i % pc] + 1;
  }

  vector<int> ans(N, 0);
  REP(i, N) {
    if (S[i] == '0') {
      ans[i] = table[(sum0 + amari0[i]) % (p + 1)];
    } else if (S[i] == '1' && p == 1) {
      ans[i] = -1;
    } else {
      ans[i] = table[(sum1 - amari1[i] + p - 1) % (p - 1)];
    }
  }

  reverse(ALL(ans));
  REP(i, N) { cout << ans[i] + 1 << 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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;

  int p = 0;
  REP(i, N) {
    if (S[i] == '1')
      p++;
  }

  reverse(ALL(S));

  vector<int> amari0(N);
  vector<int> amari1(N);

  int num0 = 1;
  int num1 = 1;

  ll sum0 = 0, sum1 = 0;

  REP(i, N) {
    num0 %= p + 1;
    amari0[i] = num0;

    if (S[i] == '1') {
      sum0 += num0;
      sum0 %= p + 1;
    }
    num0 *= 2;
  }
  REP(i, N) {
    num1 %= p - 1;
    amari1[i] = num1;

    if (S[i] == '1') {
      sum1 += num1;
      sum1 %= p - 1;
    }
    num1 *= 2;
  }

  // REPR(i,N-1){
  //     if(S[i] == '0'){
  //         cout << (sum0 + amari0[i])%(p+1) << endl;
  //     }else{
  //         cout << (sum1 - amari1[i] + p-1)%(p-1) << endl;
  //     }
  // }

  vector<int> table(200100, 0);
  FOR(i, 1, 200010) {
    int pc = __builtin_popcount(i);
    if (i % pc == 0)
      table[i] = 1;
    else
      table[i] = table[i % pc] + 1;
  }

  vector<int> ans(N, 0);
  REP(i, N) {
    if (S[i] == '0') {
      ans[i] = table[(sum0 + amari0[i]) % (p + 1)];
    } else {
      ans[i] = table[(sum1 - amari1[i] + p - 1) % (p - 1)];
    }
  }

  reverse(ALL(ans));
  REP(i, N) { cout << ans[i] + 1 << endl; }

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
constexpr int kMax = 2e5 + 1;
typedef long long LL;

int dfs(int v) {
  int cnt = 0;
  while (v > 0) {
    int popcount = __builtin_popcount(v);
    v %= popcount;
    ++cnt;
  }
  return cnt;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int popcount = 0;
  for (int i = 0; i < N; ++i)
    if (S[i] == '1')
      ++popcount;
  int pp = popcount + 1, pn = popcount - 1;

  int Xp = 0, Xn = 0;
  for (int i = 0; i < N; ++i) {
    Xp = (Xp * 2) % pp;
    Xn = (pn <= 0) ? 0 : (Xn * 2) % pn;
    if (S[i] == '1') {
      ++Xp;
      ++Xn;
    }
  }

  reverse(S.begin(), S.end());
  vector<int> v(N);
  int kp = 1, kn = 1;
  for (int i = 0; i < N; ++i) {

    if (S[i] == '1') {
      if (pn <= 0)
        v[i] = 0;
      else
        v[i] = dfs((pn + Xn - kn) % pn) + 1;
    } else {
      v[i] = dfs((Xp + kp) % pp) + 1;
    }

    kp = (2 * kp) % pp;
    kn = (pn <= 0) ? 0 : (2 * kn) % pn;
  }

  for (auto it = v.rbegin(); it != v.rend(); ++it)
    cout << *it << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
constexpr int kMax = 2e5 + 1;
typedef long long LL;

int dfs(int v) {
  int cnt = 0;
  while (v > 0) {
    int popcount = __builtin_popcount(v);
    v %= popcount;
    ++cnt;
  }
  return cnt;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int popcount = 0;
  for (int i = 0; i < N; ++i)
    if (S[i] == '1')
      ++popcount;
  int pp = popcount + 1, pn = popcount - 1;

  int Xp = 0, Xn = 0;
  for (int i = 0; i < N; ++i) {
    Xp = (Xp * 2) % pp;
    Xn = (Xn * 2) % max(pn, 1);
    if (S[i] == '1') {
      ++Xp;
      ++Xn;
    }
  }

  reverse(S.begin(), S.end());
  vector<int> v(N);
  int kp = 1, kn = 1;
  for (int i = 0; i < N; ++i) {

    if (S[i] == '1') {
      if (pn <= 0)
        v[i] = 0;
      else
        v[i] = dfs((pn + Xn - kn) % pn) + 1;
    } else {
      v[i] = dfs((Xp + kp) % pp) + 1;
    }

    kp = (2 * kp) % (popcount + 1);
    kn = (2 * kn) % (popcount - 1);
  }

  for (auto it = v.rbegin(); it != v.rend(); ++it)
    cout << *it << endl;
}