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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_popcount(unsigned int x) { return __popcnt(x); }
#endif // _MSC_VER
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     @hamayanhamayan0
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

int N;
string X;
int cnt[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
  cin >> N >> X;

  rep(i, 1, 201010) {
    int pp = __builtin_popcount(i);
    cnt[i] = cnt[i % pp] + 1;
  }

  reverse(all(X));

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

  int tot1 = 0, p1 = 1;
  int tot2 = 0, p2 = 1;
  rep(i, 0, N) {
    if (X[i] == '1') {
      tot1 = (tot1 + p1) % (cn + 1);
      if (2 <= cn)
        tot2 = (tot2 + p2) % (cn - 1);
    }

    p1 = (p1 * 2) % (cn + 1);
    if (2 <= cn)
      p2 = (p2 * 2) % (cn - 1);
  }

  vector<int> ans;
  p1 = 1;
  p2 = 1;
  rep(i, 0, N) {
    if (X[i] == '1') {
      if (cn == 1) {
        ans.push_back(0);
      } else {
        int x = (tot2 - p2 + cn - 1) % (cn - 1);
        ans.push_back(cnt[x] + 1);
      }
    } else {
      int x = (tot1 + p1 + cn + 1) % (cn + 1);
      ans.push_back(cnt[x] + 1);
    }

    p1 = (p1 * 2) % (cn + 1);
    if (2 <= cn)
      p2 = (p2 * 2) % (cn - 1);
  }

  reverse(all(ans));
  rep(i, 0, N) printf("%d\\n", ans[i]);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_popcount(unsigned int x) { return __popcnt(x); }
#endif // _MSC_VER
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     @hamayanhamayan0
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

int N;
string X;
int cnt[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
  cin >> N >> X;

  if (N == 1) {
    if (X == "0")
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return;
  }
  reverse(all(X));

  rep(i, 1, 201010) {
    int pp = __builtin_popcount(i);
    cnt[i] = cnt[i % pp] + 1;
  }

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

  vector<int> ans;
  int tot1 = 0, p1 = 1;
  int tot2 = 0, p2 = 1;
  rep(i, 0, N) {
    if (X[i] == '1')
      tot1 = (tot1 + p1) % (cn + 1);
    p1 = (p1 * 2) % (cn + 1);

    if (2 <= cn) {
      if (X[i] == '1')
        tot2 = (tot2 + p2) % (cn - 1);
      p2 = (p2 * 2) % (cn - 1);
    }
  }

  p1 = 1;
  p2 = 1;
  rep(i, 0, N) {
    if (X[i] == '1') {
      if (cn == 1) {
        printf("0\\n");
      } else {
        int x = (tot2 - p2 + cn - 1) % (cn - 1);
        printf("%d\\n", cnt[x] + 1);
      }
    } else {
      int x = (tot1 + p1 + cn + 1) % (cn + 1);
      printf("%d\\n", cnt[x] + 1);
    }
    p1 = (p1 * 2) % (cn + 1);
    p2 = (p2 * 2) % (cn - 1);
  }
}