prithivMLmods/Coder-Stat huggingface dataset:
Problem p02608 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int i, j, k;
    int q;
    int f[10001];
    memset(f, 0, sizeof(f));
    q = sqrt(n);
    for (i = 1; i <= q; i++) {
      for (j = 1; j <= q; j++) {
        for (k = 1; k <= q; k++) {
          int d = i * i + j * j + k * k + i * j + j * k + i * k;
          f[d]++;
        }
      }
    }
    for (i = 1; i <= n; i++) {
      printf("%d\n", f[i]);
    }
    printf("\n");
  }
  return (0);
}


Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int i, j, k;
    int q;
    int f[10001];
    memset(f, 0, sizeof(f));
    q = sqrt(n);
    for (i = 1; i <= q; i++) {
      for (j = 1; j <= q; j++) {
        for (k = 1; k <= q; k++) {
          int d = i * i + j * j + k * k + i * j + j * k + i * k;
          if (d <= 10000) {
            f[d]++;
          }
        }
      }
    }
    for (i = 1; i <= n; i++) {
      printf("%d\n", f[i]);
    }
    printf("\n");
  }
  return (0);
}
Problem p02608 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int i, N, x, y, z, t;

  cin >> N;
  vector<int> a(N);
  // vector<int> b(M);
  // for (i = 1; i <= N; i++){
  for (i = 1; i <= N; i++) {
    a[i] = 0;
  }
  for (x = 1; x <= N; x++) {
    for (y = 1; y <= N; y++) {
      for (z = 1; z <= N; z++) {
        t = pow(x, 2) + pow(y, 2) + pow(z, 2) + x * y + y * z + x * z;
        if (t <= N) {
          a[t] += 1;
        }
      }
    }
  }

  for (i = 1; i <= N; i++) {
    cout << a[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int i, N, x, y, z, t;

  cin >> N;
  vector<int> a(N);
  // vector<int> b(M);
  // for (i = 1; i <= N; i++){
  for (i = 1; i <= N; i++) {
    a[i] = 0;
  }
  for (x = 1; x <= sqrt(N); x++) {
    for (y = 1; y <= sqrt(N); y++) {
      for (z = 1; z <= sqrt(N); z++) {
        t = pow(x, 2) + pow(y, 2) + pow(z, 2) + x * y + y * z + x * z;
        if (t <= N) {
          a[t] += 1;
        }
      }
    }
  }

  for (i = 1; i <= N; i++) {
    cout << a[i] << endl;
  }

  return 0;
}
Problem p02608 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std; // std::の省略のため
using ll = long long;

int main() {

  ll n, a;
  int x, y, z;
  cin >> n;
  vector<int> f(n);

  int i = 0;

  while (i < n) {
    f[i] = 0;
    for (x = 1; x <= sqrt(i + 1); x++) {
      for (y = 1; y <= sqrt(i + 1); y++) {
        for (z = 1; z <= sqrt(i + 1); z++) {
          a = x * x + y * y + z * z + x * y + y * z + z * x;
          if (a == i + 1)
            f[i]++;
        }
      }
    }

    cout << f[i] << endl;
    i++;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std; // std::の省略のため
using ll = long long;

int main() {

  ll n, a;
  int x, y, z;
  cin >> n;
  vector<int> f(n);

  int i = 0;

  while (i < n) {
    f[i] = 0;
    for (x = 1; x <= sqrt(i + 1); x++) {
      for (y = 1; y <= sqrt(i + 1); y++) {
        for (z = 1; z <= sqrt(i + 1); z++) {
          a = x * x + y * y + z * z + x * y + y * z + z * x;
          if (a == i + 1)
            f[i]++;
          if (a > i + 1)
            break;
        }
      }
    }

    cout << f[i] << endl;
    i++;
  }

  return 0;
}
Problem p02608 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(void) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll ans = 0;
    for (int x = 1; x * x < i; x++) {
      for (int y = 1; y * y < i; y++) {
        int d = 4 * i - 3 * x * x - 3 * y * y - 2 * x * y;
        for (int j = 0; j * j <= d; j++) {
          if (j * j == d) {
            int z = (-1 * (x + y) + j);
            if (z < 1)
              continue;
            if (z % 2 == 1)
              continue;
            else {
              z /= 2;
              if (x * x + y * y + z * z + x * y + y * z + z * x == i) {
                ans++;
                // cout << x << ", " << y << ", " << z << endl;
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(void) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll ans = 0;
    for (int x = 1; x * x < i; x++) {
      for (int y = 1; y * y < i; y++) {
        int d = 4 * i - 3 * x * x - 3 * y * y - 2 * x * y;
        if (d < 0)
          continue;
        int z = (-1 * (x + y) + sqrt(d));
        if (z < 1)
          continue;
        else {
          z /= 2;
          if (x * x + y * y + z * z + x * y + y * z + z * x == i) {
            ans++;
            // cout << x << ", " << y << ", " << z << endl;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 10;
int n, ans[N];

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

  cin >> n;

  for (int x = 1; x * x <= n; x++) {
    for (int y = 1; y * y <= n; y++) {
      for (int z = 1; z * z <= n; z++) {
        int f = 0;
        f += x * x + y * y + z * z;
        f += x * y + y * z + z * x;
        ans[f] += 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << ans[i] << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, ans[N];

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

  cin >> n;

  for (int x = 1; x * x <= n; x++) {
    for (int y = 1; y * y <= n; y++) {
      for (int z = 1; z * z <= n; z++) {
        int f = 0;
        f += x * x + y * y + z * z;
        f += x * y + y * z + z * x;
        ans[f] += 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << ans[i] << '\n';
  }

  return 0;
}
Problem p02608 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

long f(long x, long y, long z) {
  return pow(x + y + z, 2) - x * y - y * z - z * x;
}

int main() {
  long N;
  cin >> N;

  for (int i = 1; i <= N; i++) {
    long cnt = 0;
    long max = sqrt(i);
    for (long x = 1; x <= max; x++) {
      for (long y = 1; y <= max; y++) {
        for (long z = 1; z <= max; z++) {
          if (f(x, y, z) == i)
            cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}


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

long f(long x, long y, long z) {
  return pow(x + y + z, 2) - x * y - y * z - z * x;
}

int main() {
  long N;
  cin >> N;

  for (int i = 1; i <= N; i++) {
    long cnt = 0;
    long max = sqrt(i);
    for (long x = 1; x <= max; x++) {
      for (long y = 1; y <= x; y++) {
        for (long z = 1; z <= y; z++) {
          if (f(x, y, z) == i) {
            if (x == y) {
              if (y == z) {
                cnt++;
              } else {
                cnt = cnt + 3;
              }
            } else if (y == z) {
              if (z == x) {
                cnt++;
              } else {
                cnt = cnt + 3;
              }
            } else if (z == x) {
              if (x == y) {
                cnt++;
              } else {
                cnt = cnt + 3;
              }
            } else {
              cnt = cnt + 6;
            }
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p02608 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200000005];
char s[100][8];
int main() {
  ll n, m, k, i, j, l, ans = 0, K, x = 1, y = 1, r, d, z, flag = 1;
  scanf("%lld", &n);
  for (x = 1; x <= n; x++) {
    for (y = 1; y <= n; y++) {
      for (z = 1; z <= n; z++) {
        a[x * x + y * y + z * z + x * y + y * z + z * x]++;
        if (x * x + y * y + z * z + x * y + y * z + z * x > n) {
          flag = 0;
          break;
        }
      }
      // if(flag==0)break;
    }
    //  if(flag==0)break;
  }
  for (x = 1; x <= n; x++)
    printf("%lld\n", a[x]);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200000005];
char s[100][8];
int main() {
  ll n, m, k, i, j, l, ans = 0, K, x = 1, y = 1, r, d, z, flag = 1;
  scanf("%lld", &n);
  for (x = 1; x * x <= n; x++) {
    for (y = 1; y * y <= n; y++) {
      for (z = 1; z * z <= n; z++) {
        a[x * x + y * y + z * z + x * y + y * z + z * x]++;
        if (x * x + y * y + z * z + x * y + y * z + z * x > n) {
          flag = 0;
          break;
        }
      }
      // if(flag==0)break;
    }
    //  if(flag==0)break;
  }
  for (x = 1; x <= n; x++)
    printf("%lld\n", a[x]);

  return 0;
}
Problem p02608 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;

int main(void) {
  ll n;
  cin >> n;
  ll count = 0;
  for (int i = 1; i <= n; i++) {
    count = 0;
    for (int x = 1; x <= sqrt(2 * i) + 1; x++) {
      for (int y = 1; y <= sqrt(2 * i) + 1; y++) {
        for (int z = 1; z <= sqrt(2 * i) + 1; z++) {
          if ((x + y) * (x + y) + (x + z) * (x + z) + (y + z) * (y + z) ==
              2 * i) {
            count++;
          }
        }
      }
    }
    cout << count << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;

int main(void) {
  ll n;
  cin >> n;
  ll count = 0;
  for (int i = 1; i <= n; i++) {
    count = 0;
    for (int x = 1; x <= sqrt(2 * i) + 1; x++) {
      for (int y = 1; y <= sqrt(2 * i) + 1; y++) {
        if (i - (x * x + x * y + y * y) < 2) {
          continue;
        }
        for (int z = 1; z * z + x * z + y * z <= i - (x * x + y * y + x * y);
             z++) {
          if ((x + y) * (x + y) + (x + z) * (x + z) + (y + z) * (y + z) ==
              2 * i) {
            count++;
          }
        }
      }
    }
    cout << count << endl;
  }
}
Problem p02608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> ans(1001, 0);

  for (int x = 1; x <= 100; x++) {
    for (int y = 1; y <= 100; y++) {
      for (int z = 1; z <= 100; z++) {
        int m = x * x + y * y + z * z + x * y + y * z + z * x;
        if (m <= n)
          ans.at(m)++;
      }
    }
  }

  for (int i = 1; i <= n; i++)
    cout << ans.at(i) << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> ans(10001, 0);

  for (int x = 1; x <= 100; x++) {
    for (int y = 1; y <= 100; y++) {
      for (int z = 1; z <= 100; z++) {
        int m = x * x + y * y + z * z + x * y + y * z + z * x;
        if (m <= n)
          ans.at(m)++;
      }
    }
  }

  for (int i = 1; i <= n; i++)
    cout << ans.at(i) << endl;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, b[200009], num = 0;
ll tmp = 0, c[200009], d[200009], tmp1 = 0;
char a[200009];
ll pow1(ll a, int n, ll m) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return ans;
}
void solve(int x) {
  // cout<<x<<endl;
  int num = 1;
  while (x) {
    int tmp = x, num1 = 0;
    while (tmp) {
      num1 += tmp % 2;
      tmp /= 2;
    }
    x %= num1;
    num++;
  }
  printf("%d\n", num);
}
int main() {
  scanf("%d%s", &n, a);
  for (int i = 0; i < n; i++)
    b[n - i - 1] = a[i] - '0';
  for (int i = 0; i < n; i++)
    num += b[i];
  for (int i = 0; i < n; i++) {
    c[i] = pow1(2, i, num - 1);
    d[i] = pow1(2, i, num + 1);
    if (b[i])
      tmp += c[i], tmp1 += d[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    if (b[i]) {
      if (num - 1 != 0)
        solve((tmp - c[i]) % (num - 1));
      else
        printf("0\n");
    } else
      solve((tmp1 + d[i]) % (num + 1));
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, b[200009], num = 0;
ll tmp = 0, c[200009], d[200009], tmp1 = 0;
char a[200009];
ll pow1(ll a, int n, ll m) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return ans;
}
void solve(int x) {
  // cout<<x<<endl;
  int num = 1;
  while (x) {
    int tmp = x, num1 = 0;
    while (tmp) {
      num1 += tmp % 2;
      tmp /= 2;
    }
    x %= num1;
    num++;
  }
  printf("%d\n", num);
}
int main() {
  scanf("%d%s", &n, a);
  for (int i = 0; i < n; i++)
    b[n - i - 1] = a[i] - '0';
  for (int i = 0; i < n; i++)
    num += b[i];
  for (int i = 0; i < n; i++) {
    if (num - 1 > 0)
      c[i] = pow1(2, i, num - 1);
    d[i] = pow1(2, i, num + 1);
    if (b[i])
      tmp += c[i], tmp1 += d[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    if (b[i]) {
      if (num - 1 != 0)
        solve((tmp - c[i]) % (num - 1));
      else
        printf("0\n");
    } else
      solve((tmp1 + d[i]) % (num + 1));
  }
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int popcount(int x) {
  vector<int> a;
  while (x != 0) {
    a.emplace_back(x % 2);
    x /= 2;
  }
  int res = 0;
  for (auto p : a) {
    if (p == 1) {
      res++;
    }
  }

  return res;
}

int f(int x) {
  if (x == 0) {
    return 1;
  }
  int pcnt = popcount(x);
  return f(x % pcnt) + 1;
}

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<int> X(N);
  for (int i = 0; i < N; i++) {
    X[i] = (int)S[i] - (int)'0';
  }

  long long one = 0;
  for (int i = 0; i < N; i++) {
    one += X[i];
  }

  vector<int> m(2, 0);

  for (int i = 0; i < N; i++) {
    m[0] = (m[0] * 2) % (one + 1);
    m[0] += X[i];
  }
  if (one > 1) {
    for (int i = 0; i < N; i++) {
      m[1] = (m[1] * 2) % (one - 1);
      m[1] += X[i];
    }
  }
  // cout<<"m:"<<m[0]<<" "<<m[1]<<endl;

  vector<int> ans(N);
  int k0, k1;
  k0 = 1;
  k1 = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == 0) {
      ans[i] = f((m[0] + k0) % (one + 1));

    } else {
      if (one == 1) {
        ans[i] = 0;
      } else {
        ans[i] = f((m[1] - k1 + (one - 1)) % (one - 1));
      }
    }

    k0 = k0 * 2 % (one + 1);
    if (one != 0) {
      k1 = k1 * 2 % (one - 1);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int popcount(int x) {
  vector<int> a;
  while (x != 0) {
    a.emplace_back(x % 2);
    x /= 2;
  }
  int res = 0;
  for (auto p : a) {
    if (p == 1) {
      res++;
    }
  }

  return res;
}

int f(int x) {
  if (x == 0) {
    return 1;
  }
  int pcnt = popcount(x);
  return f(x % pcnt) + 1;
}

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<int> X(N);
  for (int i = 0; i < N; i++) {
    X[i] = (int)S[i] - (int)'0';
  }

  long long one = 0;
  for (int i = 0; i < N; i++) {
    one += X[i];
  }

  vector<int> m(2, 0);

  for (int i = 0; i < N; i++) {
    m[0] = (m[0] * 2) % (one + 1);
    m[0] += X[i];
  }
  if (one > 1) {
    for (int i = 0; i < N; i++) {
      m[1] = (m[1] * 2) % (one - 1);
      m[1] += X[i];
    }
  }
  // cout<<"m:"<<m[0]<<" "<<m[1]<<endl;

  vector<int> ans(N);
  int k0, k1;
  k0 = 1;
  k1 = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == 0) {
      ans[i] = f((m[0] + k0) % (one + 1));

    } else {
      if (one == 1) {
        ans[i] = 0;
      } else {
        ans[i] = f((m[1] - k1 + (one - 1)) % (one - 1));
      }
    }

    k0 = k0 * 2 % (one + 1);
    if (one > 1) {
      k1 = k1 * 2 % (one - 1);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1e9 + 7;

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

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> f(N);
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
  }
  // choose 0 or 1
  ll r0 = 0, r1 = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == '1') {
      r0 = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        r1 = (r1 + modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  for (ll i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll val = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (val < 0)
        val += (cnt + 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    } else {
      ll val = (r1 - modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      if (val < 0)
        val += (cnt - 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << f[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1e9 + 7;

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

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> f(N);
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
  }
  // choose 0 or 1
  ll r0 = 0, r1 = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == '1') {
      r0 = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        r1 = (r1 + modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  for (ll i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll val = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (val < 0)
        val += (cnt + 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    } else {
      if (cnt == 1) {
        f[i] = 0;
        continue;
      }
      ll val = (r1 - modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      if (val < 0)
        val += (cnt - 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << f[i] << endl;
  }
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int memo[200005];

int calc(int x) {
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];
  int p = 0;
  int y = x;
  while (y > 0) {
    p += y % 2;
    y /= 2;
  }
  if (p == 0)
    return 0;
  return calc(x % p) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  memset(memo, -1, sizeof(memo));
  memo[0] = 0;
  for (int i = 1; i < 200005; ++i) {
    memo[i] = calc(i);
  }
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  int lCnt = cnt - 1;
  if (cnt == 0)
    lCnt = 0;
  int uCnt = cnt + 1;
  int lNum = 0, uNum = 0;
  rep(i, n) {
    lNum = (lNum * 2 + (s[i] - '0')) % lCnt;
    uNum = (uNum * 2 + (s[i] - '0')) % uCnt;
  }
  int lTwo = 1, uTwo = 1;
  vector<int> ans(n, 0);
  for (int i = n - 1; i >= 0; --i) {
    int num = 0;
    if (s[i] == '0') {
      num = (uNum + uTwo) % uCnt;
      ans[i] = memo[num] + 1;
    } else {
      if (cnt != 1) {
        num = (lNum + lCnt - lTwo) % lCnt;
        ans[i] = memo[num] + 1;
      } else {
        ans[i] = 0;
      }
    }
    lTwo = (lTwo * 2) % lCnt;
    uTwo = (uTwo * 2) % uCnt;
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int memo[200005];

int calc(int x) {
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];
  int p = 0;
  int y = x;
  while (y > 0) {
    p += y % 2;
    y /= 2;
  }
  if (p == 0)
    return 0;
  return calc(x % p) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  memset(memo, -1, sizeof(memo));
  memo[0] = 0;
  for (int i = 1; i < 200005; ++i) {
    memo[i] = calc(i);
  }
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  int lCnt = cnt - 1;
  chmax(lCnt, 1);
  int uCnt = cnt + 1;
  int lNum = 0, uNum = 0;
  rep(i, n) {
    lNum = (lNum * 2 + (s[i] - '0')) % lCnt;
    uNum = (uNum * 2 + (s[i] - '0')) % uCnt;
  }
  int lTwo = 1, uTwo = 1;
  vector<int> ans(n, 0);
  for (int i = n - 1; i >= 0; --i) {
    int num = 0;
    if (s[i] == '0') {
      num = (uNum + uTwo) % uCnt;
      ans[i] = memo[num] + 1;
    } else {
      if (cnt != 1) {
        num = (lNum + lCnt - lTwo) % lCnt;
        ans[i] = memo[num] + 1;
      } else {
        ans[i] = 0;
      }
    }
    lTwo = (lTwo * 2) % lCnt;
    uTwo = (uTwo * 2) % uCnt;
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll t = count(X.begin(), X.end(), '1');
  vector<ll> powl(N + 1, 1), pow(N + 1, 1), powu(N + 1, 1);
  for (int i = 1; i <= N; i++) {
    if (t > 1)
      powl[i] = powl[i - 1] * 2 % (t - 1);
    pow[i] = pow[i - 1] * 2 % (t);
    powu[i] = powu[i - 1] * 2 % (t + 1);
  }
  ll allu = 0, all = 0, alll = 0;
  for (int i = 0; i <= N - 1; i++) {
    if (X[N - 1 - i] == '1') {
      alll += powl[i];
      all += pow[i];
      allu += powu[i];
      if (t > 1)
        alll %= t - 1;
      all %= t;
      allu %= t + 1;
    }
  }

  for (int i = 0; i <= N - 1; i++) {
    ll cnt = 1;
    ll now;
    if (X[i] == '0') {
      now = (allu + powu[N - 1 - i]) % (t + 1);
    } else {
      if (t == 1) {
        cout << 0 << endl;
        continue;
      }
      now = (alll + (t - 1) - powl[N - 1 - i]) % (t - 1);
    }
    while (now > 0) {
      now %= __builtin_popcount(now);
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll t = count(X.begin(), X.end(), '1');
  if (t == 0) {
    for (int i = 0; i < N; i++)
      cout << 1 << endl;
    return 0;
  }
  vector<ll> powl(N + 1, 1), pow(N + 1, 1), powu(N + 1, 1);
  for (int i = 1; i <= N; i++) {
    if (t > 1)
      powl[i] = powl[i - 1] * 2 % (t - 1);
    pow[i] = pow[i - 1] * 2 % (t);
    powu[i] = powu[i - 1] * 2 % (t + 1);
  }
  ll allu = 0, all = 0, alll = 0;
  for (int i = 0; i <= N - 1; i++) {
    if (X[N - 1 - i] == '1') {
      alll += powl[i];
      all += pow[i];
      allu += powu[i];
      if (t > 1)
        alll %= t - 1;
      all %= t;
      allu %= t + 1;
    }
  }

  for (int i = 0; i <= N - 1; i++) {
    ll cnt = 1;
    ll now;
    if (X[i] == '0') {
      now = (allu + powu[N - 1 - i]) % (t + 1);
    } else {
      if (t == 1) {
        cout << 0 << endl;
        continue;
      }
      now = (alll + (t - 1) - powl[N - 1 - i]) % (t - 1);
    }
    while (now > 0) {
      now %= __builtin_popcount(now);
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int popcount(int n) {
  int r = 0;
  while (n > 0) {
    r += n & 1;
    n >>= 1;
  }
  return r;
}
int popcount(string n) {
  int r = 0;
  for (auto c : n) {
    r += c == '1';
  }
  return r;
}
int f(int n) {
  int k = 0;
  while (n != 0) {
    n %= popcount(n);
    k++;
  }
  return k;
}
int pow_mod(int a, int b, int mod) {
  if (b == 0)
    return 1 % mod;
  int k = pow_mod(a, b / 2, mod);
  k *= k;
  k %= mod;
  if (b % 2 == 0)
    return k;
  return (k * a) % mod;
}
int main() {
  int N;
  string X;
  cin >> N >> X;
  reverse(X.begin(), X.end());
  int s = popcount(X);
  int A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      A += pow_mod(2, i, s + 1);
      A %= s + 1;
      if (s - 1 > 0) {
        B += pow_mod(2, i, s - 1);
        B %= s - 1;
      }
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      if (s - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      int b = B;
      b -= pow_mod(2, i, s - 1);
      b += s - 1;
      b %= s - 1;
      cout << 1 + f(b) << endl;
    } else {
      int a = A;
      a += pow_mod(2, i, s + 1);
      a %= s + 1;
      cout << 1 + f(a) << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int popcount(int n) {
  int r = 0;
  while (n > 0) {
    r += n & 1;
    n >>= 1;
  }
  return r;
}
int popcount(string n) {
  int r = 0;
  for (auto c : n) {
    r += c == '1';
  }
  return r;
}
int f(int n) {
  int k = 0;
  while (n != 0) {
    n %= popcount(n);
    k++;
  }
  return k;
}
int pow_mod(int a, int b, int mod) {
  if (b == 0)
    return 1 % mod;
  ll k = pow_mod(a, b / 2, mod);
  k *= k;
  k %= mod;
  if (b % 2 == 0)
    return k;
  return (k * a) % mod;
}
int main() {
  int N;
  string X;
  cin >> N >> X;
  reverse(X.begin(), X.end());
  int s = popcount(X);
  int A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      A += pow_mod(2, i, s + 1);
      A %= s + 1;
      if (s - 1 > 0) {
        B += pow_mod(2, i, s - 1);
        B %= s - 1;
      }
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      if (s - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      int b = B;
      b -= pow_mod(2, i, s - 1);
      b += s - 1;
      b %= s - 1;
      cout << 1 + f(b) << endl;
    } else {
      int a = A;
      a += pow_mod(2, i, s + 1);
      a %= s + 1;
      cout << 1 + f(a) << endl;
    }
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

long long Calc(long long P) {
  if (P == 0)
    return 0;
  long long COUNT = 0;
  long long Q = P;
  while (Q > 0) {
    COUNT += Q % 2;
    Q /= 2;
  }
  return Calc(P % COUNT) + 1;
}

long long Pow(long long A, long long B, long long M) {
  if (B == 0)
    return 1 % M;
  if (B % 2 == 0) {
    long long C = Pow(A, B / 2, M);
    return (C * C) % M;
  }
  return (A * Pow(A, B - 1, M)) % M;
}

int main() {
  long long N;
  string S;
  cin >> N >> S;
  long long X = 0;
  for (int i = 0; i < N; i++)
    X += (S[i] - '0');
  long long MOD1 = 0, MOD2 = 0, Y = X - 1, Z = X + 1;
  if (Y != 0) {
    for (int i = 0; i < N; i++) {
      MOD1 *= 2;
      MOD1 += S[i] - '0';
      MOD1 %= Y;
    }
  }
  for (int i = 0; i < N; i++) {
    MOD2 *= 2;
    MOD2 += S[i] - '0';
    MOD2 %= Z;
  }
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      long long NEXT = (MOD2 + Pow(2, N - 1 - i, Z)) % Z;
      cout << Calc(NEXT) + 1 << endl;
    } else {
      if (Y == 0)
        cout << 0 << endl;
      long long NEXT = (MOD1 - Pow(2, N - 1 - i, Y) + Y) % Y;
      cout << Calc(NEXT) + 1 << endl;
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

long long Calc(long long P) {
  if (P == 0)
    return 0;
  long long COUNT = 0;
  long long Q = P;
  while (Q > 0) {
    COUNT += Q % 2;
    Q /= 2;
  }
  return Calc(P % COUNT) + 1;
}

long long Pow(long long A, long long B, long long M) {
  if (B == 0)
    return 1 % M;
  if (B % 2 == 0) {
    long long C = Pow(A, B / 2, M);
    return (C * C) % M;
  }
  return (A * Pow(A, B - 1, M)) % M;
}

int main() {
  long long N;
  string S;
  cin >> N >> S;
  long long X = 0;
  for (int i = 0; i < N; i++)
    X += (S[i] - '0');
  long long MOD1 = 0, MOD2 = 0, Y = X - 1, Z = X + 1;
  if (Y != 0) {
    for (int i = 0; i < N; i++) {
      MOD1 *= 2;
      MOD1 += S[i] - '0';
      MOD1 %= Y;
    }
  }
  for (int i = 0; i < N; i++) {
    MOD2 *= 2;
    MOD2 += S[i] - '0';
    MOD2 %= Z;
  }
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      long long NEXT = (MOD2 + Pow(2, N - 1 - i, Z)) % Z;
      cout << Calc(NEXT) + 1 << endl;
    } else {
      if (Y == 0)
        cout << 0 << endl;
      else {
        long long NEXT = (MOD1 - Pow(2, N - 1 - i, Y) + Y) % Y;
        cout << Calc(NEXT) + 1 << endl;
      }
    }
  }
}
Problem p02609 (C++)

Buggy submission (status: 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--;
    }
    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;
}

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--;
    }
    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;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

ll N;
string X;
ll pop[200010];
ll bin[200010][3];
vector<ll> v;

ll popcount(ll n) {
  if (n == 0)
    return 0;
  else if (pop[n] > 0)
    return pop[n];
  ll d = __builtin_popcount(n);
  pop[n] = 1 + popcount(n % d);
  return pop[n];
}

signed main() {
  cin >> N >> X;
  reverse(ALL(X));
  ll B = 0;
  REP(i, N) if (X[i] == '1') B++;
  bin[0][0] = bin[0][1] = bin[0][2] = 1;
  REP(i, N) {
    if (B > 1)
      bin[i + 1][0] = (bin[i][0] * 2) % (B - 1);
    bin[i + 1][1] = (bin[i][1] * 2) % B;
    bin[i + 1][2] = (bin[i][2] * 2) % (B + 1);
  }
  ll Y = 0, Z = 0;
  REP(i, N) {
    if (B > 1)
      Y = (Y + (X[i] - '0') * bin[i][0]) % (B - 1);
    Z = (Z + (X[i] - '0') * bin[i][2]) % (B + 1);
  }
  REP(i, N) {
    if (X[i] == '0') {
      v.push_back(popcount((Z + bin[i][2]) % (B + 1)));
    } else {
      if (B > 1)
        v.push_back(popcount((Y - bin[i][0] + B - 1) % (B - 1)));
      else
        v.push_back(-1);
    }
  }
  REP(i, N) { PRINT(v[N - i - 1] + 1); }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

ll N;
string X;
ll pop[200010];
ll bin[200010][3];
vector<ll> v;

ll popcount(ll n) {
  if (n == 0)
    return 0;
  else if (pop[n] > 0)
    return pop[n];
  ll d = __builtin_popcount(n);
  pop[n] = 1 + popcount(n % d);
  return pop[n];
}

signed main() {
  cin >> N >> X;
  reverse(ALL(X));
  ll B = 0;
  REP(i, N) if (X[i] == '1') B++;
  bin[0][0] = bin[0][1] = bin[0][2] = 1;
  REP(i, N) {
    if (B > 1)
      bin[i + 1][0] = (bin[i][0] * 2) % (B - 1);
    if (B > 0)
      bin[i + 1][1] = (bin[i][1] * 2) % B;
    bin[i + 1][2] = (bin[i][2] * 2) % (B + 1);
  }
  ll Y = 0, Z = 0;
  REP(i, N) {
    if (B > 1)
      Y = (Y + (X[i] - '0') * bin[i][0]) % (B - 1);
    Z = (Z + (X[i] - '0') * bin[i][2]) % (B + 1);
  }
  REP(i, N) {
    if (X[i] == '0') {
      v.push_back(popcount((Z + bin[i][2]) % (B + 1)));
    } else {
      if (B > 1)
        v.push_back(popcount((Y - bin[i][0] + B - 1) % (B - 1)));
      else
        v.push_back(-1);
    }
  }
  REP(i, N) { PRINT(v[N - i - 1] + 1); }
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = int;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll a, b, c, p, pc, np, h, i, j, k, l, m, n, y;
  ll ans = 0;
  string x, xx;
  cin >> n >> x;
  vector<ll> pcm(n), pcp(n);
  pc = 0;
  for (i = 0; i < n; i++)
    if (x[i] == '1')
      pc++;
  if (pc > 1)
    pcm[n - 1] = 1 % (pc - 1);
  else
    pcm[n - 1] = 0;
  pcp[n - 1] = 1 % (pc + 1);
  for (i = n - 2; i >= 0; i--) {
    if (pc > 1)
      pcm[i] = (pcm[i + 1] * 2) % (pc - 1);
    else
      pcm[i] = 0;
    pcp[i] = (pcp[i + 1] * 2) % (pc + 1);
  }
  a = b = 0;
  for (i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      a += pcm[i];
      a %= pc - 1;
      b += pcp[i];
      b %= pc + 1;
    }
  }

  // printf("pc=%d a=%d b=%d\n",pc , a , b);
  for (i = 0; i < n; i++) {
    ans = 0;
    if (x[i] == '0') {
      p = pc + 1;
      np = (b + pcp[i]) % p;
    } else {
      p = pc - 1;
      if (p != 0)
        np = (a + p - pcm[i]) % p;
      else
        np = 0;
    }
    if (p != 0)
      ans++;

    // printf("p=%d np=%d\n",p,np);

    while (np != 0) {
      ans++;
      p = __builtin_popcount(np);
      np = np % p;
    }

    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = int;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll a, b, c, p, pc, np, h, i, j, k, l, m, n, y;
  ll ans = 0;
  string x, xx;
  cin >> n >> x;
  vector<ll> pcm(n), pcp(n);
  pc = 0;
  for (i = 0; i < n; i++)
    if (x[i] == '1')
      pc++;
  if (pc > 1)
    pcm[n - 1] = 1 % (pc - 1);
  else
    pcm[n - 1] = 0;
  pcp[n - 1] = 1 % (pc + 1);
  for (i = n - 2; i >= 0; i--) {
    if (pc > 1)
      pcm[i] = (pcm[i + 1] * 2) % (pc - 1);
    else
      pcm[i] = 0;
    pcp[i] = (pcp[i + 1] * 2) % (pc + 1);
  }
  a = b = 0;
  for (i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      a += pcm[i];
      if (pc > 1)
        a %= pc - 1;
      b += pcp[i];
      b %= pc + 1;
    }
  }

  // printf("pc=%d a=%d b=%d\n",pc , a , b);
  for (i = 0; i < n; i++) {
    ans = 0;
    if (x[i] == '0') {
      p = pc + 1;
      np = (b + pcp[i]) % p;
    } else {
      p = pc - 1;
      if (p != 0)
        np = (a + p - pcm[i]) % p;
      else
        np = 0;
    }
    if (p != 0)
      ans++;

    // printf("p=%d np=%d\n",p,np);

    while (np != 0) {
      ans++;
      p = __builtin_popcount(np);
      np = np % p;
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

// 再帰関数によって操作回数をカウント
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % popcount(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> 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);
  // popcountが増える場合と減る場合で分けて考える
  rep(b, 2) {
    // 新しいpopcount
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    // npcが０以下の場合の例外処理
    // if (npc <= 0) continue;

    // 元のXに対して操作を行った結果を保持
    int r0 = 0;
    rep(i, n) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    // 下の桁から順にXiに対する操作結果を調べる．
    // 各桁ごとにk=2^n%npcを計算してr0と組み合わせて答えを算出．
    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;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}

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

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

// 再帰関数によって操作回数をカウント
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % popcount(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> 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);
  // popcountが増える場合と減る場合で分けて考える
  rep(b, 2) {
    // 新しいpopcount
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    // npcが０以下の場合の例外処理
    if (npc <= 0)
      continue;

    // 元のXに対して操作を行った結果を保持
    int r0 = 0;
    rep(i, n) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    // 下の桁から順にXiに対する操作結果を調べる．
    // 各桁ごとにk=2^n%npcを計算してr0と組み合わせて答えを算出．
    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;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
int f(int x) {
  for (int ans = 0;; x %= __builtin_popcount(x), ans++) {
    if (x == 0)
      return ans;
  }
}
int a[200000], b[200000];
signed main() {
  int n;
  string x;
  cin >> n >> x;
  int popx = count(x.begin(), x.end(), '1');
  if (popx == 1) {
    if (x.back() == '1') {
      for (int i = 0; i < n - 1; i++)
        cout << 2 << endl;
      cout << 0 << endl;
    } else {
      for (int i = 0; i < n - 1; i++)
        cout << (x[i] == '1' ? 0 : 1) << endl;
      cout << 2 << endl;
    }
  }
  a[n - 1] = b[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    a[i] = a[i + 1] * 2 % (popx + 1);
    b[i] = b[i + 1] * 2 % (popx - 1);
  }
  int aa = 0, bb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      (aa += a[i]) %= popx + 1;
      (bb += b[i]) %= popx - 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cout << f((bb - b[i] + popx - 1) % (popx - 1)) + 1 << endl;
    else
      cout << f((aa + a[i]) % (popx + 1)) + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
int f(int x) {
  for (int ans = 0;; x %= __builtin_popcount(x), ans++) {
    if (x == 0)
      return ans;
  }
}
int a[200000], b[200000];
signed main() {
  int n;
  string x;
  cin >> n >> x;
  int popx = count(x.begin(), x.end(), '1');
  if (popx == 1) {
    if (x.back() == '1') {
      for (int i = 0; i < n - 1; i++)
        cout << 2 << endl;
      cout << 0 << endl;
    } else {
      for (int i = 0; i < n - 1; i++)
        cout << (x[i] == '1' ? 0 : 1) << endl;
      cout << 2 << endl;
    }
    return 0;
  }
  a[n - 1] = b[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    a[i] = a[i + 1] * 2 % (popx + 1);
    b[i] = b[i + 1] * 2 % (popx - 1);
  }
  int aa = 0, bb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      (aa += a[i]) %= popx + 1;
      (bb += b[i]) %= popx - 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cout << f((bb - b[i] + popx - 1) % (popx - 1)) + 1 << endl;
    else
      cout << f((aa + a[i]) % (popx + 1)) + 1 << endl;
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

vector<int64_t> memo;
int64_t f(int64_t n) {
  if (n == 0) {
    return 0;
  }
  if (memo[n] != -1) {
    return memo[n];
  }

  return memo[n] = (f(n % __builtin_popcountll(n)) + 1);
}

// a^b % modを求める
int64_t MODpow(int64_t a, int64_t b, int64_t mod) {
  int64_t result = 1;
  while (b) {
    if (b % 2 == 1) {
      result *= a;
      result %= mod;
    }

    b /= 2;
    a *= a;
    a %= mod;
  }

  return result;
}

int main() {
  int64_t N;
  cin >> N;

  memo.resize(N + 2, -1);

  string X;
  cin >> X;

  int64_t b = count(X.begin(), X.end(), '1');
  // ビット数はb or (b + 1)になる

  int64_t case_b_minus_1 = 0, case_b_plus_1 = 0;
  for (int64_t i = 0; i < N; i++) {
    if (X[i] == '1') {
      (case_b_minus_1 += MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
      (case_b_plus_1 += MODpow(2, N - 1 - i, b + 1)) %= (b + 1);
    }
  }

  for (int64_t i = 0; i < N; i++) {
    // iを反転
    if (X[i] == '0') {
      // 1になる
      int64_t v = (case_b_plus_1 + MODpow(2, N - 1 - i, b + 1)) % (b + 1);
      cout << f(v) + 1 << endl;
    } else {
      // 0になる
      int64_t v = case_b_minus_1;
      // 自分の影響を除く
      (v += b - 1 - MODpow(2, N - 1 - i, b - 1)) %= (b - 1);

      cout << f(v) + 1 << endl;
    }
  }
}

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

vector<int64_t> memo;
int64_t f(int64_t n) {
  if (n == 0) {
    return 0;
  }
  if (memo[n] != -1) {
    return memo[n];
  }

  return memo[n] = (f(n % __builtin_popcountll(n)) + 1);
}

// a^b % modを求める
int64_t MODpow(int64_t a, int64_t b, int64_t mod) {
  int64_t result = 1;
  while (b) {
    if (b % 2 == 1) {
      result *= a;
      result %= mod;
    }

    b /= 2;
    a *= a;
    a %= mod;
  }

  return result;
}

int main() {
  int64_t N;
  cin >> N;

  memo.resize(N + 2, -1);

  string X;
  cin >> X;

  int64_t b = count(X.begin(), X.end(), '1');
  // ビット数はb or (b + 1)になる

  if (b == 1) {
    // b - 1が0になって終わるので適宜計算
    int64_t one_index = find(X.begin(), X.end(), '1') - X.begin();

    for (int64_t i = 0; i < N; i++) {
      if (i == one_index) {
        // 0になるので0
        cout << 0 << endl;
      } else {
        // こことone_indexだけが立つ
        int64_t v =
            (MODpow(2, N - 1 - i, 2) + MODpow(2, N - 1 - one_index, 2)) % 2;
        cout << f(v) + 1 << endl;
      }
    }

    return 0;
  }

  int64_t case_b_minus_1 = 0, case_b_plus_1 = 0;
  for (int64_t i = 0; i < N; i++) {
    if (X[i] == '1') {
      (case_b_minus_1 += MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
      (case_b_plus_1 += MODpow(2, N - 1 - i, b + 1)) %= (b + 1);
    }
  }

  for (int64_t i = 0; i < N; i++) {
    // iを反転
    if (X[i] == '0') {
      // 1になる
      int64_t v = (case_b_plus_1 + MODpow(2, N - 1 - i, b + 1)) % (b + 1);
      cout << f(v) + 1 << endl;
    } else {
      // 0になる
      int64_t v = case_b_minus_1;
      // 自分の影響を除く
      (v += b - 1 - MODpow(2, N - 1 - i, b - 1)) %= (b - 1);

      cout << f(v) + 1 << endl;
    }
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
ll inf = 1e9;

ll func(ll v) {
  ll cnt = 0;
  ll ans = 0;
  while (v != 0) {
    cnt = 0;
    for (ll i = 0; i < 64; i++)
      if ((v >> i) & 1)
        cnt++;
    v %= cnt;
    ans++;
  }
  return ans;
}

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

  ll vp = 0, vu = 0;
  vector<ll> bp(n, 1), bu(n, 1);
  for (ll i = n - 1; i >= 1; i--) {
    bp[i - 1] = (bp[i] * 2) % (cnt + 1);
    bu[i - 1] = (bu[i] * 2) % (cnt - 1);
  }
  for (ll i = n - 1; i >= 0; i--) {
    //  cout<<i<<' '<<"OK"<<endl;
    if (s[i] == '1') {
      vp += bp[i];
      vp %= cnt + 1;
      if (cnt - 1 != 0) {
        vu += bu[i];
        vu %= cnt - 1;
      }
    }
  }

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      ll c = cnt - 1;
      if (c == 0) {
        cout << c << endl;
        continue;
      }
      ll v = (vu - bu[i] + c) % c;
      cout << func(v) + 1 << endl;
    } else {
      ll c = cnt + 1;
      ll v = (vp + bp[i]) % c;
      cout << func(v) + 1 << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
ll inf = 1e9;

ll func(ll v) {
  ll cnt = 0;
  ll ans = 0;
  while (v != 0) {
    cnt = 0;
    for (ll i = 0; i < 64; i++)
      if ((v >> i) & 1)
        cnt++;
    v %= cnt;
    ans++;
  }
  return ans;
}

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

  ll vp = 0, vu = 0;
  vector<ll> bp(n, 1), bu(n, 1);
  for (ll i = n - 1; i >= 1; i--) {
    bp[i - 1] = (bp[i] * 2) % (cnt + 1);
    if (cnt - 1 != 0)
      bu[i - 1] = (bu[i] * 2) % (cnt - 1);
  }
  for (ll i = n - 1; i >= 0; i--) {
    //  cout<<i<<' '<<"OK"<<endl;
    if (s[i] == '1') {
      vp += bp[i];
      vp %= cnt + 1;
      if (cnt - 1 != 0) {
        vu += bu[i];
        vu %= cnt - 1;
      }
    }
  }

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      ll c = cnt - 1;
      if (c == 0) {
        cout << c << endl;
        continue;
      }
      ll v = (vu - bu[i] + c) % c;
      cout << func(v) + 1 << endl;
    } else {
      ll c = cnt + 1;
      ll v = (vp + bp[i]) % c;
      cout << func(v) + 1 << endl;
    }
  }
  return 0;
}