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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll n;
string x;
ll cnt = 0;

ll f[200200];
ll pos[200200], neg[200200];
ll p = 0, q = 0;

ll dfs(ll n) {
  if (n == 0)
    return 0;
  if (f[n])
    return f[n];
  ll res = n, cnt = 0;
  while (res) {
    if (res & 1)
      cnt++;
    res >>= 1;
  }
  return f[n] = dfs(n % cnt) + 1;
}

void init() {
  rep(i, 200200) dfs(i);
  pos[0] = 1;
  FOR(i, 1, 200200) pos[i] = pos[i - 1] * 2 % (cnt + 1);
  rep(i, n) {
    p *= 2;
    if (x[i] == '1')
      p++;
    p %= (cnt + 1);
  }
  if (cnt <= 1)
    return;
  neg[0] = 1;
  FOR(i, 1, 200200) neg[i] = neg[i - 1] * 2 % (cnt - 1);
  rep(i, n) {
    q *= 2;
    if (x[i] == '1')
      q++;
    q %= (cnt - 1);
  }
}

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

  cin >> n >> x;

  rep(i, n) if (x[i] == '1') cnt++;
  init();
  rep(i, n) {
    if (x[i] == '0') {
      ll res = (p + pos[n - 1 - i]) % (cnt + 1);
      // cout << pos[n - 1 - i] << '\\n';
      // cout << res << '\\n';
      cout << f[res] + 1 << '\\n';
    } else {
      if (cnt == 1) {
        cout << 0 << '\\n';
        continue;
      }
      ll res = (q - neg[n - 1 - i] + (cnt - 1) * 200) % (cnt - 1);
      // cout << res << '\\n';
      cout << f[res] + 1 << '\\n';
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll n;
string x;
ll cnt;

ll f[200200];
ll pos[200200], neg[200200];
ll p = 0, q = 0;

ll dfs(ll n) {
  if (n == 0)
    return 0;
  if (f[n])
    return f[n];
  ll res = n, cnt = 0;
  while (res) {
    if (res & 1)
      cnt++;
    res >>= 1;
  }
  return f[n] = dfs(n % cnt) + 1;
}

void init() {
  rep(i, 200200) dfs(i);
  pos[0] = 1;
  FOR(i, 1, 200200) pos[i] = pos[i - 1] * 2 % (cnt + 1);
  rep(i, n) {
    p *= 2;
    if (x[i] == '1')
      p++;
    p %= (cnt + 1);
  }
  if (cnt <= 1)
    return;
  neg[0] = 1;
  FOR(i, 1, 200200) neg[i] = neg[i - 1] * 2 % (cnt - 1);
  rep(i, n) {
    q *= 2;
    if (x[i] == '1')
      q++;
    q %= (cnt - 1);
  }
}

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

  cin >> n >> x;

  rep(i, n) if (x[i] == '1') cnt++;
  init();
  rep(i, n) {
    if (x[i] == '0') {
      ll res = (p + pos[n - 1 - i]) % (cnt + 1);
      // cout << pos[n - 1 - i] << '\\n';
      // cout << res << '\\n';
      cout << f[res] + 1 << '\\n';
    } else {
      ll res = (q - neg[n - 1 - i] + cnt - 1) % (cnt - 1);
      // cout << res << '\\n';
      cout << f[res] + 1 << '\\n';
    }
  }

  return 0;
}