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

#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main() {
  int N;
  string X;
  cin >> N >> X;

  vector<int> cnt(N + 1);

  repd(i, 1, N + 1) {
    int c = i;
    while (c != 0) {
      cnt[i] += c % 2;
      c /= 2;
    }
  }

  int cnt1 = 0;
  rep(i, N) {
    if (X[i] == '1')
      cnt1++;
  }

  vector<int> mod01(2);
  int md = 0;
  if (cnt1 == 1)
    md = 1;
  ll ct0 = 1 % (cnt1 + 1), ct1 = 1 % (cnt1 - 1 + md);

  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      mod01[0] += ct0;
      mod01[1] += ct1;
      mod01[0] %= (cnt1 + 1);
      mod01[1] %= (cnt1 - 1 + md);
    }
    ct0 *= 2;
    ct1 *= 2;
    ct0 %= (cnt1 + 1);
    ct1 %= (cnt1 - 1 + md);
  }

  vector<int> ans(N, 1);

  ct0 = 1 % (cnt1 + 1);
  ct1 = 1 % (cnt1 - 1 + md);

  for (int i = N - 1; i >= 0; i--) {
    int num;
    int res = 1;
    if (X[i] == '1') {
      num = mod01[1] - ct1;
      num %= (cnt1 - 1 + md);
      if (num < 0)
        num += (cnt1 - 1 + md);
    } else {
      num = mod01[0] + ct0;
      num %= (cnt1 + 1);
      if (num < 0)
        num += (cnt1 + 1);
    }
    while (num != 0) {
      num %= cnt[num];
      res++;
    }
    ans[i] = res;
    if (md == 1 && X[i] == '1')
      ans[i] = 0;
    ct0 *= 2;
    ct1 *= 2;
    ct0 %= (cnt1 + 1);
    ct1 %= (cnt1 - 1 + md);
  }

  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main() {
  int N;
  string X;
  cin >> N >> X;

  vector<int> cnt(N + 1);

  repd(i, 1, N + 1) {
    int c = i;
    while (c != 0) {
      cnt[i] += c % 2;
      c /= 2;
    }
  }

  int cnt1 = 0;
  rep(i, N) {
    if (X[i] == '1')
      cnt1++;
  }

  vector<int> mod01(2);
  ll ct0 = 1 % (cnt1 + 1), ct1 = 1 % (cnt1 - 1);
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      mod01[0] += ct0;
      mod01[1] += ct1;
      mod01[0] %= (cnt1 + 1);
      mod01[1] %= (cnt1 - 1);
    }
    ct0 *= 2;
    ct1 *= 2;
    ct0 %= (cnt1 + 1);
    ct1 %= (cnt1 - 1);
  }

  vector<int> ans(N, 1);

  ct0 = 1 % (cnt1 + 1);
  ct1 = 1 % (cnt1 - 1);

  for (int i = N - 1; i >= 0; i--) {
    int num;
    int res = 1;
    if (X[i] == '1') {
      num = mod01[1] - ct1;
      num %= (cnt1 - 1);
      if (num < 0)
        num += (cnt1 - 1);
    } else {
      num = mod01[0] + ct0;
      num %= (cnt1 + 1);
      if (num < 0)
        num += (cnt1 + 1);
    }
    while (num != 0) {
      num %= cnt[num];
      res++;
    }
    ans[i] = res;
    ct0 *= 2;
    ct1 *= 2;
    ct0 %= (cnt1 + 1);
    ct1 %= (cnt1 - 1);
  }

  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}