alexjercan/bugnet huggingface dataset:
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int mod = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      mod++;
  }

  vector<int> tl(N);
  tl[N - 1] = 1;
  vector<int> tu(N);
  tu[N - 1] = 1;
  for (int i = N - 2; i >= 0; i--) {
    tu[i] = tu[i + 1] * 2 % (mod + 1);
    if (mod > 1) {
      tl[i] = tl[i + 1] * 2 % (mod - 1);
    }
  }

  int ku = 0;
  int kl = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      ku += tu[i];
      ku = ku % (mod + 1);
    }
  }
  if (mod > 1) {
    for (int i = N - 1; i >= 0; i--) {
      if (X[i] == '1') {
        kl += tl[i];
        kl = kl % (mod - 1);
      }
    }
  }

  int cnt;
  int tmp, ans;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (mod == 1) {
        printf("%d\\n", 0);
        continue;
      }
      tmp = ((mod - 1) + kl - tl[i]) % (mod - 1);
    } else {
      tmp = ((mod + 1) + ku + tu[i]) % (mod + 1);
    }
    if (tmp == 0) {
      printf("%d\\n", 1);
      continue;
    }
    cnt = 0;
    while (true) {
      ans = __builtin_popcount(tmp);
      cnt++;
      if (ans == 0) {
        printf("%d\\n", cnt);
        break;
      }
      tmp = tmp % ans;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int ans;
  int mod = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      mod++;
    }
  }
  int tmp, anss;
  int two;
  int kotae = 0;
  for (int i = 0; i < N; i++) {
    tmp = mod;
    if (X[i] == '0') {
      tmp++;
    } else {
      tmp--;
    }
    if (tmp == 0) {
      printf("%d\\n", 0);
      continue;
    }

    ans = 0;
    two = 1;
    for (int j = N - 1; j > i; j--) {
      if (X[j] == '1') {
        ans += two;
        ans = ans % tmp;
      }
      two = two * 2 % tmp;
    }
    if (X[i] == '0') {
      ans += two;
      ans = ans % tmp;
    }
    two = two * 2 % tmp;
    for (int j = i - 1; j >= 0; j--) {
      if (X[j] == '1') {
        ans += two;
        ans = ans % tmp;
      }
      two = two * 2 % tmp;
    }

    if (ans == 0) {
      printf("%d\\n", 1);
      continue;
    }

    kotae = 1;

    while (true) {
      tmp = __builtin_popcount(ans);
      ans = ans % tmp;
      kotae++;
      if (ans == 0) {
        break;
      }
    }
    printf("%d\\n", kotae);
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

long long bourrin(long long a) {
  if (a == 0)
    return 0;

  int p = __builtin_popcount(a);
  a %= p;
  return 1 + bourrin(a);
}

long long fastPow(long long N, long long mod) {
  if (N == 0)
    return 1;
  if (N == 1)
    return 2;
  long long t = fastPow(N / 2, mod);
  t *= t;
  if (N % 2 == 1)
    t *= 2;
  t %= mod;
  return t;
}

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

  long long pop = 0;

  for (int c = 0; c < s.size(); c++) {
    pop += s[c] == '1';
  }

  vector<long long> bonus(N, 0);
  vector<long long> malus(N, 0);

  vector<long long> v(3, 0);

  for (int c = 0; c < s.size(); c++) {
    for (int c2 = 0; c2 < 3; c2++) {
      v[c2] *= 2;
      v[c2] += (s[c] - '0');
    }
    if (pop < N) {
      bonus[c] = (fastPow(s.size() - c - 1, (pop + 1))) % (pop + 1);
    }
    if (pop > 1) {
      malus[c] =
          (-fastPow(s.size() - c - 1, pop - 1) + (10 * (pop - 1))) % (pop - 1);
    }
    if (pop > 1)
      v[0] %= (pop - 1);
    if (pop != N)
      v[2] %= (pop + 1);
    if (pop > 0)
      v[1] %= pop;
  }
  long long trueRes = 0;
  for (int c = 0; c < s.size(); c++) {

    long long res = 0;
    if (s[c] == '0') {
      res = (v[2] + bonus[c]) % (pop + 1);
    } else {
      if (pop <= 1) {
        cout << 0 << "\\n";
        continue;
      }
      res = (v[0] + malus[c]) % (pop - 1);
    }
    cout << 1 + bourrin(res) << "\\n";
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long bourrin(long long a) {
  if (a == 0)
    return 0;

  int p = __builtin_popcount(a);
  a %= p;
  return 1 + bourrin(a);
}

long long fastPow(long long N, long long mod) {
  if (N == 0)
    return 1;
  if (N == 1)
    return 2;
  long long t = fastPow(N / 2, mod);
  t *= t;
  if (N % 2 == 1)
    t *= 2;
  t %= mod;
  return t;
}

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

  long long pop = 0;

  for (int c = 0; c < s.size(); c++) {
    pop += s[c] == '1';
  }

  vector<long long> bonus(N, 0);
  vector<long long> malus(N, 0);

  vector<long long> v(3, 0);

  for (int c = 0; c < s.size(); c++) {
    for (int c2 = 0; c2 < 3; c2++) {
      v[c2] *= 2;
      v[c2] += (s[c] - '0');
    }
    if (pop < N) {
      bonus[c] = (fastPow(s.size() - c - 1, (pop + 1))) % (pop + 1);
    }
    if (pop > 1) {
      malus[c] =
          (-fastPow(s.size() - c - 1, pop - 1) + (10 * (pop - 1))) % (pop - 1);
    }
    if (pop > 1)
      v[0] %= (pop - 1);
    if (pop != N)
      v[2] %= (pop + 1);
    v[1] %= pop;
  }
  long long trueRes = 0;
  for (int c = 0; c < s.size(); c++) {

    long long res = 0;
    if (s[c] == '0') {
      res = (v[2] + bonus[c]) % (pop + 1);
    } else {
      if (pop == 1) {
        cout << 0 << "\\n";
        continue;
      }
      res = (v[0] + malus[c]) % (pop - 1);
    }
    cout << 1 + bourrin(res) << "\\n";
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

#define N 200000

int n;
string x;
int remainderDP[3][N], remainderX[3];

int calcRemainder(int k, int num1) {
  if (num1 <= 0) {
    return 1;
  }
  remainderDP[k][n - 1] = 1 % num1;
  if (x[n - 1] == '1') {
    remainderX[k] = remainderDP[k][n - 1];
  }
  for (int i = 1; i < n; i++) {
    remainderDP[k][n - 1 - i] = (remainderDP[k][n - i] * 2) % num1;
    if (x[n - 1 - i] == '1') {
      remainderX[k] = (remainderX[k] + remainderDP[k][n - 1 - i]) % num1;
    }
  }
  return 0;
}

int popcount() {
  int cnt = 0;
  for (int i = 0; i < x.size(); i++) {
    if (x[i] == '1') {
      cnt++;
    }
  }
  return cnt;
}

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

  int numOneX = popcount();
  for (int i = 0; i <= 2; i++) {
    calcRemainder(i, numOneX - 1 + i);
  }
  for (int i = 0; i < n; i++) {
    int cnt = 0, remainderXi;
    if (x[i] == '1') {
      if (numOneX - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      remainderXi =
          (remainderX[0] - remainderDP[0][i] + numOneX - 1) % (numOneX - 1);
    } else {
      remainderXi = (remainderX[2] + remainderDP[2][i]) % (numOneX + 1);
    }
    cnt++;

    while (remainderXi != 0) {
      int remainderTmp = remainderXi;
      int numOne2 = 0;
      while (remainderTmp != 0) {
        numOne2 += remainderTmp & 1;
        remainderTmp = remainderTmp >> 1;
      }
      remainderXi = remainderXi % numOne2;
      cnt++;
    }

    cout << cnt << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define N 200000

int n;
string x;
int remainderDP[3][N], remainderX[3];

int calcRemainder(int k, int num1) {
  remainderDP[k][n - 1] = 1 % num1;
  if (x[n - 1] == '1') {
    remainderX[k] = remainderDP[k][n - 1];
  }
  for (int i = 1; i < n; i++) {
    remainderDP[k][n - 1 - i] = (remainderDP[k][n - i] * 2) % num1;
    if (x[n - 1 - i] == '1') {
      remainderX[k] = (remainderX[k] + remainderDP[k][n - 1 - i]) % num1;
    }
  }
  return 0;
}

int popcount() {
  int cnt = 0;
  for (int i = 0; i < x.size(); i++) {
    if (x[i] == '1') {
      cnt++;
    }
  }
  return cnt;
}

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

  int numOneX = popcount();
  for (int i = 0; i <= 2; i++) {
    calcRemainder(i, numOneX - 1 + i);
  }
  for (int i = 0; i < n; i++) {
    int cnt = 0, remainderXi;
    if (x[i] == '1') {
      remainderXi =
          (remainderX[0] - remainderDP[0][i] + numOneX - 1) % (numOneX - 1);
    } else {
      remainderXi = (remainderX[2] + remainderDP[2][i]) % (numOneX + 1);
    }
    cnt++;

    while (remainderXi != 0) {
      int remainderTmp = remainderXi;
      int numOne2 = 0;
      while (remainderTmp != 0) {
        numOne2 += remainderTmp & 1;
        remainderTmp = remainderTmp >> 1;
      }
      remainderXi = remainderXi % numOne2;
      cnt++;
    }

    cout << cnt << endl;
  }

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

#include <cstdio>

int n;
char x[200005];

int popcount(int x) {
  if (x == 0) {
    return 0;
  }
  if (x % 2 == 0) {
    return popcount(x / 2);
  } else {
    return popcount(x / 2) + 1;
  }
}

int f(int s) {
  int t = 1;
  while (s) {
    s %= popcount(s);
    t++;
  }
  return t;
}

int pow(int x, int y, int z) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return pow(1ll * x * x % z, y / 2, z) % z;
  } else {
    return 1ll * x * pow(x, y - 1, z) % z;
  }
}

int main() {
  scanf("%d", &n);
  scanf("%s", x);
  int pc = 0;
  for (int i = 0; x[i]; i++) {
    if (x[i] == '1') {
      pc++;
    }
  }
  int res_p = 0;
  int res_m = 0;
  for (int i = 0; x[i]; i++) {
    res_p = (res_p * 2 + (x[i] - '0')) % (pc + 1);
    if (pc != 1)
      res_m = (res_m * 2 + (x[i] - '0')) % (pc - 1);
  }
  for (int i = 0; i < n; i++) {
    int pc_n = x[i] == '0' ? pc + 1 : pc - 1;
    if (pc_n == 0) {
      printf("0\\n");
      continue;
    }
    int diff = pow(2, n - i - 1, pc_n);
    int res;
    if (x[i] == '0') {
      res = res_p + diff;
    } else {
      res = res_m - diff;
    }
    res = ((res % pc_n) + pc_n) % pc_n;
    printf("%d\\n", f(res));
  }
}


Error: 0

Fixed submission:

#include <cstdio>

int n;
char x[200005];

int popcount(int x) {
  if (x == 0) {
    return 0;
  }
  if (x % 2 == 0) {
    return popcount(x / 2);
  } else {
    return popcount(x / 2) + 1;
  }
}

int f(int s) {
  int t = 1;
  while (s) {
    s %= popcount(s);
    t++;
  }
  return t;
}

int pow(int x, int y, int z) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return pow(1ll * x * x % z, y / 2, z) % z;
  } else {
    return 1ll * x * pow(x, y - 1, z) % z;
  }
}

int main() {
  scanf("%d", &n);
  scanf("%s", x);
  int pc = 0;
  for (int i = 0; x[i]; i++) {
    if (x[i] == '1') {
      pc++;
    }
  }
  int res_p = 0;
  int res_m = 0;
  for (int i = 0; x[i]; i++) {
    res_p = (res_p * 2 + (x[i] - '0')) % (pc + 1);
    if (pc != 1)
      res_m = (res_m * 2 + (x[i] - '0')) % (pc - 1);
  }
  for (int i = 0; i < n; i++) {
    int pc_n = x[i] == '0' ? pc + 1 : pc - 1;
    int diff = pow(2, n - i - 1, pc_n);
    int res;
    if (x[i] == '0') {
      res = res_p + diff;
    } else {
      res = res_m - diff;
    }
    res = ((res % pc_n) + pc_n) % pc_n;
    printf("%d\\n", f(res));
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, dis[200005][3];
ll cnt_0, cnt_1;
ll f(ll x, ll cnt) {
  while (x != 0) {
    ll m = x;
    ll q = 0;
    while (m) {
      if (m & 1)
        q++;
      m = m / 2;
    }
    x = x % q;
    cnt++;
  }
  return cnt;
}
int main() {
  string s;
  cin >> n >> s;
  ll cnt = 0;
  for (ll i = 0; i < n; i++)
    if (s[i] == '1')
      cnt++;
  for (ll i = n - 1, sum = 1; i >= 0 && cnt != 1; i--) {
    dis[i][0] = sum;
    if (s[i] == '1')
      cnt_0 = cnt_0 + sum;
    sum = (sum * 2) % (cnt - 1);
  }
  for (ll i = n - 1, sum = 1; i >= 0; i--) {
    dis[i][1] = sum;
    if (s[i] == '1')
      cnt_1 = cnt_1 + sum;
    sum = (sum * 2) % (cnt + 1);
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll x = cnt_1 + dis[i][1];
      x = x % (cnt + 1);
      cout << f(x, 1) << endl;
    }
    if (s[i] == '1') {
      ll x = cnt_0 - dis[i][0];
      if (cnt == 1)
        cout << 0 << endl;
      else {
        x = x % (cnt - 1);
        cout << f(x, 1) << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, dis[200005][3];
ll cnt_0, cnt_1;
ll f(ll x, ll cnt) {
  while (x != 0) {
    ll m = x;
    ll q = 0;
    while (m) {
      if (m & 1)
        q++;
      m = m / 2;
    }
    x = x % q;
    cnt++;
  }
  return cnt;
}
int main() {
  string s;
  cin >> n >> s;
  ll cnt = 0;
  for (ll i = 0; i < n; i++)
    if (s[i] == '1')
      cnt++;
  for (ll i = n - 1, sum = 1; i >= 0; i--) {
    dis[i][0] = sum;
    if (s[i] == '1')
      cnt_0 = cnt_0 + sum;
    sum = (sum * 2) % (cnt - 1);
  }
  for (ll i = n - 1, sum = 1; i >= 0; i--) {
    dis[i][1] = sum;
    if (s[i] == '1')
      cnt_1 = cnt_1 + sum;
    sum = (sum * 2) % (cnt + 1);
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll x = cnt_1 + dis[i][1];
      x = x % (cnt + 1);
      cout << f(x, 1) << endl;
    }
    if (s[i] == '1') {
      ll x = cnt_0 - dis[i][0];
      if (cnt - 1 == 0)
        cout << 0 << endl;
      else {
        x = x % (cnt - 1);
        cout << f(x, 1) << endl;
      }
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <ctype.h>
#define DEBUG
const int bufSize = 1e6;
inline char nc() {
#ifdef DEBUG
  return getchar();
#endif
  static char buf[bufSize], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <typename T> inline T read(T &r) {
  static char c;
  r = 0;
  for (c = nc(); !isdigit(c);)
    c = nc();
  for (; isdigit(c); c = nc())
    r = r * 10 + c - 48;
  return r;
}
const int maxn = 2e7 + 100;
char s[maxn];
int n;
int f1[maxn], f2[maxn];
inline int popcount(int x) {
  int ans = 0;
  while (x)
    ans += x & 1, x >>= 1;
  return ans;
}
inline int opt(int x) {
  int ans = 0;
  while (x)
    x %= popcount(x), ++ans;
  return ans;
}
int main() {
  read(n);
  scanf("%s", s + 1);
  int t = 0, t1 = 0, t2 = 0;
  for (int i = 1; i <= n; ++i)
    t += (s[i] == '1');
  t1 = t + 1, t2 = t - 1;
  f1[n] = f2[n] = 1;
  for (int i = n - 1; i; --i) {
    f1[i] = (f1[i + 1] * 2) % t1;
    if (t2 != 0)
      f2[i] = (f2[i + 1] * 2) % t2;
  }
  int temp1 = 0, temp2 = 0;
  for (int i = 1; i <= n; ++i) {
    temp1 <<= 1, temp2 <<= 1;
    temp1 += s[i] - 48, temp1 %= t1;
    if (t2 != 0)
      temp2 += s[i] - 48, temp2 %= t2;
  }
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1') {
      if (t2 == 0) {
        printf("0\\n");
        continue;
      }
      int val = ((temp2 - f2[i]) % t2 + t2) % t2;
      printf("%d\\n", opt(val) + 1);
    } else {
      int val = (temp1 + f1[i] + t1) % t1;
      printf("%d\\n", opt(val) + 1);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <ctype.h>
#define DEBUG
const int bufSize = 1e6;
inline char nc() {
#ifdef DEBUG
  return getchar();
#endif
  static char buf[bufSize], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <typename T> inline T read(T &r) {
  static char c;
  r = 0;
  for (c = nc(); !isdigit(c);)
    c = nc();
  for (; isdigit(c); c = nc())
    r = r * 10 + c - 48;
  return r;
}
const int maxn = 2e7 + 100;
char s[maxn];
int n;
int f1[maxn], f2[maxn];
inline int popcount(int x) {
  int ans = 0;
  while (x)
    ans += x & 1, x >>= 1;
  return ans;
}
inline int opt(int x) {
  int ans = 0;
  while (x)
    x %= popcount(x), ++ans;
  return ans;
}
int main() {
  read(n);
  scanf("%s", s + 1);
  int t = 0, t1 = 0, t2 = 0;
  for (int i = 1; i <= n; ++i)
    t += (s[i] == '1');
  t1 = t + 1, t2 = t - 1;
  int temp1 = 0, temp2 = 0;
  for (int i = 1; i <= n; ++i) {
    temp1 <<= 1, temp2 <<= 1;
    temp1 += s[i] - 48, temp1 %= t1;
    temp2 += s[i] - 48, temp2 %= t2;
  }
  f1[n] = f2[n] = 1;
  for (int i = n - 1; i; --i)
    f1[i] = (f1[i + 1] * 2) % t1, f2[i] = (f2[i + 1] * 2) % t2;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1') {
      int val = ((temp2 - f2[i]) % t2 + t2) % t2;
      printf("%d\\n", opt(val) + 1);
    } else {
      int val = (temp1 + f1[i] + t1) % t1;
      printf("%d\\n", opt(val) + 1);
    }
  }
  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;
using ll = long long;
const double PI = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

ll f(ll x) {
  if (x == 0)
    return 0;
  return f(x % __builtin_popcountll(x)) + 1;
}

int main() {
  ll n;
  string s;
  cin >> n >> s;
  ll cnt = 0;
  rep(i, n) cnt += s[i] - '0';
  vector<ll> ans(n, 0);
  rep(i, 2) {
    ll tmp = cnt + (i == 0 ? 1 : -1);
    if (tmp <= 0)
      continue;
    ll r = 0;
    rep(j, n) {
      r = (r * 2) % tmp;
      r += s[j] - '0';
    }
    ll t = 1;
    for (ll j = n - 1; j >= 0; --j) {
      if (s[j] - '0' == i) {
        ll u = r;
        if (i == 0)
          u = (u + t) % tmp;
        else
          u = (u - t + tmp) % tmp;
        ans[j] = f(u) + 1;
      }
      t = (t * 2) % tmp;
    }
  }
  for (auto i : ans) {
    cout << i << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

ll f(ll x) {
  if (x == 0)
    return 0;
  return f(x % __builtin_popcountll(x)) + 1;
}

int main() {
  ll n;
  string s;
  cin >> n >> s;
  ll cnt = 0;
  rep(i, n) cnt += s[i] - '0';
  vector<ll> ans(n);
  rep(i, 2) {
    ll tmp = cnt + (i == 0 ? 1 : -1);
    ll r = 0;
    rep(j, n) {
      r = (r * 2) % tmp;
      r += s[j] - '0';
    }
    ll t = 1;
    for (ll j = n - 1; j >= 0; --j) {
      if (s[j] - '0' == i) {
        ll u = r;
        if (i == 0)
          u = (u + t) % tmp;
        else
          u = (u - t + tmp) % tmp;
        ans[j] = f(u) + 1;
      }
      t = (t * 2) % tmp;
    }
  }
  for (auto i : ans) {
    cout << i << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
// 解説写し
int pcnt(int x) { return __builtin_popcount(x); }
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  for (int i = 0; i < n; i++)
    x[i] = s[i] - '0';
  int pc = 0;
  for (int i = 0; i < n; i++)
    pc += x[i];
  vector<int> ans(n);
  for (int b = 0; b < 2; b++) {
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    if (npc <= 0)
      continue;
    int r0 = 0;
    for (int i = 0; i < n; i++) {
      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;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// 解説写し
int pcnt(int x) { return __builtin_popcount(x); }
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  for (int i = 0; i < n; i++)
    x[i] = s[i] - '0';
  int pc = 0;
  for (int i = 0; i < n; i++)
    pc += x[i];
  vector<int> ans(n);
  for (int b = 0; b < 2; b++) {
    int npc = pc;
    if (npc <= 0)
      continue;
    if (b == 0)
      npc++;
    else
      npc--;
    int r0 = 0;
    for (int i = 0; i < n; i++) {
      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;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
char s[200200];
ll ans[200200];
int qp(ll a, ll b, ll md) {
  ll ret = 1;
  a = a % md;
  while (b) {
    if (b & 1)
      ret = ret * a % md;
    a = a * a % md;
    b >>= 1;
  }
  return ret;
}
ll c(ll x) {
  ll ret = 0;
  while (x) {
    if (x & 1)
      ret++;
    x >>= 1;
  }
  return ret;
}
ll calc(ll x) {
  ll ret = 0;
  while (x) {
    ret++;
    x %= c(x);
  }
  return ret;
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int c1 = 0, c0 = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0')
      c0++;
    else
      c1++;
  }
  ll md = c1 + 1, ba = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      ba = (qp(2, n - i, md) + ba) % md;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0') {
      ans[i] = (ba + qp(2, n - i, md)) % md;
      ans[i] = calc(ans[i]) + 1;
    }
  }
  ba = 0;
  md = c1 - 1;
  if (md <= 0) {
    for (int i = 1; i <= n; i++) {
      if (s[i] == '1')
        ans[i] = 0;
    }
  } else {
    for (int i = 1; i <= n; i++) {
      if (s[i] == '1')
        ba = (qp(2, n - i, md) + ba) % md;
    }
    for (int i = 1; i <= n; i++) {
      if (s[i] == '1') {
        ans[i] = (ba - qp(2, n - i, md) + md) % md;
        ans[i] = calc(ans[i]) + 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
char s[200200];
int ans[200200];
int qp(int a, int b, int md) {
  int ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % md;
    a = a * a % md;
    b >>= 1;
  }
  return ret;
}
int c(int x) {
  int ret = 0;
  while (x) {
    if (x & 1)
      ret++;
    x >>= 1;
  }
  return ret;
}
int calc(int x) {
  int ret = 0;
  while (x) {
    ret++;
    x %= c(x);
  }
  return ret;
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int c1 = 0, c0 = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0')
      c0++;
    else
      c1++;
  }
  int md = c1 + 1, ba = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      ba = (qp(2, n - i, md) + ba) % md;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0') {
      ans[i] = (ba + qp(2, n - i, md)) % md;
      ans[i] = calc(ans[i]) + 1;
    }
  }
  ba = 0;
  md = c1 - 1;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      ba = (qp(2, n - i, md) + ba) % md;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      ans[i] = (ba - qp(2, n - i, md) + md) % md;
      ans[i] = calc(ans[i]) + 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}