Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MAX 510000
#define rrep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define dcout cout << fixed << setprecision(15);
#define mp make_pair
#define pb push_back

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

constexpr int MOD = 1e9 + 7;
constexpr ll inf = 1LL << 60;

template <typename S, typename T> inline void chmax(S &a, const T &b) {
  if (a < b)
    a = b;
}

template <typename S, typename T> inline void chmin(S &a, const T &b) {
  if (a > b)
    a = b;
}

// lcm//
ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

// a^n mod p//
ll modpow(ll a, ll n, ll p) {
  if (n == 0)
    return (ll)1;
  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;
}

// inversemod//
ll modinv(ll a, ll m) {
  if (m == 0)
    return (ll)1;
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// Cmonp//
ll fac[MAX], finv[MAX], inv[MAX];
//
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

//
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// LARGE n
ll com(ll n, ll m) {
  if (n < m || n <= 0 || m < 0) {
    return 0;
  }
  if (m == 0 || n == m) {
    return 1;
  }
  ll k = 1;
  for (ll i = 1; i <= m; i++) {
    k *= (n - i + 1);
    k %= MOD;
    k *= modinv(i, MOD);
    k %= MOD;
  }
  return k;
}

// radP
ll rad(ll u, ll p) {
  ll cnt = 0;
  while (u % p == 0) {
    u /= p;
    cnt++;
  }
  return cnt;
}

////////////////////////////////////////////////////////////////////
ll pow2[24];
ll kekka[1000];
ll res;
ll ok;
void count(ll p, ll kai) {

  if (p == 0) {
    res = kai;
    return;
  }
  if (ok && p <= 999) {
    res = kai + kekka[p];
    return;
  }
  ll keta = 0;
  for (ll i = 0; i <= 23; i++) {
    ll val = pow2[i];
    if (p & (ll)1 << i)
      keta++;
  }
  count(p % keta, kai + 1);
  return;
}

int main() {

  ios::sync_with_stdio(false);
  ok = 0;
  res = 0;
  pow2[0] = 1;
  for (int i = 1; i <= 23; i++) {
    pow2[i] = (ll)2 * pow2[i - 1];
  }

  ll n;
  cin >> n;

  string s;
  cin >> s;

  for (ll i = 1; i <= 999; i++) {
    res = 0;
    count(i, 0);
    kekka[i] = res;
  }
  res = 0;
  ok = 1;
  ll cnt1 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt1++;
  }

  ll ans[n];
  rep(i, n) { ans[i - 1] = 0; }

  ll rem[3][n];
  rem[0][n - 1] = 1;
  rem[1][n - 1] = 1;
  rem[2][n - 1] = 1;

  for (int i = 0; i <= 2; i++) {
    for (int j = n - 2; j >= 0; j--) {
      ll p = cnt1 + i - 1;
      rem[i][j] = (rem[i][j + 1] * 2) % p;
    }
  }

  ll r0wa = 0;
  ll r1wa = 0;
  ll r2wa = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      r0wa += rem[0][i];
      if (cnt1 >= 2) {
        r0wa %= cnt1 - 1;
      }
      r1wa += rem[1][i];
      r1wa %= cnt1;
      r2wa += rem[2][i];
      r2wa %= cnt1 + 1;
    }
  }

  for (int i = 0; i < n; i++) {
    res = 0;
    ll val;
    if (s[i] == '0') {
      val = r2wa;
      val += rem[2][i];
      val %= cnt1 + 1;
      count(val, 1);
    }
    if (s[i] == '1') {
      val = r0wa;
      val -= rem[0][i];
      val += cnt1 * 2 - 2;
      if (cnt1 == 1) {
        ans[i] = 0;
        continue;
      }
      val %= cnt1 - 1;
      count(val, 1);
    }
    ans[i] = res;
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MAX 510000
#define rrep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define dcout cout << fixed << setprecision(15);
#define mp make_pair
#define pb push_back

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

constexpr int MOD = 1e9 + 7;
constexpr ll inf = 1LL << 60;

template <typename S, typename T> inline void chmax(S &a, const T &b) {
  if (a < b)
    a = b;
}

template <typename S, typename T> inline void chmin(S &a, const T &b) {
  if (a > b)
    a = b;
}

// lcm//
ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

// a^n mod p//
ll modpow(ll a, ll n, ll p) {
  if (n == 0)
    return (ll)1;
  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;
}

// inversemod//
ll modinv(ll a, ll m) {
  if (m == 0)
    return (ll)1;
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// Cmonp//
ll fac[MAX], finv[MAX], inv[MAX];
//
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

//
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// LARGE n
ll com(ll n, ll m) {
  if (n < m || n <= 0 || m < 0) {
    return 0;
  }
  if (m == 0 || n == m) {
    return 1;
  }
  ll k = 1;
  for (ll i = 1; i <= m; i++) {
    k *= (n - i + 1);
    k %= MOD;
    k *= modinv(i, MOD);
    k %= MOD;
  }
  return k;
}

// radP
ll rad(ll u, ll p) {
  ll cnt = 0;
  while (u % p == 0) {
    u /= p;
    cnt++;
  }
  return cnt;
}

////////////////////////////////////////////////////////////////////
ll pow2[24];
ll kekka[1000];
ll res;
ll ok;
void count(ll p, ll kai) {

  if (p == 0) {
    res = kai;
    return;
  }
  if (ok && p <= 999) {
    res = kai + kekka[p];
    return;
  }
  ll keta = 0;
  for (ll i = 0; i <= 23; i++) {
    ll val = pow2[i];
    if (p & (ll)1 << i)
      keta++;
  }
  count(p % keta, kai + 1);
  return;
}

int main() {

  ios::sync_with_stdio(false);
  ok = 0;
  res = 0;
  pow2[0] = 1;
  for (int i = 1; i <= 23; i++) {
    pow2[i] = (ll)2 * pow2[i - 1];
  }

  ll n;
  cin >> n;

  string s;
  cin >> s;

  for (ll i = 1; i <= 999; i++) {
    res = 0;
    count(i, 0);
    kekka[i] = res;
  }
  res = 0;
  ok = 1;
  ll cnt1 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt1++;
  }

  ll ans[n];
  rep(i, n) { ans[i - 1] = 0; }

  ll rem[3][n];
  rem[0][n - 1] = 1;
  rem[1][n - 1] = 1;
  rem[2][n - 1] = 1;

  for (int i = 0; i <= 2; i++) {
    for (int j = n - 2; j >= 0; j--) {
      ll p = cnt1 + i - 1;
      if (p >= 1) {
        rem[i][j] = (rem[i][j + 1] * 2) % p;
      }
    }
  }

  ll r0wa = 0;
  ll r1wa = 0;
  ll r2wa = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      r0wa += rem[0][i];
      if (cnt1 >= 2) {
        r0wa %= cnt1 - 1;
      }
      r1wa += rem[1][i];
      r1wa %= cnt1;
      r2wa += rem[2][i];
      r2wa %= cnt1 + 1;
    }
  }

  for (int i = 0; i < n; i++) {
    res = 0;
    ll val;
    if (s[i] == '0') {
      val = r2wa;
      val += rem[2][i];
      val %= cnt1 + 1;
      count(val, 1);
    }
    if (s[i] == '1') {
      val = r0wa;
      val -= rem[0][i];
      val += cnt1 * 2 - 2;
      if (cnt1 == 1) {
        ans[i] = 0;
        continue;
      }
      val %= cnt1 - 1;
      count(val, 1);
    }
    ans[i] = res;
  }

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

  return 0;
}