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

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
const ll INF = 1LL << 60;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<int> f(N + 1, -1);
  vector<int> facminus, facplus;
  int cnt = 0;
  rep(i, N) if (X[i] == '1') cnt++;
  bool forbiddenMinus = false;
  int xminus = 0, xplus = 0;
  int t = 1;
  cnt--;
  if (cnt < 1) {
    forbiddenMinus = true;
  } else {
    rep(i, N) {
      if (X[N - i - 1] == '1') {
        xminus += t;
        xminus %= cnt;
      }
      t %= cnt;
      facminus.push_back(t);
      t *= 2;
    }
  }

  cnt += 2;
  t = 1;
  rep(i, N) {
    if (X[N - i - 1] == '1') {
      xplus += t;
      xplus %= cnt;
    }
    t %= cnt;
    facplus.push_back(t);
    t *= 2;
  }
  cnt--;
  // cout << xminus << endl;
  // cout << xplus << endl;
  // rep(i, N) cout << facminus[i] << endl;
  // rep(i, N) cout << facplus[i] << endl;
  rep(i, N) {
    if (forbiddenMinus == true && X[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    int tmp = 0;
    if (X[i] == '0') {
      tmp = xplus + facplus[N - i - 1];
      tmp %= cnt + 1;
    } else {
      tmp = xminus - facminus[N - i - 1];
      tmp += cnt - 1;
      tmp %= cnt - 1;
    }
    // cout << i << " " << tmp << endl;
    int ans = 1;
    while (tmp != 0) {
      bitset<20> s(tmp);
      int cnt2 = 0;
      rep(i, 20) if (s.test(i)) cnt2++;
      tmp %= cnt2;
      ans++;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
const ll INF = 1LL << 60;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  // N = 200000;
  // X = string(N, '1');
  vector<int> f(N + 1, -1);
  vector<int> facminus, facplus;
  int cnt = 0;
  rep(i, N) if (X[i] == '1') cnt++;
  bool forbiddenMinus = false;
  int xminus = 0, xplus = 0;
  int t = 1;
  cnt--;
  if (cnt < 1) {
    forbiddenMinus = true;
  } else {
    rep(i, N) {
      if (X[N - i - 1] == '1') {
        xminus += t;
        xminus %= cnt;
      }
      t %= cnt;
      facminus.push_back(t);
      t *= 2;
    }
  }

  cnt += 2;
  t = 1;
  rep(i, N) {
    if (X[N - i - 1] == '1') {
      xplus += t;
      xplus %= cnt;
    }
    t %= cnt;
    facplus.push_back(t);
    t *= 2;
  }
  cnt--;
  // cout << xminus << endl;
  // cout << xplus << endl;
  // rep(i, N) cout << facminus[i] << endl;
  // rep(i, N) cout << facplus[i] << endl;
  rep(i, N) {
    if (forbiddenMinus == true && X[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    int tmp = 0;
    if (X[i] == '0') {
      tmp = xplus + facplus[N - i - 1];
      tmp %= cnt + 1;
    } else {
      tmp = xminus - facminus[N - i - 1];
      tmp += cnt - 1;
      tmp %= cnt - 1;
    }
    // cout << i << " " << tmp << endl;
    int ans = 1;
    while (tmp != 0) {
      bitset<12> s(tmp);
      int cnt2 = 0;
      rep(i, 12) if (s.test(i)) cnt2++;
      tmp %= cnt2;
      ans++;
    }
    cout << ans << endl;
  }
}