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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int v[200010], r[200010];

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  string s;
  cin >> s;

  int pcnt = 0;
  for (int i = 0; i < n; i++)
    pcnt += s[i] == '1';

  for (int i = 1; i <= n; i++) {
    r[i] = 1 + r[i % __builtin_popcount(i)];
  }

  int p = pcnt + 1, val1 = 0;
  int q = pcnt - 1, val2 = 0;
  for (int i = 0; i < n; i++) {
    val1 = val1 * 2 + (s[i] == '1');
    val1 %= p;
    if (q > 0) {
      val2 = val2 * 2 + (s[i] == '1');
      val2 %= q;
    }
  }

  int mul1 = 1, mul2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int now = (val1 + mul1) % p;
      v[i] = now % p;
    } else if (q > 0) {
      int now = (val2 + q - mul2 % q) % q;
      v[i] = now % q;
    } else {
      v[i] = -1;
    }
    mul1 = mul1 * 2 % p;
    mul2 = mul2 * 2 % q;
  }

  for (int i = 0; i < n; i++) {
    cout << (v[i] == -1 ? 0 : 1 + r[v[i]]) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int v[200010], r[200010];

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  string s;
  cin >> s;

  int pcnt = 0;
  for (int i = 0; i < n; i++)
    pcnt += s[i] == '1';

  for (int i = 1; i <= n; i++) {
    r[i] = 1 + r[i % __builtin_popcount(i)];
  }

  int p = pcnt + 1, val1 = 0;
  int q = pcnt - 1, val2 = 0;
  for (int i = 0; i < n; i++) {
    val1 = val1 * 2 + (s[i] == '1');
    val1 %= p;
    if (q > 0) {
      val2 = val2 * 2 + (s[i] == '1');
      val2 %= q;
    }
  }

  int mul1 = 1, mul2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int now = (val1 + mul1) % p;
      v[i] = now % p;
    } else if (q > 0) {
      int now = (val2 + q - mul2 % q) % q;
      v[i] = now % q;
    } else {
      v[i] = -1;
    }
    mul1 = mul1 * 2 % p;
    if (q > 0)
      mul2 = mul2 * 2 % q;
  }

  for (int i = 0; i < n; i++) {
    cout << (v[i] == -1 ? 0 : 1 + r[v[i]]) << endl;
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
ll A = 0, B = 0, N, x;
ll Q(ll &i, string &S, vector<ll> &F, vector<ll> &G) {
  if (S.at(i) == '0')
    return (A + F.at(i)) % (x + 1);
  return ((B - G.at(i)) % (x - 1) + x - 1) % (x - 1);
}
ll f(ll m) {
  if (m == 0)
    return 0;
  ll i = 0, n = m;
  while (n > 0) {
    if (n % 2 == 1)
      i++;
    n /= 2;
  }
  return 1 + f(m % i);
}
signed main() {
  ll i, s = 1;
  string S, T;
  cin >> N >> S;
  T = S;
  sort(ALL(T));
  x = upper_bound(ALL(T), '0') - T.begin();
  x = N - x;
  vector<ll> F(N);
  REP(i, N) {
    F.at(N - 1 - i) = s;
    if (S.at(N - 1 - i) == '1')
      A += s;
    s *= 2;
    s %= x + 1;
    A %= x + 1;
  }
  s = 1;
  vector<ll> G(N);
  if (x <= 1) {
    REP(i, N) G.at(i) = -1;
  } else
    REP(i, N) {
      G.at(N - 1 - i) = s;
      if (S.at(N - 1 - i) == '1')
        B += s;
      s *= 2;
      s %= x - 1;
      B %= x - 1;
    }
  REP(i, N) {
    if (x == 1 && S.at(i) == '0')
      cout << 0 << endl;
    else {
      ll RES = Q(i, S, F, G);
      cout << 1 + f(RES) << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
ll A = 0, B = 0, N, x;
ll Q(ll &i, string &S, vector<ll> &F, vector<ll> &G) {
  if (S.at(i) == '0')
    return (A + F.at(i)) % (x + 1);
  return ((B - G.at(i)) % (x - 1) + x - 1) % (x - 1);
}
ll f(ll m) {
  if (m == 0)
    return 0;
  ll i = 0, n = m;
  while (n > 0) {
    if (n % 2 == 1)
      i++;
    n /= 2;
  }
  return 1 + f(m % i);
}
signed main() {
  ll i, s = 1;
  string S, T;
  cin >> N >> S;
  T = S;
  sort(ALL(T));
  x = upper_bound(ALL(T), '0') - T.begin();
  x = N - x;
  vector<ll> F(N);
  REP(i, N) {
    F.at(N - 1 - i) = s;
    if (S.at(N - 1 - i) == '1')
      A += s;
    s *= 2;
    s %= x + 1;
    A %= x + 1;
  }
  s = 1;
  vector<ll> G(N);
  if (x <= 1) {
    REP(i, N) G.at(i) = -1;
  } else
    REP(i, N) {
      G.at(N - 1 - i) = s;
      if (S.at(N - 1 - i) == '1')
        B += s;
      s *= 2;
      s %= x - 1;
      B %= x - 1;
    }
  REP(i, N) {
    if (x == 1 && S.at(i) == '1')
      cout << 0 << endl;
    else {
      ll RES = Q(i, S, F, G);
      cout << 1 + f(RES) << endl;
    }
  }
  return 0;
}
Problem p02609 (C++)

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

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

  int PopCount(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1')
      ++PopCount;
  }

  vector<int> pMod(N, 1), nMod(N, 1);
  for (int i = N - 2; i >= 0; --i) {
    pMod[i] = (pMod[i + 1] << 1) % (PopCount + 1);
    nMod[i] = (nMod[i + 1] << 1) % (PopCount - 1);
  }

  // Xp := X % (PopCount + 1)
  // Xn := X % (PopCount - 1)

  int Xp(0), Xn(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      (Xp += pMod[i]) %= (PopCount + 1);
      if (PopCount - 1 > 0)
        (Xn += nMod[i]) %= (PopCount - 1);
    }
  }

  for (int i = 0; i < N; ++i) {
    int ans = 1;
    if (X[i] == '1') {
      if (PopCount - 1 <= 0) {
        cout << 0 << endl;
        continue;
      }
      int res = (Xn - nMod[i] + (PopCount - 1)) % (PopCount - 1);
      // シミュレーション
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    } else {
      int res = (Xp + pMod[i] + (PopCount + 1)) % (PopCount + 1);
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

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

  int PopCount(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1')
      ++PopCount;
  }

  vector<int> pMod(N, 1), nMod(N, 1);
  for (int i = N - 2; i >= 0; --i) {
    pMod[i] = (pMod[i + 1] << 1) % (PopCount + 1);
    if (PopCount - 1 > 0)
      nMod[i] = (nMod[i + 1] << 1) % (PopCount - 1);
  }

  // Xp := X % (PopCount + 1)
  // Xn := X % (PopCount - 1)

  int Xp(0), Xn(0);
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      (Xp += pMod[i]) %= (PopCount + 1);
      if (PopCount - 1 > 0)
        (Xn += nMod[i]) %= (PopCount - 1);
    }
  }

  for (int i = 0; i < N; ++i) {
    int ans = 1;
    if (X[i] == '1') {
      if (PopCount - 1 <= 0) {
        cout << 0 << endl;
        continue;
      }
      int res = (Xn - nMod[i] + (PopCount - 1)) % (PopCount - 1);
      // シミュレーション
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    } else {
      int res = (Xp + pMod[i] + (PopCount + 1)) % (PopCount + 1);
      while (res != 0) {
        res = res % __builtin_popcount((unsigned int)res);
        ++ans;
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}

const int mod = 1e9 + 7;
const int INF = 1001001001;

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

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

  int pop = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      pop++;
    }
  }

  vector<int> ans(N);
  for (int r = 0; r < 2; r++) {
    int basep = pop;
    if (r == 0) {
      basep++;
    } else {
      basep--;
    }

    int base = 0;
    int s = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] == '1') {
        (base += s) %= basep;
      }
      (s *= 2) %= basep;
    }
    s = 1;
    int start = 0;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] - '0' == r) {
        if (r == 0) {
          (start = base + s) %= basep;
        } else if (r == 1) {
          (start = base - s + basep) %= basep;
        }
        ans[i] = f(start) + 1;
      }
      (s *= 2) %= basep;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}

const int mod = 1e9 + 7;
const int INF = 1001001001;

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

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

  int pop = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      pop++;
    }
  }

  vector<int> ans(N);
  for (int r = 0; r < 2; r++) {
    int basep = pop;
    if (r == 0) {
      basep++;
    } else {
      basep--;
    }

    if (basep <= 0) {
      continue;
    }

    int base = 0;
    int s = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] == '1') {
        (base += s) %= basep;
      }
      (s *= 2) %= basep;
    }
    s = 1;
    int start = 0;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] - '0' == r) {
        if (r == 0) {
          (start = base + s) %= basep;
        } else if (r == 1) {
          (start = base - s + basep) %= basep;
        }
        ans[i] = f(start) + 1;
      }
      (s *= 2) %= basep;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>

using namespace std;

long long mod_pow(long long x, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(20) << std::fixed;

  int N;
  string X;
  cin >> N >> X;

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (X[i] == '1');
  }

  long long A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (cnt - 1 >= 0) {
        A = (A + mod_pow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      }
      B = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    long long ans = 0, t = 0;
    if (X[i] == '1') {
      if (cnt != 1) {
        t = ((A - mod_pow(2, N - i - 1, cnt - 1)) + (cnt - 1)) % (cnt - 1);
        ans++;
      }
    } else {
      t = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      ans++;
    }
    while (t != 0LL) {
      t %= __builtin_popcountll(t);
      ans++;
    }
    cout << ans << "\n";
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>

using namespace std;

long long mod_pow(long long x, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(20) << std::fixed;

  int N;
  string X;
  cin >> N >> X;

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (X[i] == '1');
  }

  long long A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (cnt - 1 > 0) {
        A = (A + mod_pow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      }
      B = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    long long ans = 0, t = 0;
    if (X[i] == '1') {
      if (cnt != 1) {
        t = ((A - mod_pow(2, N - i - 1, cnt - 1)) + (cnt - 1)) % (cnt - 1);
        ans++;
      }
    } else {
      t = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      ans++;
    }
    while (t != 0LL) {
      t %= __builtin_popcountll(t);
      ans++;
    }
    cout << ans << "\n";
  }
  return 0;
}
Problem p02609 (C++)

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

int v[2][200010], res[200010];

int f(int n) {
  int res = 0;
  while (n > 0) {
    n %= __builtin_popcount(n);
    ++res;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int d = count(begin(x), end(x), '1');

  for (int i = 0; i < 1 + (d > 1); ++i) {
    v[i][0] = 1;
    for (int j = 0; j < n; ++j)
      v[i][j + 1] = v[i][j] * 2 % (i ? d - 1 : d + 1);
  }

  int r0 = 0, r1 = 0;
  for (auto &c : x)
    r0 = (r0 * 2 + (c - '0')) % (d + 1);
  for (auto &c : x)
    r1 = (r1 * 2 + (c - '0')) % (d - 1);
  reverse(begin(x), end(x));
  for (int i = 0; i < n; ++i) {
    char &c = x[i];
    if (c == '0') {
      int t = (r0 + v[0][i]) % (d + 1);
      res[i] = f(t) + 1;
    } else {
      if (d > 1) {
        int t = (r1 - v[1][i] + d - 1) % (d - 1);
        res[i] = f(t) + 1;
      }
    }
  }
  reverse(res, res + n);
  for (int i = 0; i < n; ++i)
    cout << res[i] << '\n';
}


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

int v[2][200010], res[200010];

int f(int n) {
  int res = 0;
  while (n > 0) {
    n %= __builtin_popcount(n);
    ++res;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int d = count(begin(x), end(x), '1');

  for (int i = 0; i < 1 + (d > 1); ++i) {
    v[i][0] = 1;
    for (int j = 0; j < n; ++j)
      v[i][j + 1] = v[i][j] * 2 % (i ? d - 1 : d + 1);
  }

  int r0 = 0, r1 = 0;
  for (auto &c : x)
    r0 = (r0 * 2 + (c - '0')) % (d + 1);
  if (d > 1)
    for (auto &c : x)
      r1 = (r1 * 2 + (c - '0')) % (d - 1);
  reverse(begin(x), end(x));
  for (int i = 0; i < n; ++i) {
    char &c = x[i];
    if (c == '0') {
      int t = (r0 + v[0][i]) % (d + 1);
      res[i] = f(t) + 1;
    } else {
      if (d > 1) {
        int t = (r1 - v[1][i] + d - 1) % (d - 1);
        res[i] = f(t) + 1;
      }
    }
  }
  reverse(res, res + n);
  for (int i = 0; i < n; ++i)
    cout << res[i] << '\n';
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
ll n;
string s;
ll b;
ll p, m;
ll ans[200005];
int main(void) {
  cin >> n >> s;
  if (s == "1") {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      b++;
  }
  ll k = 1, l = 1;
  for (int i = 0; i < n; i++) {
    p = (p + (s[n - 1 - i] - '0') * k) % (b + 1);
    if (b != 1) {
      m = (m + (s[n - 1 - i] - '0') * l) % (b - 1);
      l = (l * 2) % (b - 1);
    }
    k = (k * 2) % (b + 1);
  }
  // cout<<p<<" "<<m<<endl;
  k = 1, l = 1;
  ll c;
  for (int i = n - 1; i >= 0; i--) {
    c = 1;
    if (s[i] == '0') {
      ll t = (p + k) % (b + 1);
      // cout<<i<<" "<<t<<endl;
      while (t != 0) {
        ll a = 0;
        ll co = t;
        while (co > 0) {
          a += (co % 2);
          co = co / 2;
        }
        t = t % a;
        c++;
      }
    } else {
      if (b == 1) {
        ans[i] = 0;
        continue;
      }
      ll T = (m - l) % (b - 1);
      // cout<<i<<" "<<T<<endl;
      if (T < 0) {
        T += (b - 1);
      }
      while (T != 0) {
        ll A = 0;
        ll CO = T;
        while (CO > 0) {
          A += (CO % 2);
          CO = CO / 2;
        }
        T = T % A;
        c++;
      }
    }
    ans[i] = c;
    k = (k * 2) % (b + 1);
    l = (l * 2) % (b - 1);
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
ll n;
string s;
ll b;
ll p, m;
ll ans[200005];
int main(void) {
  cin >> n >> s;
  if (s == "1") {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      b++;
  }
  ll k = 1, l = 1;
  for (int i = 0; i < n; i++) {
    p = (p + (s[n - 1 - i] - '0') * k) % (b + 1);
    if (b != 1) {
      m = (m + (s[n - 1 - i] - '0') * l) % (b - 1);
      l = (l * 2) % (b - 1);
    }
    k = (k * 2) % (b + 1);
  }
  // cout<<p<<" "<<m<<endl;
  k = 1, l = 1;
  ll c;
  for (int i = n - 1; i >= 0; i--) {
    c = 1;
    if (s[i] == '0') {
      ll t = (p + k) % (b + 1);
      // cout<<i<<" "<<t<<endl;
      while (t != 0) {
        ll a = 0;
        ll co = t;
        while (co > 0) {
          a += (co % 2);
          co = co / 2;
        }
        t = t % a;
        c++;
      }
    } else {
      if (b == 1) {
        ans[i] = 0;
        continue;
      }
      ll T = (m - l) % (b - 1);
      // cout<<i<<" "<<T<<endl;
      if (T < 0) {
        T += (b - 1);
      }
      while (T != 0) {
        ll A = 0;
        ll CO = T;
        while (CO > 0) {
          A += (CO % 2);
          CO = CO / 2;
        }
        T = T % A;
        c++;
      }
    }
    ans[i] = c;
    k = (k * 2) % (b + 1);
    if (b != 1) {
      l = (l * 2) % (b - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define power 210000
int rm[power] = {}, rp[power] = {};
void Initialize(int c) {
  rm[0] = 1;
  rp[0] = 1;
  for (int i = 1; i < power; i++) {
    rm[i] = (rm[i - 1] * 2) % (c - 1);
    rp[i] = (rp[i - 1] * 2) % (c + 1);
  }
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int c = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  Initialize(c);
  int val_minus = 0, val_plus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      val_minus += 1;
      val_plus += 1;
    }
    val_minus %= c - 1;
    val_plus %= c + 1;
    if (i == n - 1)
      break;
    val_minus *= 2;
    val_plus *= 2;
  }
  int ans[power] = {};
  for (int i = 1; i < power; i++) {
    int count = 0;
    for (int j = 0; j < 20; j++) {
      if (i & (1 << j))
        count++;
    }
    ans[i] = ans[i % count] + 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      int rest = val_minus - rm[n - 1 - i];
      rest += c - 1;
      rest %= c - 1;
      cout << ans[rest] + 1 << endl;
    } else {
      int rest = val_plus + rp[n - 1 - i];
      rest %= c + 1;
      cout << ans[rest] + 1 << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define power 210000
int rm[power] = {}, rp[power] = {};
void Initialize(int c) {
  rm[0] = 1;
  rp[0] = 1;
  for (int i = 1; i < power; i++) {
    rm[i] = (rm[i - 1] * 2) % (c - 1);
    rp[i] = (rp[i - 1] * 2) % (c + 1);
  }
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int c = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  if (c == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  if (c == 1) {
    if (s[n - 1] == '1') {
      for (int i = 0; i < n - 1; i++) {
        cout << 2 << endl;
      }
      cout << 0 << endl;
    } else {
      for (int i = 0; i < n - 1; i++) {
        if (s[i] == '1') {
          cout << 0 << endl;
          continue;
        }
        cout << 1 << endl;
      }
      cout << 2 << endl;
    }
    return 0;
  }
  Initialize(c);
  int val_minus = 0, val_plus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      val_minus += 1;
      val_plus += 1;
    }
    val_minus %= c - 1;
    val_plus %= c + 1;
    if (i == n - 1)
      break;
    val_minus *= 2;
    val_plus *= 2;
  }
  int ans[power] = {};
  for (int i = 1; i < power; i++) {
    int count = 0;
    for (int j = 0; j < 20; j++) {
      if (i & (1 << j))
        count++;
    }
    ans[i] = ans[i % count] + 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      int rest = val_minus - rm[n - 1 - i];
      rest += c - 1;
      rest %= c - 1;
      cout << ans[rest] + 1 << endl;
    } else {
      int rest = val_plus + rp[n - 1 - i];
      rest %= c + 1;
      cout << ans[rest] + 1 << endl;
    }
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int n;
char x[200005];

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

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

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

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


Fixed submission:
#include <cstdio>

int n;
char x[200005];

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

long long f(long long x) {
  int cnt = 0;
  for (int i = 0; i < 20; i++) {
    cnt += (x >> i) & 1;
  }
  return x % cnt;
}

long long modpow(int p, long long m) {
  long long ret = 1;
  for (int i = 0; i < p; i++) {
    ret = ret * 2 % m;
  }
  return ret;
}

int main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  std::vector<long long> m(3), r(3);
  for (int i = 0; i < n; i++)
    m[1] += s[i] == '1';
  m[0] = m[1] - 1;
  m[2] = m[1] + 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (m[j] == 0)
        continue;
      r[j] = (r[j] * 2 + (s[i] == '1')) % m[j];
    }
  }
  for (int i = 0; i < n; i++) {
    int id = s[i] == '1' ? 0 : 2;
    if (m[id] == 0) {
      std::cout << 0 << std::endl;
      continue;
    }
    int ans = 1;
    long long q =
        (r[id] + m[id] + (id - 1) * modpow(n - 1 - i, m[id]) % m[id]) % m[id];
    while (q != 0) {
      q = f(q);
      ans++;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

long long f(long long x) {
  int cnt = 0;
  for (int i = 0; i < 20; i++) {
    cnt += (x >> i) & 1;
  }
  return x % cnt;
}

long long modpow(int p, long long m) {
  long long ret = 1;
  long long b = 2;
  for (int i = 0; i < 20; i++) {
    if ((p >> i) & 1)
      ret = ret * b % m;
    b = b * b % m;
  }
  return ret;
}

int main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  std::vector<long long> m(3), r(3);
  for (int i = 0; i < n; i++)
    m[1] += s[i] == '1';
  m[0] = m[1] - 1;
  m[2] = m[1] + 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (m[j] == 0)
        continue;
      r[j] = (r[j] * 2 + (s[i] == '1')) % m[j];
    }
  }
  for (int i = 0; i < n; i++) {
    int id = s[i] == '1' ? 0 : 2;
    if (m[id] == 0) {
      std::cout << 0 << std::endl;
      continue;
    }
    int ans = 1;
    long long q =
        (r[id] + m[id] + (id - 1) * modpow(n - 1 - i, m[id]) % m[id]) % m[id];
    while (q != 0) {
      q = f(q);
      ans++;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = a; i < b; i++)
#define int long long
#define mod 1000000009
#define garou                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
int pow(int p, int r, int m) {
  int ans = 1;
  while (r > 0) {
    if (r & 1) {
      ans = ((ans % m) * (p % m)) % m;
    }
    r = r >> 1;
    p = ((p % m) * (p % m)) % m;
  }
  return ans;
}
int popcount(int n) {
  int ans = 0;
  REP(i, 0, 32) {
    if ((n & (1 << i)))
      ans++;
  }
  return ans;
}
int binary(string x, int m) {
  int n = x.length();
  int ans = 0, j = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] != '0')
      ans = (ans + pow(2, j, m)) % m;
    j++;
  }
  return ans;
}
signed main() {
  garou;
  int n;
  cin >> n;
  string x;
  cin >> x;
  int one = 0;
  int n1 = x.length();
  REP(i, 0, n) {
    if (x[i] == '1')
      one++;
  }
  int a = binary(x, one + 1);
  int b = binary(x, one - 1);
  REP(i, 0, n1) {
    if (x[i] == '1' && (one - 1) == 0) {
      cout << 0 << endl;
      continue;
    }
    int c = 0;
    if (x[i] == '0')
      c = (a + pow(2, n1 - i - 1, one + 1)) % (one + 1);
    else
      c = (b - pow(2, n1 - i - 1, one - 1) + one - 1) % (one - 1);
    int re = 0;
    while (c) {
      c %= popcount(c);
      re++;
    }
    cout << re + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = a; i < b; i++)
#define int long long
#define mod 1000000009
#define garou                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
int pow(int p, int r, int m) {
  int ans = 1;
  while (r > 0) {
    if (r & 1) {
      ans = ((ans % m) * (p % m)) % m;
    }
    r = r >> 1;
    p = ((p % m) * (p % m)) % m;
  }
  return ans;
}
int popcount(int n) {
  int ans = 0;
  REP(i, 0, 32) {
    if ((n & (1 << i)))
      ans++;
  }
  return ans;
}
int binary(string x, int m) {
  int n = x.length();
  int ans = 0, j = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] != '0')
      ans = (ans + pow(2, j, m)) % m;
    j++;
  }
  return ans;
}
signed main() {
  garou;
  int n;
  cin >> n;
  string x;
  cin >> x;
  int one = 0;
  int n1 = x.length();
  REP(i, 0, n) {
    if (x[i] == '1')
      one++;
  }
  int a = binary(x, one + 1);
  int b = 0;
  if (one > 1)
    b = binary(x, one - 1);
  REP(i, 0, n1) {
    if (x[i] == '1' && (one - 1) == 0) {
      cout << 0 << endl;
      continue;
    }
    int c = 0;
    if (x[i] == '0')
      c = (a + pow(2, n1 - i - 1, one + 1)) % (one + 1);
    else
      c = (b - pow(2, n1 - i - 1, one - 1) + one - 1) % (one - 1);
    int re = 0;
    while (c) {
      c %= popcount(c);
      re++;
    }
    cout << re + 1 << endl;
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pb push_back
#define ii pair<int, int>
#define iii pair<ii, int>
#define ppb pop_back
#define st first
#define nd second
#define ll long long
#define N 500005
#define inf 2000000005
#define MOD 1000000007
#define LOG 14
#define EPS 0.000000001
using namespace std;

int n;
int mod[2];
int f[N];
int p[2][N], s[2];
string S;

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  for (int i = 1; i < N; i++) {
    int cnt = __builtin_popcount(i);
    f[i] = f[i % cnt] + 1;
  }
  cin >> n;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (S[i] == '1');
  mod[0] = cnt - 1;
  mod[1] = cnt + 1;
  for (int i = 0; i < 2; i++) {
    p[i][0] = 1;
    for (int j = 1; j <= n; j++) {
      p[i][j] = (ll)p[i][j - 1] * 2 % mod[i];
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {
      int bit = n - j - 1;
      if (S[j] == '1')
        s[i] = ((ll)s[i] + p[i][bit]) % mod[i];
    }
  }
  for (int i = 0; i < n; i++) {
    int bit = n - i - 1;
    int sum;
    if (S[i] == '1') {
      sum = s[0];
      sum = (((ll)sum - p[0][bit]) % mod[0] + mod[0]) % mod[0];
    } else {
      sum = s[1];
      sum = (((ll)sum + p[1][bit]) % mod[1] + mod[1]) % mod[1];
    }
    cout << f[sum] + 1 << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pb push_back
#define ii pair<int, int>
#define iii pair<ii, int>
#define ppb pop_back
#define st first
#define nd second
#define ll long long
#define N 500005
#define inf 2000000005
#define MOD 1000000007
#define LOG 14
#define EPS 0.000000001
using namespace std;

int n;
int mod[2];
int f[N];
int p[2][N], s[2];
string S;

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  for (int i = 1; i < N; i++) {
    int cnt = __builtin_popcount(i);
    f[i] = f[i % cnt] + 1;
  }
  cin >> n;
  cin >> S;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (S[i] == '1');
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (S[i] == '1')
        cout << "0\n";
      else {
        if (i == n - 1 or S[n - 1] == '1') {
          cout << "2\n";
        } else {
          cout << "1\n";
        }
      }
    }
    return 0;
  }
  mod[0] = cnt - 1;
  mod[1] = cnt + 1;
  for (int i = 0; i < 2; i++) {
    p[i][0] = 1;
    for (int j = 1; j <= n; j++) {
      p[i][j] = (ll)p[i][j - 1] * 2 % mod[i];
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {
      int bit = n - j - 1;
      if (S[j] == '1')
        s[i] = ((ll)s[i] + p[i][bit]) % mod[i];
    }
  }
  for (int i = 0; i < n; i++) {
    int bit = n - i - 1;
    int sum;
    if (S[i] == '1') {
      sum = s[0];
      sum = (((ll)sum - p[0][bit]) % mod[0] + mod[0]) % mod[0];
    } else {
      sum = s[1];
      sum = (((ll)sum + p[1][bit]) % mod[1] + mod[1]) % mod[1];
    }
    cout << f[sum] + 1 << "\n";
  }
}
Problem p02609 (C++)

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

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

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

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


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

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

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  for (int i = 0; i < n; ++i)
    x[i] = s[i] - '0';
  int pc = 0;
  for (int i = 0; i < n; ++i)
    pc += x[i];
  vector<int> ans(n);
  for (int b = 0; b < 2; ++b) {
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;

    if (npc <= 0)
      continue;
    int r0 = 0;
    for (int i = 0; i < n; ++i) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (x[i] == b) {
        int r = r0;
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[i] = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02609 (C++)

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

using namespace std;

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

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

  int cnt = 0;
  for (auto c : s) {
    cnt += (c == '1');
  }

  auto poly = [&](int mod) {
    vector<int> r(n);
    r[0] = 1;
    for (int i = 1; i < n; i++) {
      r[i] = r[i - 1] * 2 % mod;
    }
    return r;
  };

  auto f = [&](vector<int> &a, int mod) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum = (sum + (s[i] - '0') * a[n - 1 - i]) % mod;
    }
    return sum;
  };

  // one 1 to 0
  auto a0 = poly(cnt - 1);
  int r0 = f(a0, cnt - 1);

  auto a1 = poly(cnt + 1);
  int r1 = f(a1, cnt + 1);

  auto solve = [](int x) {
    int res = 0;
    while (x) {
      int cnt = __builtin_popcount(x);
      x = x % cnt;
      res++;
    }
    return res;
  };

  for (int i = 0; i < n; i++) {
    int sum;
    if (s[i] == '1') {
      sum = (r0 - a0[n - 1 - i]) % (cnt - 1);
      if (sum < 0)
        sum += (cnt - 1);
    } else {
      sum = (r1 + a1[n - 1 - i]) % (cnt + 1);
      if (sum < 0)
        sum += (cnt + 1);
    }
    cout << solve(sum) + 1 << '\n';
  }

  return 0;
}


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

using namespace std;

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

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

  int cnt = 0;
  for (auto c : s) {
    cnt += (c == '1');
  }

  // special
  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 (s[i] == '1')
        cout << 0 << '\n';
      else {
        if (s[n - 1] == '1' || i == n - 1)
          cout << 2 << '\n';
        else
          cout << 1 << '\n';
      }
    }
    return 0;
  }

  auto poly = [&](int mod) {
    vector<int> r(n);
    r[0] = 1;
    for (int i = 1; i < n; i++) {
      r[i] = r[i - 1] * 2 % mod;
    }
    return r;
  };

  auto f = [&](vector<int> &a, int mod) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum = (sum + (s[i] - '0') * a[n - 1 - i]) % mod;
    }
    return sum;
  };

  // one 1 to 0
  auto a0 = poly(cnt - 1);
  int r0 = f(a0, cnt - 1);

  auto a1 = poly(cnt + 1);
  int r1 = f(a1, cnt + 1);

  auto solve = [](int x) {
    int res = 0;
    while (x) {
      int cnt = __builtin_popcount(x);
      x = x % cnt;
      res++;
    }
    return res;
  };

  for (int i = 0; i < n; i++) {
    int sum;
    if (s[i] == '1') {
      sum = (r0 - a0[n - 1 - i]) % (cnt - 1);
      if (sum < 0)
        sum += (cnt - 1);
    } else {
      sum = (r1 + a1[n - 1 - i]) % (cnt + 1);
      if (sum < 0)
        sum += (cnt + 1);
    }
    cout << solve(sum) + 1 << '\n';
  }

  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#ifndef DEBUG
#pragma GCC optimize("O3,unroll-loops")
#pragma loop_opt(on)
#endif
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#define FAST ;
#else
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define endl '\n'
#define cerr
#endif
#define _ << ' ' <<
#define Fr(i, s, e) for (auto i = s; i < e; ++i)
#define All(v) v.begin(), v.end()
#define Ft first
#define Sd second
using ll = long long;
using pii = pair<int, int>;
constexpr ll MOD = 1e9 + 7;

signed main() {
  FAST;

  int n;
  string x;
  cin >> n >> x;
  reverse(All(x));
  vector<int> ans(n);
  int c = 0;
  Fr(j, 0, n) c += x[j] == '1';
  auto f = [&](char t, int z) {
    const int mod = c + z;
    int k = 0, r = 1;
    Fr(i, 0, n) {
      k = (k + r * (x[i] == '1')) % mod;
      r = r * 2 % mod;
    }
    r = (z + mod) % mod;
    Fr(i, 0, n) {
      if (x[i] == t) {
        ans[i] = 1;
        int y = (k + r) % mod;
        while (y) {
          ans[i]++;
          y = y % __builtin_popcount(y);
        }
      }
      r = r * 2 % mod;
    }
  };
  f('1', -1);
  f('0', +1);
  reverse(All(ans));
  Fr(i, 0, n) cout << ans[i] << endl;
}


Fixed submission:
#ifndef DEBUG
#pragma GCC optimize("O3,unroll-loops")
#pragma loop_opt(on)
#endif
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#define FAST ;
#else
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define endl '\n'
#define cerr
#endif
#define _ << ' ' <<
#define Fr(i, s, e) for (auto i = s; i < e; ++i)
#define All(v) v.begin(), v.end()
#define Ft first
#define Sd second
using ll = long long;
using pii = pair<int, int>;
constexpr ll MOD = 1e9 + 7;

signed main() {
  FAST;

  int n;
  string x;
  cin >> n >> x;
  reverse(All(x));
  vector<int> ans(n);
  int c = 0;
  Fr(j, 0, n) c += x[j] == '1';
  auto f = [&](char t, int z) {
    const int mod = c + z;
    if (!mod)
      return;
    int k = 0, r = 1;
    Fr(i, 0, n) {
      k = (k + r * (x[i] == '1')) % mod;
      r = r * 2 % mod;
    }
    r = (z + mod) % mod;
    Fr(i, 0, n) {
      if (x[i] == t) {
        ans[i] = 1;
        int y = (k + r) % mod;
        while (y) {
          ans[i]++;
          y = y % __builtin_popcount(y);
        }
      }
      r = r * 2 % mod;
    }
  };
  f('1', -1);
  f('0', +1);
  reverse(All(ans));
  Fr(i, 0, n) cout << ans[i] << endl;
}
Problem p02609 (Python)

Buggy submission (status: Runtime Error):
import math


def f(n):
    pop = 0
    ntemp = n
    for i in range(int(math.log2(n)) + 1):
        pop += ntemp % 2
        ntemp //= 2
    return n % pop


n = int(input())
S = input()
popcount = 0
for i in range(n):
    popcount += int(S[i])
Sint = int(S, 2)
remminus = Sint % (popcount - 1)
remplus = Sint % (popcount + 1)

# 1,2,4,...の余りのリストを準備
remlistminus = []
remlistplus = []
temp = 1
for i in range(n):
    remlistminus.append(temp)
    temp = (temp * 2) % (popcount - 1)
temp = 1
for i in range(n):
    remlistplus.append(temp)
    temp = (temp * 2) % (popcount + 1)
remlistminus.reverse()
remlistplus.reverse()

ans = []
for i in range(n):
    count = 1
    if S[i] == "0":
        rem = (remplus + remlistplus[i]) % (popcount + 1)
        while rem > 0:
            rem = f(rem)
            count += 1
    else:
        rem = (remminus - remlistminus[i]) % (popcount - 1)
        while rem > 0:
            rem = f(rem)
            count += 1
    ans.append(count)
for i in range(n):
    print(ans[i])


Fixed submission:
import math


def f(n):
    pop = 0
    ntemp = n
    for i in range(int(math.log2(n)) + 1):
        pop += ntemp % 2
        ntemp //= 2
    return n % pop


n = int(input())
S = input()
popcount = 0
for i in range(n):
    popcount += int(S[i])

# 1が1個しかない場合の例外処理
if popcount == 1:
    if S[-1] == 1:
        ans = [2] * n
        ans[-1] = 0
    else:
        ans = [1] * n
        ans[-1] = 2
        for i in range(n):
            if S[i] == "1":
                ans[i] = 0
    for i in range(n):
        print(ans[i])
    exit()


Sint = int(S, 2)
remminus = Sint % (popcount - 1)
remplus = Sint % (popcount + 1)

# 1,2,4,...の余りのリストを準備
remlistminus = []
remlistplus = []
temp = 1
for i in range(n):
    remlistminus.append(temp)
    temp = (temp * 2) % (popcount - 1)
temp = 1
for i in range(n):
    remlistplus.append(temp)
    temp = (temp * 2) % (popcount + 1)
remlistminus.reverse()
remlistplus.reverse()

ans = []
for i in range(n):
    count = 1
    if S[i] == "0":
        rem = (remplus + remlistplus[i]) % (popcount + 1)
        while rem > 0:
            rem = f(rem)
            count += 1
    else:
        rem = (remminus - remlistminus[i]) % (popcount - 1)
        while rem > 0:
            rem = f(rem)
            count += 1
    ans.append(count)
for i in range(n):
    print(ans[i])
Problem p02609 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
X = input()
Xi = int(X, 2)

c0 = X.count("1")
Xp = Xi % (c0 + 1)
Xm = Xi % (c0 - 1)

for i, x in enumerate(X):
    if x == "1":
        if c0 == 1:
            print(0)
            continue
        y = (Xm - pow(2, N - 1 - i, c0 - 1)) % (c0 - 1)
    else:
        y = (Xp + pow(2, N - 1 - i, c0 + 1)) % (c0 + 1)

    ans = 1
    while y:
        y %= bin(y).count("1")
        ans += 1
    print(ans)


Fixed submission:
N = int(input())
X = input()
Xi = int(X, 2)

c0 = X.count("1")
Xp = Xi % (c0 + 1)

if c0 != 1:
    Xm = Xi % (c0 - 1)

for i, x in enumerate(X):
    if x == "1":
        if c0 == 1:
            print(0)
            continue
        y = (Xm - pow(2, N - 1 - i, c0 - 1)) % (c0 - 1)
    else:
        y = (Xp + pow(2, N - 1 - i, c0 + 1)) % (c0 + 1)

    ans = 1
    while y:
        y %= bin(y).count("1")
        ans += 1
    print(ans)
Problem p02609 (Python)

Buggy submission (status: Runtime Error):
from functools import lru_cache

N = int(input())
X = input()


@lru_cache(maxsize=None)
def popcount(x):
    return x % bin(x).count("1")


def f(x):
    cnt = 1
    while x > 0:
        x = popcount(x)
        cnt += 1

    return cnt


count = X.count("1")
mod_one = count - 1
mod_zero = count + 1
ones = [1 % mod_one]
zeros = [1 % mod_zero]
x_mod_one = 0 if X[-1] == "0" else 1 % mod_one
x_mod_zero = 0 if X[-1] == "0" else 1 % mod_zero

for xi in reversed(X[:-1]):
    ones.append(ones[-1] * 2 % mod_one)
    zeros.append(zeros[-1] * 2 % mod_zero)
    if xi == "1":
        x_mod_one += ones[-1]
        x_mod_one %= mod_one
        x_mod_zero += zeros[-1]
        x_mod_zero %= mod_zero

ones = list(reversed(ones))
zeros = list(reversed(zeros))

for i in range(N):
    if count == 1 and X[i] == "1":
        print(0)
    elif X[i] == "1":
        x = (x_mod_one - ones[i]) % mod_one
        print(f(x))
    else:
        x = (x_mod_zero + zeros[i]) % mod_zero
        print(f(x))


Fixed submission:
from functools import lru_cache

N = int(input())
X = input()


@lru_cache(maxsize=None)
def popcount(x):
    return x % bin(x).count("1")


def f(x):
    cnt = 1
    while x > 0:
        x = popcount(x)
        cnt += 1

    return cnt


count = X.count("1")
mod_one = count - 1
if mod_one == 0:
    mod_one = 1
mod_zero = count + 1
ones = [1 % mod_one]
zeros = [1 % mod_zero]
x_mod_one = 0 if X[-1] == "0" else 1 % mod_one
x_mod_zero = 0 if X[-1] == "0" else 1 % mod_zero

for xi in reversed(X[:-1]):
    ones.append(ones[-1] * 2 % mod_one)
    zeros.append(zeros[-1] * 2 % mod_zero)
    if xi == "1":
        x_mod_one += ones[-1]
        x_mod_one %= mod_one
        x_mod_zero += zeros[-1]
        x_mod_zero %= mod_zero

ones = list(reversed(ones))
zeros = list(reversed(zeros))

for i in range(N):
    if count == 1 and X[i] == "1":
        print(0)
    elif X[i] == "1":
        x = (x_mod_one - ones[i]) % mod_one
        print(f(x))
    else:
        x = (x_mod_zero + zeros[i]) % mod_zero
        print(f(x))
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long modpow(long a, long b, long mod) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return res;
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int one = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      one++;
  long plus[n + 1];
  long minus[n + 1];
  long sum_plus = 0;
  long sum_minus = 0;
  for (long i = 0; i < n; i++) {
    if (one != n) {
      plus[i] = modpow(2L, i, (long)(one + 1));
      if (s[n - 1 - i] == '1')
        sum_plus = (sum_plus + plus[i]) % ((long)(one + 1));
    }
    if (one != 0) {
      minus[i] = modpow(2L, i, (long)(one - 1));
      if (s[n - 1 - i] == '1')
        sum_minus = (sum_minus + minus[i]) % ((long)(one - 1));
    }
  }
  for (int i = 0; i < n; i++) {
    long tmp;
    int ans = 0;
    if (s[i] == '0') {
      tmp = (sum_plus + plus[n - 1 - i]) % ((long)(one + 1));
      ans++;
      while (tmp > 0) {
        tmp = tmp % __builtin_popcount((int)tmp);
        ans++;
      }
      cout << ans << endl;
    } else {
      if (one == 1)
        cout << 0 << endl;
      else {
        tmp = (sum_minus + one - 1 - minus[n - 1 - i]) % ((long)(one - 1));
        ans++;
        while (tmp > 0) {
          tmp = tmp % __builtin_popcount((int)tmp);
          ans++;
        }
        cout << ans << endl;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long modpow(long a, long b, long mod) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return res;
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int one = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      one++;
  long plus[n + 1];
  long minus[n + 1];
  long sum_plus = 0;
  long sum_minus = 0;
  for (long i = 0; i < n; i++) {
    if (one != n) {
      plus[i] = modpow(2L, i, (long)(one + 1));
      if (s[n - 1 - i] == '1')
        sum_plus = (sum_plus + plus[i]) % ((long)(one + 1));
    }
    if (one > 1) {
      minus[i] = modpow(2L, i, (long)(one - 1));
      if (s[n - 1 - i] == '1')
        sum_minus = (sum_minus + minus[i]) % ((long)(one - 1));
    }
  }
  for (int i = 0; i < n; i++) {
    long tmp;
    int ans = 0;
    if (s[i] == '0') {
      tmp = (sum_plus + plus[n - 1 - i]) % ((long)(one + 1));
      ans++;
      while (tmp > 0) {
        tmp = tmp % __builtin_popcount((int)tmp);
        ans++;
      }
      cout << ans << endl;
    } else {
      if (one == 1)
        cout << 0 << endl;
      else {
        tmp = (sum_minus + one - 1 - minus[n - 1 - i]) % ((long)(one - 1));
        ans++;
        while (tmp > 0) {
          tmp = tmp % __builtin_popcount((int)tmp);
          ans++;
        }
        cout << ans << endl;
      }
    }
  }
}