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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<db, db> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rb(i, x) for (auto &i : x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define UNIQUE(x) (x).resize(unique(all(x)) - begin(x))

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define F first
#define S second

#define pow10(n) stoll("1" + string(n, '0'))

#define debug(x) cout << ">>> " << #x << ": " << x << '\\n'

const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LNF = 1e18;

void setIO(string s) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen((s + ".in").c_str(), "r", stdin);
//	freopen((s+".out").c_str(),"w",stdout);
#endif
}

int main() {

  setIO("input");
  int n;
  str s;
  cin >> n >> s;
  int pc = count(all(s), '1');
  int cnt_minus = 0, cnt_plus = 0;
  for (int i = 0; i < n; ++i) {
    if (pc > 1)
      cnt_plus = (cnt_plus * 2 + s[i] - '0') % (pc - 1);
    cnt_minus = (cnt_minus * 2 + s[i] - '0') % (pc + 1);
  }

  vi ans(n);
  int base_plus = 1, base_minus = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1' && pc == 1)
      continue;
    int num = s[i] == '1' ? (cnt_plus - base_plus + pc - 1) % (pc - 1)
                          : (cnt_minus + base_minus) % (pc + 1);
    ++ans[i];
    while (num > 0) {
      num %= __builtin_popcount(num);
      ++ans[i];
    }

    if (pc > 1)
      base_plus = base_plus * 2 % (pc - 1);
    base_minus = base_minus * 2 % (pc + 1);
  }

  for (auto &i : ans)
    cout << i << '\\n';
}

/*  ! CHECK THIS IF YOU GET IT WRONG !
 * int overflow, array bounds, NO VLAs
 * uninitialized local variables, rounding errors
 * intermediate result type, scientific
 * special cases (n = 0 or 1?)
 * reset your variables in scope
 * INF not large enough for ans, adding value to _MAX
 * using wrong variables, sorting extra 0s in global array
 * comment out input from file
 * operator precedence
 */

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<db, db> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rb(i, x) for (auto &i : x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define UNIQUE(x) (x).resize(unique(all(x)) - begin(x))

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define F first
#define S second

#define pow10(n) stoll("1" + string(n, '0'))

#define debug(x) cout << ">>> " << #x << ": " << x << '\\n'

const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LNF = 1e18;

void setIO(string s) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen((s + ".in").c_str(), "r", stdin);
//	freopen((s+".out").c_str(),"w",stdout);
#endif
}

ll fast_exp(int base, int exp, int mod) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    exp /= 2;
  }
  return res % mod;
}

int main() {

  setIO("input");
  int n;
  str s;
  cin >> n >> s;
  int pc = 0;
  for (auto &i : s)
    if (i == '1')
      ++pc;
  int zero = 0;
  for (int i = 0; i < sz(s); ++i)
    zero = (zero * 2 + (s[i] - '0')) % (pc + 1);
  int one = 0;
  for (int i = 0; i < sz(s); ++i)
    one = (one * 2 + (s[i] - '0')) % (pc - 1);

  for (int i = 0; i < sz(s); ++i) {
    int num;
    if (s[i] == '1' && pc == 1) {
      cout << 0 << '\\n';
      continue;
    } else if (s[i] == '1') {
      num = one - fast_exp(2, n - i - 1, pc - 1);
      num = (num + pc - 1) % (pc - 1);
    } else {
      num = zero + fast_exp(2, n - i - 1, pc + 1);
      num %= pc + 1;
    }
    int cnt = 1;
    while (num != 0) {
      num = num % __builtin_popcount(num);
      ++cnt;
    }
    cout << cnt << '\\n';
  }
}

/*  ! CHECK THIS IF YOU GET IT WRONG !
 * int overflow, array bounds, NO VLAs
 * uninitialized local variables, rounding errors
 * intermediate result type, scientific
 * special cases (n = 0 or 1?)
 * reset your variables in scope
 * INF not large enough for ans, adding value to _MAX
 * using wrong variables, sorting extra 0s in global array
 * comment out input from file
 * operator precedence
 */