Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const long double PI = 3.141592653589793238462643383279502884197;
ll MOD;

ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll mp(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}
ll cmb(ll r, ll c) { return fac[r] * inv[c] % MOD * inv[r - c] % MOD; }

priority_queue<int, vector<int>, greater<int>> pq;
vector<int> v;

int n;
char s[200001];
ll two[200001][3]; //-1,0,1 순서.

int f(int o) {
  if (o == 0)
    return 0;
  int cnt = __builtin_popcount(o);
  return f(o % cnt) + 1;
}

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

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    s[i] -= '0';
    cnt += s[i];
  }

  for (int j = 0; j < 3; j++) {
    two[0][j] = 1;
    if (j == 0 && cnt == 1 || j == 1 && cnt == 0)
      continue;
    for (int i = 1; i < n; i++)
      two[i][j] = two[i - 1][j] * 2 % (cnt + j - 1);
  }

  int oc = cnt;
  ll now[3] = {0};
  reverse(s, s + n);

  for (int j = 0; j < 3; j++) {
    if (j == 0 && cnt == 1 || j == 1 && cnt == 0)
      continue;
    for (int i = n - 1; i >= 0; i--) {
      now[j] = (now[j] + s[i] * two[i][j]) % (cnt + j - 1);
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    cnt -= s[i];
    s[i] ^= 1;
    cnt += s[i];

    int j = cnt - oc + 1;
    ll pre = now[j];

    if (cnt == 0)
      printf("%d\\n", 0);
    else {
      if (s[i] == 0)
        now[j] = (now[j] - two[i][j] + cnt) % cnt;
      else
        now[j] = (now[j] + two[i][j]) % cnt;

      printf("%d\\n", f(now[j]) + 1);
      now[j] = pre;
    }

    cnt -= s[i];
    s[i] ^= 1;
    cnt += s[i];
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const long double PI = 3.141592653589793238462643383279502884197;
ll MOD;

ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll mp(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}
ll cmb(ll r, ll c) { return fac[r] * inv[c] % MOD * inv[r - c] % MOD; }

priority_queue<int, vector<int>, greater<int>> pq;
vector<int> v;

int n;
char s[200001];
ll two[200001][3]; //-1,0,1 순서.

int f(int o) {
  if (o == 0)
    return 0;
  int cnt = __builtin_popcount(o);
  return f(o % cnt) + 1;
}

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

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    s[i] -= '0';
    cnt += s[i];
  }

  for (int j = 0; j < 3; j++) {
    two[0][j] = 1;
    if (j == 0 && cnt == 1)
      continue;
    for (int i = 1; i < n; i++)
      two[i][j] = two[i - 1][j] * 2 % (cnt + j - 1);
  }

  int oc = cnt;
  ll now[3] = {0};
  reverse(s, s + n);

  for (int j = 0; j < 3; j++) {
    if (j == 0 && cnt == 1)
      continue;
    for (int i = n - 1; i >= 0; i--) {
      now[j] = (now[j] + s[i] * two[i][j]) % (cnt + j - 1);
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    cnt -= s[i];
    s[i] ^= 1;
    cnt += s[i];

    int j = cnt - oc + 1;
    ll pre = now[j];

    if (cnt == 0)
      printf("%d\\n", 0);
    else {
      if (s[i] == 0)
        now[j] = (now[j] - two[i][j] + cnt) % cnt;
      else
        now[j] = (now[j] + two[i][j]) % cnt;

      printf("%d\\n", f(now[j]) + 1);
      now[j] = pre;
    }

    cnt -= s[i];
    s[i] ^= 1;
    cnt += s[i];
  }
}