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

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

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10, mod = 1e9 + 7;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int f[N];
  f[0] = 0;
  for (int i = 1; i < N; i++) {
    f[i] = f[i % __builtin_popcount(i)] + 1;
  }
  string x;
  cin >> x;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (x[i] == '1');
  int pw1[n], pw2[n];
  int val1 = 0, val2 = 0;
  for (int i = n - 1, cur1 = 1, cur2 = 1; i >= 0;
       i--, cur1 = (cur1 * 2) % (cnt - 1), cur2 = (cur2 * 2) % (cnt + 1)) {
    pw1[n - 1 - i] = cur1;
    pw2[n - 1 - i] = cur2;
    if (x[i] == '1') {
      val1 = (val1 + cur1) % (cnt - 1);
      val2 = (val2 + cur2) % (cnt + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    int add = -1, newVal = -1;
    if (x[i] == '0') {
      add = pw2[n - 1 - i];
      newVal = val2;
      newVal = (newVal + add) % (cnt + 1);
    } else {
      add = (cnt - 1) - pw1[n - 1 - i];
      newVal = val1;
      newVal = (newVal + add) % (cnt - 1);
    }
    cout << f[newVal] + 1 << "\n";
  }
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10, mod = 1e9 + 7;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int f[N];
  f[0] = 0;
  for (int i = 1; i < N; i++) {
    f[i] = f[i % __builtin_popcount(i)] + 1;
  }
  string x;
  cin >> x;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (x[i] == '1');
  if (cnt == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << "\n";
    return 0;
  }
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (x[i] == '1')
        cout << 0 << "\n";
      else if (i == n - 1 || x[n - 1] == '1')
        cout << 2 << "\n";
      else
        cout << 1 << "\n";
    }
    return 0;
  }
  int pw1[n], pw2[n];
  int val1 = 0, val2 = 0;
  for (int i = n - 1, cur1 = 1, cur2 = 1; i >= 0;
       i--, cur1 = (cur1 * 2) % (cnt - 1), cur2 = (cur2 * 2) % (cnt + 1)) {
    pw1[n - 1 - i] = cur1;
    pw2[n - 1 - i] = cur2;
    if (x[i] == '1') {
      val1 = (val1 + cur1) % (cnt - 1);
      val2 = (val2 + cur2) % (cnt + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    int add = -1, newVal = -1;
    if (x[i] == '0') {
      add = pw2[n - 1 - i];
      newVal = val2;
      newVal = (newVal + add) % (cnt + 1);
    } else {
      add = (cnt - 1) - pw1[n - 1 - i];
      newVal = val1;
      newVal = (newVal + add) % (cnt - 1);
    }
    cout << f[newVal] + 1 << "\n";
  }
  return 0;
}
Problem p02609 (C++)

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

long long modpow(long long n, long long k, long long MOD) {
  long long ans = 1;
  n %= MOD;
  while (k > 0) {
    if (k & 1)
      ans = (ans * n) % MOD;
    k >>= 1;
    n = (n * n) % MOD;
  }
  return ans;
}

int rem(int n, string s, int pp) {
  if (pp <= 0)
    return -1;

  reverse(s.begin(), s.end());

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ans = (ans + modpow(2, i, pp)) % pp;
  }
  return ans;
}

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

  int n;
  string s;
  cin >> n >> s;
  int pp = 0;
  for (char c : s) {
    pp += (c == '1');
  }

  int loss = rem(n, s, pp - 1);
  int gain = rem(n, s, pp + 1);

  for (int i = 0, j = n - 1; i < n; i++, j--) {
    int ans = 1, cur = 0;
    if (s[i] == '0') {
      // gain a bit
      cur = gain;
      cur = (cur + modpow(2, j, pp + 1)) % (pp + 1);
    } else {
      // lose a bit
      cur = loss;
      cur = (cur - modpow(2, j, pp - 1)) % (pp - 1);
      if (cur < 0)
        cur += (pp - 1);
    }

    while (cur > 0) {
      ans++;
      cur %= __builtin_popcount(cur);
    }
    cout << ans << '\n';
  }

  return 0;
}


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

long long modpow(long long n, long long k, long long MOD) {
  long long ans = 1;
  n %= MOD;
  while (k > 0) {
    if (k & 1)
      ans = (ans * n) % MOD;
    k >>= 1;
    n = (n * n) % MOD;
  }
  return ans;
}

int rem(int n, string s, int pp) {
  if (pp <= 0)
    return -1;

  reverse(s.begin(), s.end());

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ans = (ans + modpow(2, i, pp)) % pp;
  }
  return ans;
}

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

  int n;
  string s;
  cin >> n >> s;
  int pp = 0;
  for (char c : s) {
    pp += (c == '1');
  }

  int loss = rem(n, s, pp - 1);
  int gain = rem(n, s, pp + 1);

  for (int i = 0, j = n - 1; i < n; i++, j--) {
    int ans = 1, cur = 0;
    if (s[i] == '0') {
      // gain a bit
      cur = gain;
      cur = (cur + modpow(2, j, pp + 1)) % (pp + 1);
    } else {
      // lose a bit
      if (pp == 1) {
        ans = 0;
        cur = 0;
      } else {
        cur = loss;
        cur = (cur - modpow(2, j, pp - 1)) % (pp - 1);
        if (cur < 0)
          cur += (pp - 1);
      }
    }

    while (cur > 0) {
      ans++;
      cur %= __builtin_popcount(cur);
    }
    cout << ans << '\n';
  }

  return 0;
}
Problem p02609 (C++)

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

int main() {
  int N;
  std::string X;
  std::cin >> N >> X;
  const int popcount = std::count(X.begin(), X.end(), '1');
  std::map<int, int> x_map;
  std::vector<std::map<int, int>> digit_map(N);
  for (int mod = std::max(1, popcount - 1); mod <= popcount + 1; ++mod) {
    int remainder = 0;
    for (const char digit : X) {
      remainder = (remainder * 2 + digit - '0') % mod;
    }
    x_map[mod] = remainder;
    int digit = 1;
    for (int i = 0; i < N; ++i) {
      digit_map[N - i - 1][mod] = digit;
      digit = digit * 2 % mod;
    }
  }
  for (int i = 0; i < N; ++i) {
    const int mod = popcount + (X[i] == '0' ? 1 : -1);
    int f = 0;
    if (popcount > 0) {
      f = 1;
      int n = x_map[mod];
      if (X[i] == '0') {
        n = (n + digit_map[i][mod]) % mod;
      } else {
        n = (n - digit_map[i][mod] + mod) % mod;
      }
      while (n > 0) {
        ++f;
        n = n % __builtin_popcount(n);
      }
    }
    std::cout << f << std::endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
  int N;
  std::string X;
  std::cin >> N >> X;
  const int popcount = std::count(X.begin(), X.end(), '1');
  std::map<int, int> x_map;
  std::vector<std::map<int, int>> digit_map(N);
  for (int mod = std::max(1, popcount - 1); mod <= popcount + 1; ++mod) {
    int remainder = 0;
    for (const char digit : X) {
      remainder = (remainder * 2 + digit - '0') % mod;
    }
    x_map[mod] = remainder;
    int digit = 1;
    for (int i = 0; i < N; ++i) {
      digit_map[N - i - 1][mod] = digit;
      digit = digit * 2 % mod;
    }
  }
  for (int i = 0; i < N; ++i) {
    const int mod = popcount + (X[i] == '0' ? 1 : -1);
    int f = 0;
    if (mod > 0) {
      f = 1;
      int n = x_map[mod];
      if (X[i] == '0') {
        n = (n + digit_map[i][mod]) % mod;
      } else {
        n = (n - digit_map[i][mod] + mod) % mod;
      }
      while (n > 0) {
        ++f;
        n = n % __builtin_popcount(n);
      }
    }
    std::cout << f << std::endl;
  }
  return 0;
}
Problem p02609 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N;
string X;
ll g[400001];
ll f[400001];

ll calc(ll m) {
  if (m == 0)
    return 0;
  ll ret = 0;
  for (auto &x : X) {
    ret *= 2;
    ret += x - '0';
    ret %= m;
  }
  return ret;
}

int main() {
  cin >> N;
  cin >> X;
  vector<ll> ans;
  for (int i = 1; i <= 400000; i++) {
    bitset<32> bt(i);
    g[i] = i % bt.count();
  }
  for (int i = 1; i <= 400000; i++) {
    if (g[i] == 0) {
      f[i] = 1;
      continue;
    }
    f[i] = f[g[i]] + 1;
  }
  bitset<200000> btX(X);
  ll x_count = btX.count();
  ll mod_p = x_count + 1;
  ll mod_m = x_count - 1;
  ll X_mod_p = calc(mod_p);
  ll X_mod_m = calc(mod_m);
  ll pp, mp;
  pp = mp = 1;
  for (int i = N - 1; i >= 0; i--) {
    pp %= mod_p;
    mp %= mod_m;
    if (X[i] == '0') {
      ll t = pp + X_mod_p;
      t %= mod_p;
      ans.push_back(f[t] + 1);
    } else {
      if (mod_m == 0) {
        ans.push_back(0);
        continue;
      }
      ll t = X_mod_m - mp;
      t += mod_m;
      t %= mod_m;
      ans.push_back(f[t] + 1);
    }
    pp *= 2;
    mp *= 2;
  }
  for (int i = N - 1; i >= 0; i--)
    cout << ans[i] << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N;
string X;
ll g[400001];
ll f[400001];

ll calc(ll m) {
  if (m == 0)
    return 0;
  ll ret = 0;
  for (auto &x : X) {
    ret *= 2;
    ret += x - '0';
    ret %= m;
  }
  return ret;
}

int main() {
  cin >> N;
  cin >> X;
  vector<ll> ans;
  for (int i = 1; i <= 400000; i++) {
    bitset<32> bt(i);
    g[i] = i % bt.count();
  }
  for (int i = 1; i <= 400000; i++) {
    if (g[i] == 0) {
      f[i] = 1;
      continue;
    }
    f[i] = f[g[i]] + 1;
  }
  bitset<200000> btX(X);
  ll x_count = btX.count();
  ll mod_p = x_count + 1;
  ll mod_m = x_count - 1;
  ll X_mod_p = calc(mod_p);
  ll X_mod_m = calc(mod_m);
  ll pp, mp;
  pp = mp = 1;
  for (int i = N - 1; i >= 0; i--) {
    pp %= mod_p;
    if (mod_m != 0)
      mp %= mod_m;
    if (X[i] == '0') {
      ll t = pp + X_mod_p;
      t %= mod_p;
      ans.push_back(f[t] + 1);
    } else {
      if (mod_m == 0) {
        ans.push_back(0);
        continue;
      }
      ll t = X_mod_m - mp;
      t += mod_m;
      t %= mod_m;
      ans.push_back(f[t] + 1);
    }
    pp *= 2;
    mp *= 2;
  }
  for (int i = N - 1; i >= 0; i--)
    cout << ans[i] << endl;
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int f[N], ans[N];
char s[N];
int main() {
  int n, sum = 0, s1, s2, sum1 = 0, sum2 = 0, d1 = 1, d2 = 1;
  scanf("%d%s", &n, s);
  for (int i = 0; s[i]; i++)
    sum += (s[i] ^= 0x30);
  s1 = sum - 1, s2 = sum + 1;
  f[0] = 1;
  for (int i = 1; i <= s2; i++)
    f[i] = 1 + f[i % __builtin_popcount(i)];
  if (s1)
    for (int i = n - 1; ~i; i--)
      (sum1 += s[i] * d1) %= s1, (sum2 += s[i] * d2) %= s2, d1 = d1 * 2 % s1,
                                                            d2 = d2 * 2 % s2;
  else
    for (int i = n - 1; ~i; i--)
      (sum2 += s[i] * d2) %= s2, d2 = d2 * 2 % s2;
  d1 = d2 = 1;
  for (int i = n - 1; ~i; i--) {
    if (s[i])
      ans[i] = s1 ? f[(sum1 - d1 + s1) % s1] : 0;
    else
      ans[i] = f[(sum2 + d2) % s2];
    d2 = d2 * 2 % s2, d1 = d1 * 2 % s1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int f[N], ans[N];
char s[N];
int main() {
  int n, sum = 0, s1, s2, sum1 = 0, sum2 = 0, d1 = 1, d2 = 1;
  scanf("%d%s", &n, s);
  for (int i = 0; s[i]; i++)
    sum += (s[i] ^= 0x30);
  s1 = sum - 1, s2 = sum + 1;
  f[0] = 1;
  for (int i = 1; i <= s2; i++)
    f[i] = 1 + f[i % __builtin_popcount(i)];
  if (s1)
    for (int i = n - 1; ~i; i--)
      (sum1 += s[i] * d1) %= s1, (sum2 += s[i] * d2) %= s2, d1 = d1 * 2 % s1,
                                                            d2 = d2 * 2 % s2;
  else
    for (int i = n - 1; ~i; i--)
      (sum2 += s[i] * d2) %= s2, d2 = d2 * 2 % s2;
  d1 = d2 = 1;
  for (int i = n - 1; ~i; i--) {
    if (s[i])
      ans[i] = s1 ? f[(sum1 - d1 + s1) % s1] : 0;
    else
      ans[i] = f[(sum2 + d2) % s2];
    d2 = d2 * 2 % s2;
    if (s1)
      d1 = d1 * 2 % s1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p02609 (C++)

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

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;
map<int, int> m;
string s;
int n;
int culc(int num) {
  if (num == 0)
    return 0;
  if (num == 1)
    return 1;
  if (m.count(num) == 1)
    return m[num];
  int c = __builtin_popcount(num);
  return m[num] = culc(num % c) + 1;
}
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> n;
  int count = 0;
  cin >> s;
  vector<vector<int>> cu(2, vector<int>(n + 2));
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      count++;
  }
  cu[0][0] = 1;
  cu[1][0] = 1;
  for (int i = 0; i < n; ++i) {
    cu[0][i + 1] = cu[0][i] * 2 % (count + 1);
    if (count > 1)
      cu[1][i + 1] = cu[1][i] * 2 % (count - 1);
  }
  vector<int> sum(2);
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      (sum[0] += cu[0][n - i - 1]) %= (count + 1);
      (sum[1] += cu[1][n - i - 1]) %= (count - 1);
    }
  }
  vector<int> ret(n);
  for (int i = 0; i < n; ++i) {
    int tmp = 0;
    if (s[i] == '0')
      tmp = (sum[0] + cu[0][n - i - 1]) % (count + 1);
    else {
      if (count == 1)
        continue;
      tmp =
          ((sum[1] - cu[1][n - i - 1]) % (count - 1) + count - 1) % (count - 1);
    }
    ret[i] = culc(tmp) + 1;
  }
  for (int i = 0; i < n; ++i)
    cout << ret[i] << endl;
  return 0;
}

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

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;
map<int, int> m;
string s;
int n;
int culc(int num) {
  if (num == 0)
    return 0;
  if (num == 1)
    return 1;
  if (m.count(num) == 1)
    return m[num];
  int c = __builtin_popcount(num);
  return m[num] = culc(num % c) + 1;
}
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> n;
  int count = 0;
  cin >> s;
  vector<vector<int>> cu(2, vector<int>(n + 2));
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      count++;
  }
  cu[0][0] = 1;
  cu[1][0] = 1;
  for (int i = 0; i < n; ++i) {
    cu[0][i + 1] = cu[0][i] * 2 % (count + 1);
    if (count > 1)
      cu[1][i + 1] = cu[1][i] * 2 % (count - 1);
  }
  vector<int> sum(2);
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      (sum[0] += cu[0][n - i - 1]) %= (count + 1);
      if (count > 1)
        (sum[1] += cu[1][n - i - 1]) %= (count - 1);
    }
  }
  vector<int> ret(n);
  for (int i = 0; i < n; ++i) {
    int tmp = 0;
    if (s[i] == '0')
      tmp = (sum[0] + cu[0][n - i - 1]) % (count + 1);
    else {
      if (count == 1)
        continue;
      tmp =
          ((sum[1] - cu[1][n - i - 1]) % (count - 1) + count - 1) % (count - 1);
    }
    ret[i] = culc(tmp) + 1;
  }
  for (int i = 0; i < n; ++i)
    cout << ret[i] << endl;
  return 0;
}
Problem p02609 (C++)

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

int deal(int x) {
  int res = 0;
  while (x) {
    res++;
    x = x % __builtin_popcount(x);
  }
  return res;
}

int n, p1 = -1, p2 = 1, x1, x2;
char s[200005];
int pw1[200005], pw2[200005];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> s + 1;
  for (int i = 1; i <= n; i++) {
    p1 += s[i] == '1';
    p2 += s[i] == '1';
  }
  pw1[0] = pw2[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (p1)
      x1 = (x1 << 1 | (s[i] == '1')) % p1;
    x2 = (x2 << 1 | (s[i] == '1')) % p2;
    if (p1)
      pw1[i] = (pw1[i - 1] << 1) % p1;
    pw2[i] = (pw2[i - 1] << 1) % p2;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      cout << deal((x1 - pw1[n - i] + p1) % p1) + 1 << endl;
    } else {
      cout << deal((x2 + pw2[n - i] + p2) % p2) + 1 << endl;
    }
  }

  return 0;
}

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

int deal(int x) {
  int res = 0;
  while (x) {
    res++;
    x = x % __builtin_popcount(x);
  }
  return res;
}

int n, p1 = -1, p2 = 1, x1, x2;
char s[200005];
int pw1[200005], pw2[200005];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> s + 1;
  for (int i = 1; i <= n; i++) {
    p1 += s[i] == '1';
    p2 += s[i] == '1';
  }
  pw1[0] = pw2[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (p1)
      x1 = (x1 << 1 | (s[i] == '1')) % p1;
    x2 = (x2 << 1 | (s[i] == '1')) % p2;
    if (p1)
      pw1[i] = (pw1[i - 1] << 1) % p1;
    pw2[i] = (pw2[i - 1] << 1) % p2;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (p1)
        cout << deal((x1 - pw1[n - i] + p1) % p1) + 1 << endl;
      else
        cout << 0 << endl;
    } else {
      cout << deal((x2 + pw2[n - i] + p2) % p2) + 1 << endl;
    }
  }

  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long ll;

constexpr long long INFLL = 1e18;

using namespace std;

int f(int temp) {
  if (temp == 0) {
    return 0;
  } else {
    return f(temp % __builtin_popcount(temp)) + 1;
  }
}

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  int pc = 0;
  for (int i = 0; i < n; i++) {
    x.at(i) = s.at(i) - '0';
    if (x.at(i)) {
      pc++;
    }
  }

  vector<int> ans(n);

  for (int i = 0; i < 2; i++) {
    int pc2 = pc;
    if (i == 0) {
      pc2++;
    } else {
      pc2--;
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum = (sum * 2) % pc2;
      sum += x.at(j);
    }
    int temp = 1;
    int temp2;
    for (int j = n - 1; j >= 0; j--) {
      if (i == 0) {
        if (x.at(j) == 0) {
          temp2 = (sum + temp) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      } else {
        if (x.at(j) == 1) {
          temp2 = (sum - temp + pc2) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      }
      temp = (temp * 2) % pc2;
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long ll;

constexpr long long INFLL = 1e18;

using namespace std;

int f(int temp) {
  if (temp == 0) {
    return 0;
  } else {
    return f(temp % __builtin_popcount(temp)) + 1;
  }
}

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  int pc = 0;
  for (int i = 0; i < n; i++) {
    x.at(i) = s.at(i) - '0';
    if (x.at(i)) {
      pc++;
    }
  }

  vector<int> ans(n);

  for (int i = 0; i < 2; i++) {
    int pc2 = pc;
    if (i == 0) {
      pc2++;
    } else {
      pc2--;
    }
    if (pc2 <= 0) {
      continue;
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum = (sum * 2) % pc2;
      sum += x.at(j);
    }
    int temp = 1;
    int temp2;
    for (int j = n - 1; j >= 0; j--) {
      if (i == 0) {
        if (x.at(j) == 0) {
          temp2 = (sum + temp) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      } else {
        if (x.at(j) == 1) {
          temp2 = (sum - temp + pc2) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      }
      temp = (temp * 2) % pc2;
    }
  }

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

  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
int dfs(int n) {
  if (n == 0) {
    return 0;
  }
  int now = n;
  int co = 0;
  while (now > 0) {
    if (now % 2 == 1) {
      co++;
    }
    now /= 2;
  }
  return dfs(n % co) + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int tot = 0;
  rep(i, n) {
    if (s[i] == '1') {
      tot++;
    }
  }

  vector<int> com(n), cop(n);
  int p = 1;
  rep(i, n) {
    if (tot > 1) {
      com[i] = p % (tot - 1);
      p = p * 2 % (tot - 1);
    }
  }
  p = 1;
  rep(i, n) {

    cop[i] = p % (tot + 1);
    p = p * 2 % (tot + 1);
  }
  int tm = 0;
  int tp = 0;
  rep(i, n) {
    if (s[n - 1 - i] == '1') {
      if (tot > 1) {
        tm = (tm + com[i]) % (tot - 1);
      }
      tp = (tp + cop[i]) % (tot + 1);
    }
  }
  rep(i, n) {
    int nowm = tm;
    int nowp = tp;
    if (s[i] == '1') {
      nowm = (nowm + tot - 1 - com[n - 1 - i]) % (tot - 1);
      cout << dfs(nowm) + 1 << endl;
    } else {
      nowp = (nowp + cop[n - 1 - i]) % (tot + 1);
      cout << dfs(nowp) + 1 << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
int dfs(int n) {
  if (n == 0) {
    return 0;
  }
  int now = n;
  int co = 0;
  while (now > 0) {
    if (now % 2 == 1) {
      co++;
    }
    now /= 2;
  }
  return dfs(n % co) + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int tot = 0;
  rep(i, n) {
    if (s[i] == '1') {
      tot++;
    }
  }

  vector<int> com(n), cop(n);
  int p = 1;
  rep(i, n) {
    if (tot > 1) {
      com[i] = p % (tot - 1);
      p = p * 2 % (tot - 1);
    }
  }
  p = 1;
  rep(i, n) {

    cop[i] = p % (tot + 1);
    p = p * 2 % (tot + 1);
  }
  int tm = 0;
  int tp = 0;
  rep(i, n) {
    if (s[n - 1 - i] == '1') {
      if (tot > 1) {
        tm = (tm + com[i]) % (tot - 1);
      }
      tp = (tp + cop[i]) % (tot + 1);
    }
  }
  rep(i, n) {
    int nowm = tm;
    int nowp = tp;
    if (s[i] == '1') {
      if (tot <= 1) {
        cout << 0 << endl;
      } else {
        nowm = (nowm + tot - 1 - com[n - 1 - i]) % (tot - 1);
        cout << dfs(nowm) + 1 << endl;
      }
    } else {
      nowp = (nowp + cop[n - 1 - i]) % (tot + 1);
      cout << dfs(nowp) + 1 << endl;
    }
  }
}
Problem p02609 (C++)

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

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

int main() {
  int N;
  cin >> N;
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  vector<int> S(N);
  for (int i = 0; i < N; i++)
    S[i] = (str[i] - '0');

  int64_t cnt = accumulate(S.begin(), S.end(), 0);
  int64_t V = (cnt == 0 ? 1 : (cnt + 1) * (cnt - 1));

  int64_t R = 0, B = 1;
  for (int i = 0; i < N; i++) {
    if (S[i])
      R = (R + B) % V;
    B = B * 2 % V;
  }

  auto calc = [&](auto &&calc, int v) -> int {
    if (v == 0) {
      return 0;
    } else {
      return calc(calc, v % pop_count(v)) + 1;
    }
  };

  B = 1;
  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int64_t v = cnt + (S[i] ? -1 : 1);
    if (v == 0)
      continue;
    int64_t r = (R + B % v * (S[i] % v ? v - 1 : 1)) % v;
    ans[N - 1 - i] = 1 + calc(calc, r);
    B = B * 2 % V;
  }

  for (int a : ans)
    cout << a << endl;
  return 0;
}


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

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

int main() {
  int N;
  cin >> N;
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  vector<int> S(N);
  for (int i = 0; i < N; i++)
    S[i] = (str[i] - '0');

  int64_t cnt = accumulate(S.begin(), S.end(), 0);
  int64_t V = 1;
  for (auto a : {cnt + 1, cnt - 1})
    if (a > 0)
      V *= a;

  int64_t R = 0, B = 1;
  for (int i = 0; i < N; i++) {
    if (S[i])
      R = (R + B) % V;
    B = B * 2 % V;
  }

  auto calc = [&](auto &&calc, int v) -> int {
    if (v == 0) {
      return 0;
    } else {
      return calc(calc, v % pop_count(v)) + 1;
    }
  };

  B = 1;
  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int64_t v = cnt + (S[i] ? -1 : 1);
    if (v == 0)
      continue;
    int64_t r = (R + B % v * (S[i] % v ? v - 1 : 1)) % v;
    ans[N - 1 - i] = 1 + calc(calc, r);
    B = B * 2 % V;
  }

  for (int a : ans)
    cout << a << endl;
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
int N;
string s;
int MODplus1[200005];
int MODminus1[200005];

int f(int x) {
  if (x == 0)
    return 0;
  int cnt = 0;
  rep(i, 20) {
    if ((1 << i) & x)
      cnt++;
  }
  return f(x % cnt) + 1;
}

int calc_mod(int x, int _mod[200005]) {
  _mod[0] = 1;
  rep(i, N) {
    if (i == 0)
      continue;
    _mod[i] = 2 * _mod[i - 1] % x;
  }
  int sum = 0;
  rep(i, N) {
    if (s[i] == '1')
      sum += _mod[N - 1 - i];
    sum %= x;
  }
  return sum;
}

int main() {
  INCANT;
  cin >> N >> s;
  int cnt = 0;
  rep(i, N) if (s[i] == '1') cnt++;
  int sum1 = calc_mod(cnt + 1, MODplus1);
  int sum2 = calc_mod(cnt - 1, MODminus1);
  rep(i, N) {
    int sum;
    if (s[i] == '1') { // 1→0
      if (cnt - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      sum = sum2;
      sum -= MODminus1[N - 1 - i];
      sum += (cnt - 1);
      sum %= (cnt - 1);
    } else { // 0→1
      sum = sum1;
      sum += MODplus1[N - 1 - i];
      sum %= (cnt + 1);
    }
    cout << f(sum) + 1 << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
int N;
string s;
int MODplus1[200005];
int MODminus1[200005];

int f(int x) {
  if (x == 0)
    return 0;
  int cnt = 0;
  rep(i, 20) {
    if ((1 << i) & x)
      cnt++;
  }
  return f(x % cnt) + 1;
}

int calc_mod(int x, int _mod[200005]) {
  if (x == 0)
    return 0;
  _mod[0] = 1;
  rep(i, N) {
    if (i == 0)
      continue;
    _mod[i] = 2 * _mod[i - 1] % x;
  }
  int sum = 0;
  rep(i, N) {
    if (s[i] == '1')
      sum += _mod[N - 1 - i];
    sum %= x;
  }
  return sum;
}

int main() {
  INCANT;
  cin >> N >> s;
  int cnt = 0;
  rep(i, N) if (s[i] == '1') cnt++;
  int sum1 = calc_mod(cnt + 1, MODplus1);
  int sum2 = calc_mod(cnt - 1, MODminus1);
  rep(i, N) {
    int sum;
    if (s[i] == '1') { // 1→0
      if (cnt - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      sum = sum2;
      sum -= MODminus1[N - 1 - i];
      sum += (cnt - 1);
      sum %= (cnt - 1);
    } else { // 0→1
      sum = sum1;
      sum += MODplus1[N - 1 - i];
      sum %= (cnt + 1);
    }
    cout << f(sum) + 1 << endl;
  }
  return 0;
}
Problem p02609 (C++)

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

using namespace std;

// ゼロになる時が例外

int main() {
  int n;
  cin >> n;
  vector<int> A;
  string S;
  cin >> S;
  for (const auto &s : S)
    A.push_back(s - '0');
  int cnt = 0;
  for (const auto &a : A) {
    if (a == 1)
      cnt++;
  }

  vector<int> D(n);
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1)
        cout << 0 << endl;
      else if (i == n - 1)
        cout << 2 << endl;
      else
        cout << 1 << endl;
    }
    return 0;
  }

  vector<vector<int>> B(3, vector<int>(n));
  for (int j = -1; j < 2; j++) {
    int m = 1;
    for (int i = 0; i < n; i++) {
      B[j + 1][n - i - 1] = m;
      m *= 2;
      m %= (cnt + j);
    }
  }

  vector<int> C(3);
  for (int j = 0; j <= 2; j++) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1) {
        C[j] += B[j][i];
        C[j] %= (cnt + j - 1);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (A[i] == 1)
      D[i] = (C[0] - B[0][i] + cnt - 1) % (cnt - 1);
    else
      D[i] = (C[2] + B[2][i]) % (cnt + 1);
  }

  map<int, int> ans;
  auto f = [&](int d, auto self) -> int {
    if (ans.count(d))
      return ans[d];
    if (d == 0)
      return 0;
    return ans[d] = self(d % __builtin_popcount(d), self) + 1;
  };

  for (int i = 0; i < n; i++) {
    f(D[i], f);
  }

  for (int i = 0; i < n; i++)
    cout << ans[D[i]] + 1 << endl;
}

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

using namespace std;

// ゼロになる時が例外

int main() {
  int n;
  cin >> n;
  vector<int> A;
  string S;
  cin >> S;
  for (const auto &s : S)
    A.push_back(s - '0');
  int cnt = 0;
  for (const auto &a : A) {
    if (a == 1)
      cnt++;
  }

  vector<int> D(n);
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1)
        cout << 0 << endl;
      else if (i == n - 1)
        cout << 2 << endl;
      else
        cout << 1 << endl;
    }
    return 0;
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }

  vector<vector<int>> B(3, vector<int>(n));
  for (int j = -1; j < 2; j++) {
    int m = 1;
    for (int i = 0; i < n; i++) {
      B[j + 1][n - i - 1] = m;
      m *= 2;
      m %= (cnt + j);
    }
  }

  vector<int> C(3);
  for (int j = 0; j <= 2; j++) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1) {
        C[j] += B[j][i];
        C[j] %= (cnt + j - 1);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (A[i] == 1)
      D[i] = (C[0] - B[0][i] + cnt - 1) % (cnt - 1);
    else
      D[i] = (C[2] + B[2][i]) % (cnt + 1);
  }

  map<int, int> ans;
  auto f = [&](int d, auto self) -> int {
    if (ans.count(d))
      return ans[d];
    if (d == 0)
      return 0;
    return ans[d] = self(d % __builtin_popcount(d), self) + 1;
  };

  for (int i = 0; i < n; i++) {
    f(D[i], f);
  }

  for (int i = 0; i < n; i++)
    cout << ans[D[i]] + 1 << endl;
}
Problem p02609 (C++)

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

#define pb push_back
#define sz size()
#define ll long long

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  string s;
  cin >> n;
  cin >> s;

  n = s.size();

  int i;
  int nuli = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      nuli++;
    }

  int n1 = nuli - 1;
  int n2 = nuli + 1;

  ll presm1 = 0;
  ll presm2 = 0;

  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      if (n1 > 0)
        presm1 += binpow(2, n - i - 1, n1);
      presm2 += binpow(2, n - i - 1, n2);
    }

  if (n1 > 0)
    presm1 %= n1;
  if (n2 > 0)
    presm2 %= n2;

  int powBuck[20001];
  memset(powBuck, -1, sizeof(powBuck));

  for (i = 0; i < n; i++) {

    int curr = 0;

    if (s[i] == '1') {

      if (nuli == 1) {
        cout << "0\n";
        continue;
      }
      if (n1 > 0) {
        curr = presm1;
        ll kolku = binpow(2, n - i - 1, n1);
        curr = (curr + n1 - kolku) % n1;
      }
    } else {
      curr = presm2;
      ll kolku = binpow(2, n - i - 1, n2);
      curr = (curr + kolku) % n2;
    }

    int br = 1;
    while (curr) {

      if (powBuck[curr] == -1)
        powBuck[curr] = __builtin_popcount(curr);

      curr = curr % powBuck[curr];
      br++;
    }
    cout << br << "\n";
  }

  return 0;
}

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

#define pb push_back
#define sz size()
#define ll long long

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  string s;
  cin >> n;
  cin >> s;

  n = s.size();

  int i;
  int nuli = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      nuli++;
    }

  int n1 = nuli - 1;
  int n2 = nuli + 1;

  ll presm1 = 0;
  ll presm2 = 0;

  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      if (n1 > 0)
        presm1 += binpow(2, n - i - 1, n1);
      presm2 += binpow(2, n - i - 1, n2);
    }

  if (n1 > 0)
    presm1 %= n1;
  if (n2 > 0)
    presm2 %= n2;

  int powBuck[200001];
  memset(powBuck, -1, sizeof(powBuck));

  for (i = 0; i < n; i++) {

    int curr = 0;

    if (s[i] == '1') {

      if (nuli == 1) {
        cout << "0\n";
        continue;
      }
      if (n1 > 0) {
        curr = presm1;
        ll kolku = binpow(2, n - i - 1, n1);
        curr = (curr + n1 - kolku) % n1;
      }
    } else {
      curr = presm2;
      ll kolku = binpow(2, n - i - 1, n2);
      curr = (curr + kolku) % n2;
    }

    int br = 1;
    while (curr) {

      if (powBuck[curr] == -1)
        powBuck[curr] = __builtin_popcount(curr);

      curr = curr % powBuck[curr];
      br++;
    }
    cout << br << "\n";
  }

  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 ll;
const int MAX = 200010;
int main() {
  int n;
  string x;
  cin >> n >> x;
  int pc = 0;
  rep(i, n) {
    if ((x[i] - '0'))
      pc++;
  }
  vector<int> base_0(n), base_1(n);

  base_0[n - 1] = 1;
  base_1[n - 1] = 1;
  int val_0 = 0;
  int val_1 = 0;
  for (int i = n - 1; i >= 0; i--) {
    val_0 += (x[i] - '0') * base_0[i];
    val_1 += (x[i] - '0') * base_1[i];
    val_0 %= (pc + 1);
    if (pc != 1)
      val_1 %= (pc - 1);
    if (i != 0) {
      base_0[i - 1] = base_0[i] * 2;
      base_0[i - 1] %= (pc + 1);
      base_1[i - 1] = base_1[i] * 2;
      base_1[i - 1] %= (pc - 1);
    }
  }
  // 埋め込み
  vector<int> f(MAX, 0);
  f[0] = 0;
  rep(i, 200002) { f[i + 1] = 1 + f[(i + 1) % __builtin_popcount(i + 1)]; }
  // 操作
  rep(i, n) {
    int num;
    if ((x[i] - '0') && pc == 1) {
      cout << 0 << endl;
      continue;
    } else if ((x[i] - '0'))
      num = (val_1 - base_1[i] + (pc - 1)) % (pc - 1);
    else
      num = (val_0 + base_0[i]) % (pc + 1);
    // printf("num:%d\n", num);
    cout << f[num] + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MAX = 200010;
int main() {
  int n;
  string x;
  cin >> n >> x;
  int pc = 0;
  rep(i, n) {
    if ((x[i] - '0'))
      pc++;
  }
  vector<int> base_0(n), base_1(n);

  base_0[n - 1] = 1;
  base_1[n - 1] = 1;
  int val_0 = 0;
  int val_1 = 0;
  for (int i = n - 1; i >= 0; i--) {
    val_0 += (x[i] - '0') * base_0[i];
    val_1 += (x[i] - '0') * base_1[i];
    val_0 %= (pc + 1);
    if (pc != 1)
      val_1 %= (pc - 1);
    if (i != 0) {
      base_0[i - 1] = base_0[i] * 2;
      base_0[i - 1] %= (pc + 1);
      base_1[i - 1] = base_1[i] * 2;
      if (pc != 1)
        base_1[i - 1] %= (pc - 1);
    }
  }
  // 埋め込み
  vector<int> f(MAX, 0);
  f[0] = 0;
  rep(i, 200002) { f[i + 1] = 1 + f[(i + 1) % __builtin_popcount(i + 1)]; }
  // 操作
  rep(i, n) {
    int num;
    if ((x[i] - '0') && pc == 1) {
      cout << 0 << endl;
      continue;
    } else if ((x[i] - '0'))
      num = (val_1 - base_1[i] + (pc - 1)) % (pc - 1);
    else
      num = (val_0 + base_0[i]) % (pc + 1);
    // printf("num:%d\n", num);
    cout << f[num] + 1 << endl;
  }
}
Problem p02609 (C++)

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

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

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

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

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

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

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

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

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

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

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

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

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

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

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

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

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

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

  return 0;
}
Problem p02609 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    N = int(readline())
    X = readline().rstrip().decode("utf-8")
    nx = int(X, 2)

    ans = [0] * N

    oc = X.count("1")

    if oc - 1 != 0:
        mx = nx % (oc - 1)
        f = True
    else:
        f = False

    px = nx % (oc + 1)

    for i in range(N):
        if X[i] == "0":
            a = (px + pow(2, N - i - 1, oc + 1)) % (oc + 1)
        elif f:
            a = (mx - pow(2, N - i - 1, oc - 1)) % (oc - 1)
        else:
            ans[i] = 0

        count = 1
        while a:
            a = a % bin(a).count("1")
            count += 1

        ans[i] = count

    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    N = int(readline())
    X = readline().rstrip().decode("utf-8")
    nx = int(X, 2)

    ans = [0] * N

    oc = X.count("1")

    if oc - 1 != 0:
        mx = nx % (oc - 1)
        f = True
    else:
        f = False

    px = nx % (oc + 1)

    for i in range(N):
        if X[i] == "0":
            a = (px + pow(2, N - i - 1, oc + 1)) % (oc + 1)
        elif f:
            a = (mx - pow(2, N - i - 1, oc - 1)) % (oc - 1)
        else:
            ans[i] = 0
            continue

        count = 1
        while a:
            a = a % bin(a).count("1")
            count += 1

        ans[i] = count

    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()

ll modPow(ll a, ll n, ll p) {
  if (n == 0)
    return 1; // 0乗にも対応する場合
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  ll t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main() {
  ll N, x = 0, s = 0, t = 0;
  string X;
  cin >> N >> X;
  vector<ll> f(N);
  rep(i, N) {
    if (i == 0)
      f.at(i) = 0;
    else {
      bitset<20> s(i);
      f.at(i) = f.at(i % s.count()) + 1;
    }
  }
  rep(i, N) {
    if (X.at(i) == '1')
      x++;
  }
  rep(i, N) {
    if (X.at(i) == '1') {
      t += modPow(2, N - 1 - i, x + 1);
      t %= x + 1;
      s += modPow(2, N - 1 - i, x - 1);
      s %= x - 1;
    }
  }
  rep(i, N) {
    if (x == 1 && X.at(i) == '1') {
      cout << 0 << endl;
    } else if (X.at(i) == '1') {
      ll S = s;
      S += x - 1;
      S -= modPow(2, N - 1 - i, x - 1);
      S %= x - 1;
      cout << f.at(S) + 1 << endl;
    } else {
      ll T = t;
      T += modPow(2, N - 1 - i, x + 1);
      T %= x + 1;
      cout << f.at(T) + 1 << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()

ll modPow(ll a, ll n, ll p) {
  if (n == 0)
    return 1; // 0乗にも対応する場合
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  ll t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main() {
  ll N, x = 0, s = 0, t = 0;
  string X;
  cin >> N >> X;
  vector<ll> f(N);
  rep(i, N) {
    if (i == 0)
      f.at(i) = 0;
    else {
      bitset<20> s(i);
      f.at(i) = f.at(i % s.count()) + 1;
    }
  }
  rep(i, N) {
    if (X.at(i) == '1')
      x++;
  }
  rep(i, N) {
    if (X.at(i) == '1') {
      t += modPow(2, N - 1 - i, x + 1);
      t %= x + 1;
      if (x > 1) {
        s += modPow(2, N - 1 - i, x - 1);
        s %= x - 1;
      }
    }
  }
  rep(i, N) {
    if (x == 1 && X.at(i) == '1') {
      cout << 0 << endl;
    } else if (X.at(i) == '1') {
      ll S = s;
      S += x - 1;
      S -= modPow(2, N - 1 - i, x - 1);
      S %= x - 1;
      cout << f.at(S) + 1 << endl;
    } else {
      ll T = t;
      T += modPow(2, N - 1 - i, x + 1);
      T %= x + 1;
      cout << f.at(T) + 1 << endl;
    }
  }
}
Problem p02609 (C++)

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

#define int long long
int pw(int n, int k, int MOD) {
  assert(k >= 0);
  int res = 1;
  while (k) {
    if (k & 1)
      (res *= n) %= MOD;
    (n *= n) %= MOD;
    k >>= 1;
  }
  return res;
}
int f(string s, int a) {
  int res = 0, t = 1;
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++) {
    (res += t * (s[i] == '1')) %= a;
    (t *= 2) %= a;
  }
  return res;
}
int p(int a) {
  int res = 0;
  while (a) {
    if (a & 1)
      res++;
    a >>= 1;
  }
  return res;
}
int F(int a) {
  int res = 0;
  while (a) {
    res++;
    a %= p(a);
  }
  return res;
}

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sum = 0;
  for (char p : s)
    sum += p == '1';
  int A = f(s, sum + 1);
  int B = f(s, sum - 1);
  for (int i = 0; i < n; i++) {
    if (sum == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    if (s[i] == '0') {
      int res = A;
      res += pw(2, n - i - 1, sum + 1);
      res %= sum + 1;
      cout << F(res) + 1 << endl;
    } else {
      int res = B;
      res -= pw(2, n - i - 1, sum - 1);
      res += sum - 1;
      res %= sum - 1;
      cout << F(res) + 1 << endl;
    }
  }
}


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

#define int long long
int pw(int n, int k, int MOD) {
  assert(k >= 0);
  int res = 1;
  while (k) {
    if (k & 1)
      (res *= n) %= MOD;
    (n *= n) %= MOD;
    k >>= 1;
  }
  return res;
}
int f(string s, int a) {
  if (a == 0)
    return -1;
  int res = 0, t = 1;
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++) {
    (res += t * (s[i] == '1')) %= a;
    (t *= 2) %= a;
  }
  return res;
}
int p(int a) {
  int res = 0;
  while (a) {
    if (a & 1)
      res++;
    a >>= 1;
  }
  return res;
}
int F(int a) {
  int res = 0;
  while (a) {
    res++;
    a %= p(a);
  }
  return res;
}

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sum = 0;
  for (char p : s)
    sum += p == '1';
  int A = f(s, sum + 1);
  int B = f(s, sum - 1);
  for (int i = 0; i < n; i++) {
    if (sum == 1 && s[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    if (s[i] == '0') {
      int res = A;
      res += pw(2, n - i - 1, sum + 1);
      res %= sum + 1;
      cout << F(res) + 1 << endl;
    } else {
      int res = B;
      res -= pw(2, n - i - 1, sum - 1);
      res += sum - 1;
      res %= sum - 1;
      cout << F(res) + 1 << endl;
    }
  }
}