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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

vecl a(200010, 1), b(200010, 1);
vecl popcount(200010, 0);

ll f(ll N) {
  ll ans = 0;
  while (N > 0) {
    ans++;
    N %= popcount[N];
  }
  return ans;
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  REP(i, N) cnt += X[i] == '1';
  REP(i, 200010) {
    ll n = i;
    while (n > 0) {
      popcount[i] += (n % 2 == 1);
      n /= 2;
    }
    a[i + 1] = a[i] * 2;
    b[i + 1] = b[i] * 2;
    a[i + 1] %= cnt + 1;
    if (cnt - 1 != 0)
      b[i + 1] %= cnt - 1;
  }

  ll s = 0, v = 0;

  REP(i, N) {
    s += a[N - i - 1] * (X[i] - '0');
    v += b[N - i - 1] * (X[i] - '0');
  }

  REP(i, N) {
    if (X[i] == '0') {
      cout << f((s + a[N - i - 1]) % (cnt + 1)) + 1 << endl;
    } else {
      if (cnt - 1 != 0)
        cout << f((v - b[N - i - 1]) % (cnt - 1)) + 1 << endl;
      else
        cout << 0 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

vecl a(100001, 1), b(100001, 1);
vecl popcount(100001, 0);

int f(int N) {
  int ans = 0;
  while (N > 0) {
    ans++;
    N %= popcount[N];
  }
  return ans;
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  REP(i, N) cnt += X[i] == '1';
  REP(i, 100001) {
    int n = i;
    while (n > 0) {
      popcount[i] += (n % 2 == 1);
      n /= 2;
    }
    // if(i <= 10) cout << popcount[i] << endl;
    a[i + 1] = a[i] * 2;
    b[i + 1] = b[i] * 2;
    a[i + 1] %= cnt + 1;
    if (cnt - 1 != 0)
      b[i + 1] %= cnt - 1;
  }
  int s = 0, v = 0;
  /*
  REP(i,N) {
      s += a[N-i-1]*(X[i]-'0');
      v += b[N-i-1]*(X[i]-'0');
  }
  */
  // cout << s << " " << v << endl;
  REP(i, N) {
    if (X[i] == '0') {
      cout << f((s + a[N - i - 1]) % (cnt + 1)) + 1 << endl;
    } else {
      if (cnt - 1 != 0)
        cout << f((v - b[N - i - 1]) % (cnt - 1)) + 1 << endl;
      else
        cout << 0 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;

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

int main(void) {
  int n;
  string s;
  cin >> n >> s;
  vector<int> x(n);
  rep(i, n) x[i] = s[i] - '0';
  int pc = 0;
  rep(i, n) pc += x[i];
  vector<int> ans(n);
  rep(b, 2) {
    int npc = pc;
    if (b == 0) {
      npc++;
    } else {
      npc--;
    }
    if (npc <= 0)
      continue;
    int r = 0;
    rep(i, n) {
      r = (r * 2) % npc;
      r += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == b) {
        int nr;
        if (b == 0) {
          nr = (r + k) % npc;
        } else {
          nr = (r - k + npc) % npc;
        }
        ans[i] = f(nr) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;

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

int main(void) {
  int n;
  string s;
  cin >> n >> s;
  vector<int> x(n);
  rep(i, n) x[i] = s[i] - '0';
  int pc = 0;
  rep(i, n) pc += x[i];
  vector<int> ans(n);
  rep(b, 2) {
    int npc = pc;
    if (b == 0) {
      npc++;
    } else {
      npc--;
    }
    int r;
    rep(i, n) {
      r = (r * 2) % npc;
      r += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == b) {
        int nr;
        if (b == 0) {
          nr = (r + k) % npc;
        } else {
          nr = (r - k + npc) % npc;
        }
        ans[i] = f(nr) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
long long n;
int memo[200001];
string s;
long long f(long long x) {
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];
  long long num = 0;
  for (int i = 0; i < 19; i++) {
    if (x & (1 << i))
      num++;
  }
  return memo[x] = f(x % num) + 1;
}
long long power(long long a, long long b, long long p) {
  a %= p;
  long long res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % p;
    b /= 2;
    a = (a * a) % p;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(memo, -1, sizeof(memo));
  cin >> n >> s;
  long long num = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num++;
  }
  long long up = 0, down = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      up += power(2, n - i - 1, num + 1);
      if (num > 1)
        down += power(2, n - i - 1, num - 1);
      up %= (num + 1);
      if (num > 1)
        down %= (num - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (num == 1 && s[i] == '1') {
      cout << 0 << '\\n';
      continue;
    }
    long long curnum;
    if (s[i] == '1') {
      curnum = down;
      curnum -= power(2, n - i - 1, num - 1);
      curnum %= (num - 1);
      if (curnum < 0)
        curnum += num - 1;
    } else {
      curnum = up;
      curnum += power(2, n - i - 1, num + 1);
      curnum %= (num + 1);
      if (curnum < 0)
        curnum += num + 1;
    }
    cout << f(curnum) + 1 << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
long long n;
int memo[200001];
string s;
long long f(long long x) {
  if (x == 0)
    return 0;
  // if (memo[x]!=-1) return memo[x];
  long long num = 0;
  for (int i = 0; i < 19; i++) {
    if (x & (1 << i))
      num++;
  }
  return memo[x] = f(x % num) + 1;
}
long long power(long long a, long long b, long long p) {
  a %= p;
  long long res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res *= a;
    b /= 2;
    a = (a * a) % p;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(memo, -1, sizeof(memo));
  cin >> n >> s;
  long long num = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num++;
  }
  long long up = 0, down = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      up += power(2, n - i - 1, num + 1);
      if (num > 1)
        down += power(2, n - i - 1, num - 1);
      up %= (num + 1);
      if (num > 1)
        down %= (num - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (num == 1 && s[i] == '1') {
      cout << 0 << '\\n';
      continue;
    }
    long long curnum;
    if (s[i] == '1') {
      curnum = down;
      curnum -= power(2, n - i - 1, num - 1);
      curnum %= (num - 1);
      if (curnum < 0)
        curnum += num - 1;
    } else {
      curnum = up;
      curnum += power(2, n - i - 1, num + 1);
      curnum %= (num + 1);
      if (curnum < 0)
        curnum += num + 1;
    }
    assert(curnum <= n);
    cout << f(curnum) + 1 << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;

int popcount(int x) { return __builtin_popcount(x); }

int f(int x) {
  int nop = 0;
  while (x > 0) {
    x %= popcount(x);
    nop++;
  }
  return nop;
}

int main(void) {
  int N;
  string X;
  cin >> N >> X;
  int a[N];
  rep(i, N) a[i] = X[i] - '0';

  int pop0 = 0;
  rep(i, N) pop0 += a[i];

  int ans[N];
  rep(i, N) ans[i] = 0;

  rep(i, 2) {
    int pop;
    if (i == 1)
      pop = pop0 - 1;
    else
      pop = pop0 + 1;

    if (pop <= 0)
      continue;

    int mod0 = 0;

    rep(j, N) {
      mod0 = (mod0 * 2) % pop;
      mod0 += a[j];
    }

    int k = 1;
    for (int j = N - 1; j >= 0; j--) {
      if (a[j] == i) {
        int x = mod0;
        if (a[j] == 1)
          x = (x - k + pop) % pop;
        else
          x = (x + k) % pop;
        ans[j] = f(x) + 1;
      }
      k = (2 * k) % pop;
    }
  }

  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 ll = long long;
using namespace std;

int popcount(int x) { return __builtin_popcount(x); }

int f(int x) {
  int nop = 0;
  while (x > 0) {
    x %= popcount(x);
    nop++;
  }
  return nop;
}

int main(void) {
  /*
  int N;
  ll X;
  cin >> N >> X;
  ll binary = X;
int decimal = 0;
int base = 1;
  while(binary>0){
decimal = decimal + ( binary % 10 ) * base;
binary = binary / 10;
base = base * 2;
}

  for (int i = N-1; i >= 0; i--) {
          int hanten = decimal ^ int(pow(2,i));
          int ans = 0;
          while (hanten > 0){
                  int d = popcount(hanten);
                  hanten %= d;
                  ans++;
          }
          cout << ans << endl;
  }

  */

  int N;
  string X;
  cin >> N >> X;
  int a[N];
  rep(i, N) a[i] = X[i] - '0';

  int pop0 = 0;
  rep(i, N) pop0 += a[i];
  int mod_n = 0, mod_p = 0;
  for (int i = -1; i <= 1; i += 2) {
    int pop = pop0 + i;
    int tmp_m = 0;
    int tmp_d = 1;

    if (pop <= 0)
      continue;

    rep(j, N) {
      tmp_m += a[N - 1 - j] * tmp_d;
      tmp_m %= pop;
      tmp_d *= 2;
    }
    if (i == -1)
      mod_n = tmp_m;
    else
      mod_p = tmp_m;
  }

  int kn = 1, kp = 1;
  int ans[N];
  for (int i = N - 1; i >= 0; i--) {
    int x;
    int pop;
    if (a[i] == 1) {
      pop = pop0 - 1;
      if (pop == 0) {
        ans[i] = 0;
        continue;
      }
      x = mod_n - kn;
    } else {
      pop = pop0 + 1;
      x = mod_p + kp;
    }
    x = (x + pop) % pop;
    ans[i] = f(x) + 1;
    kn = (2 * kn) % (pop0 - 1);
    kp = (2 * kp) % (pop0 + 1);
  }

  rep(i, N) cout << ans[i] << endl;

  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

long long int mypow(long long int a, long long int b, long long int mod) {
  if (b == 0)
    return 1 % mod;
  if (b == 1)
    return a % mod;
  long long int res = mypow(a, b / 2, mod);
  res = res * res % mod;
  if (b % 2 == 1)
    res = res * a % mod;
  return res;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int index = -1;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (index == -1) {
        index = i;
      } else {
        index = -1;
        break;
      }
    }
  }
  int one = 0;
  for (int i = 0; i < n; i++) {
    one += x[i] - '0';
  }

  int add_c = 0, minus_c = 0;
  for (int i = 0; i < n; i++) {
    add_c = add_c * 2 + x[i] - '0';
    add_c %= one + 1;
  }
  for (int i = 0; i < n && one - 1 > 0; i++) {
    minus_c = minus_c * 2 + x[i] - '0';
    minus_c %= one - 1;
  }
  for (int i = 0; i < n; i++) {
    x[i] = '1' - x[i] + '0';
    if (index == i) {
      cout << 0 << endl;
      x[i] = '1' - x[i] + '0';
      continue;
    }
    int res = 1;
    int c = 0;
    if (x[i] == '0')
      c = ((minus_c - mypow(2, n - i - 1, one - 1)) % (one - 1) + one - 1) %
          (one - 1);
    else
      c = ((add_c + mypow(2, n - i - 1, one + 1)) % (one + 1) + one + 1) %
          (one + 1);
    while (c > 0) {
      res++;
      int next = c;
      int bit = 0;
      while (next > 0) {
        bit += next % 2;
        next /= 2;
      }
      c %= bit;
    }
    cout << res << endl;
    x[i] = '1' - x[i] + '0';
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int index = -1;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (index == -1) {
        index = i;
      } else {
        index = -1;
        break;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    x[i] = '1' - x[i] + '0';
    if (index == i) {
      cout << 0 << endl;
      x[i] = '1' - x[i] + '0';

      continue;
    }
    int res = 1;
    int c = 0;
    int bit = 0;
    for (int i = 0; i < n; i++) {
      bit += x[i] - '0';
    }
    for (int i = 0; i < n; i++) {
      c = c * 2 + x[i] - '0';
      c %= bit;
    }
    while (c > 0) {
      res++;
      int next = c;
      bit = 0;
      while (next > 0) {
        bit += next % 2;
        next /= 2;
      }
      c %= bit;
    }
    cout << res << endl;
    x[i] = '1' - x[i] + '0';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int n;
string s;

int getx(int mod) {
  ll x = 0, two = 1;
  rep(i, n) {
    x = (x + two * (int)(s[n - 1 - i] - '0')) % mod;
    two = (two * 2) % mod;
  }

  return x;
}

int popcount(int x) {
  int ret = 0;
  while (x > 0) {
    ret += x & 1;
    x = x >> 1;
  }
  return ret;
}

int main() {

  cin >> n;
  cin >> s;
  int pc = 0;
  rep(i, n) {
    if (s[i] == '1')
      pc++;
  }

  int pc0 = pc + 1, pc1 = pc - 1, x0, x1, two0 = 1, two1 = 1;

  x0 = getx(pc0);
  if (pc1 == 0)
    x1 = 0;
  else
    x1 = getx(pc1);

  // cout << x0 << " " << x1 << endl;

  int x[n];
  vector<int> ans(n, 0);

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      // cout << x0 + two0 << endl;
      x[i] = (x0 + two0) % pc0;
      if (x[i] < 0)
        x[i] += pc0;
    } else {
      // cout << x1 - two1 << endl;
      if (pc1 == 0) {
        x[i] = 0;
        ans[i] = -1;
      } else
        x[i] = (x1 - two1) % pc1;
      if (x[i] < 0)
        x[i] += pc1;
    }
    two0 = (two0 * 2) % pc0;
    if (pc1 == 0)
      two1 = 0;
    else
      two1 = (two1 * 2) % pc1;
  }

  rep(i, n) {
    int t = x[i];
    while (t != 0) {
      t %= popcount(t);
      ans[i]++;
    }
    ans[i]++;
  }

  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;

int n;
string s;

int getx(int mod) {
  ll x = 0, two = 1;
  rep(i, n) {
    x = (x + two * (int)(s[n - 1 - i] - '0')) % mod;
    two = (two * 2) % mod;
  }

  return x;
}

int popcount(int x) {
  int ret = 0;
  while (x > 0) {
    ret += x & 1;
    x = x >> 1;
  }
  return ret;
}

int main() {

  cin >> n;
  cin >> s;
  int pc = 0;
  rep(i, n) {
    if (s[i] == '1')
      pc++;
  }

  int pc0 = pc + 1, pc1 = pc - 1, x0, x1, two0 = 1, two1 = 1;

  x0 = getx(pc0);
  x1 = getx(pc1);

  // cout << x0 << " " << x1 << endl;

  int x[n];

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      // cout << x0 + two0 << endl;
      x[i] = (x0 + two0) % pc0;
      if (x[i] < 0)
        x[i] += pc0;
    } else {
      // cout << x1 - two1 << endl;
      x[i] = (x1 - two1) % pc1;
      if (x[i] < 0)
        x[i] += pc1;
    }
    two0 = (two0 * 2) % pc0;
    two1 = (two1 * 2) % pc1;
  }

  int ans[n];
  rep(i, n) {
    int t = x[i], cnt = 0;
    while (t != 0) {
      // cout << t << endl;
      t %= popcount(t);
      cnt++;
    }
    ans[i] = cnt + 1;
  }

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

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

#include <bits/stdc++.h>
#include <random>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;
const ll Lmax = ll(1e18);
const ll INF = 1e9;
const int Bmax = 2e5;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), ap(n), am(n);
  bitset<Bmax> x;
  cin >> x;
  int p = x.count();

  int now = 1;
  int nowp = 1;
  int nowm = 1;
  int rem = 0;
  int remp = 0;
  int remm = 0;

  rep(i, n) {
    if (p > 0) {
      a[i] = now % p;
      if (x.test(i))
        rem = (rem + now) % p;
      now = (now * 2) % p;
    }

    ap[i] = nowp % (p + 1);
    if (x.test(i))
      remp = (remp + nowp) % (p + 1);
    nowp = (nowp * 2) % (p + 1);

    if (p > 1) {
      am[i] = nowm % (p - 1);
      if (x.test(i))
        remm = (remm + nowm) % (p - 1);
      nowm = (nowm * 2) % (p - 1);
    }
  }

  rep(i, n) {
    int remi;
    if (x.test(n - 1 - i)) {
      if (p <= 1) {
        cout << 0 << endl;
        continue;
      } else {
        remi = (remm + (p - 1) - am[n - 1 - i]) % (p - 1);
      }
    } else {
      remi = (remp + ap[n - 1 - i]) % (p + 1);
    }
    int ans = 1;

    while (remi != 0) {
      ans++;
      int xi = remi;
      bitset<32> t = bitset<32>(xi);
      int pi = t.count();
      remi = xi % pi;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <random>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;
const ll Lmax = ll(1e18);
const ll INF = 1e9;
const int Bmax = 2e5;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), ap(n), am(n);
  bitset<Bmax> x;
  cin >> x;
  int p = x.count();

  int now = 1 % p;
  int nowp = 1 % p;
  int nowm = 1 % p;
  int rem = 0;
  int remp = 0;
  int remm = 0;

  rep(i, n) {
    a[i] = now;
    if (x.test(i))
      rem = (rem + now) % p;
    now = (now * 2) % p;

    ap[i] = nowp;
    if (x.test(i))
      remp = (remp + nowp) % (p + 1);
    nowp = (nowp * 2) % (p + 1);

    if (p > 1) {
      am[i] = nowm;
      if (x.test(i))
        remm = (remm + nowm) % (p - 1);
      nowm = (nowm * 2) % (p - 1);
    }
  }

  rep(i, n) {
    int remi;
    if (x.test(n - 1 - i)) {
      if (p == 1) {
        cout << 0 << endl;
        continue;
      } else {
        remi = (remm + (p - 1) - am[n - 1 - i]) % (p - 1);
      }
    } else {
      remi = (remp + ap[n - 1 - i]) % (p + 1);
    }
    int ans = 1;

    while (remi != 0) {
      ans++;
      int xi = remi;
      bitset<32> t = bitset<32>(xi);
      int pi = t.count();
      remi = xi % pi;
    }
    cout << ans << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

int pop_count(long 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 calc1(int cnt, string x) {
  int res = 0;
  for (int i = 0; i < x.size(); i++) {
    res <<= 1;
    if (x[i] == '1') {
      res += 1;
    }
    res %= cnt;
  }
  return res;
}
int calc2(int a) {
  if (a == 0) {
    return 0;
  }
  int res = 0;
  res = calc2(a % pop_count(a)) + 1;
  return res;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cnt++;
    }
  }
  int r0 = cnt != 1 ? calc1(cnt - 1, x) : 0;
  int r1 = calc1(cnt + 1, x);
  vector<int> ans(n);
  int t0 = 1, t1 = 1;
  for (int i = n - 1; i >= 0; i--) {
    int a;
    if (x[i] == '0') {
      a = r1 + t1;
      a %= cnt + 1;
      ans[i] = 1 + calc2(a);
    } else if (cnt == 1) {
      ans[i] = 0;
    } else {
      a = r0 - t0 + cnt - 1;
      a %= cnt - 1;
      ans[i] = 1 + calc2(a);
    }
    if (cnt != 1) {
      t0 <<= 1;
      t0 %= cnt - 1;
    }
    t1 <<= 1;
    t1 %= cnt + 1;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

int pop_count(long 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 calc1(int cnt, string x) {
  int res = 0;
  for (int i = 0; i < x.size(); i++) {
    res <<= 1;
    if (x[i] == '1') {
      res += 1;
    }
    res %= cnt;
  }
  return res;
}
int calc2(int a) {
  if (a == 0) {
    return 0;
  }
  int res = 0;
  res = calc2(a % pop_count(a)) + 1;
  return res;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cnt++;
    }
  }
  int r0 = cnt != 0 ? calc1(cnt - 1, x) : 0;
  int r1 = calc1(cnt + 1, x);
  vector<int> ans(n);
  int t0 = 1, t1 = 1;
  for (int i = n - 1; i >= 0; i--) {
    int a;
    if (x[i] == '0') {
      a = r1 + t1;
      a %= cnt + 1;
    } else {
      a = r0 - t0 + cnt - 1;
      a %= cnt - 1;
    }
    ans[i] = 1 + calc2(a);
    t0 <<= 1;
    t1 <<= 1;
    t0 %= cnt - 1;
    t1 %= cnt + 1;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}