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

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
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;
}
using namespace std;

ll Count(ll n) {
  ll cnt = 0;
  while (n) {
    n = n % __builtin_popcount(n);
    cnt++;
  }
  return cnt;
}

int main() {
  Init();
  ll n, bitCnt = 0;
  string s;
  cin >> n >> s;
  rep(i, n) if (s[i] == '1') bitCnt++;

  ll a = bitCnt + 1;
  ll b = bitCnt - 1;
  vi p(n);
  p[n - 1] = 1;
  vi q(n);
  q[n - 1] = 1;
  for (ll i = n - 2; i >= 0; i--) {
    if (a)
      p[i] = p[i + 1] * 2 % a;
    if (b)
      q[i] = q[i + 1] * 2 % b;
  }
  ll sp = 0, sm = 0;
  rep(i, n) if (s[i] == '1') sp += p[i], sm += q[i];

  rep(i, n) {
    if (s[i] == '1') {
      // 反転する箇所が1の場合
      if (b <= 0)
        cout << 0 << endl;
      else
        cout << (1 + Count((sm - q[i] + b) % b)) << endl;
    } else {
      // 反転する箇所が0の場合
      cout << (1 + Count((sp + p[i]) % a)) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
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;
}
using namespace std;

ll Count(ll n) {
  ll cnt = 0;
  while (n) {
    n = n % __builtin_popcount(n);
    cnt++;
  }
  return cnt;
}

int main() {
  Init();
  ll n, bitCnt = 0;
  string s;
  cin >> n >> s;
  rep(i, n) if (s[i] == '1') bitCnt++;

  ll a = bitCnt + 1;
  ll b = bitCnt - 1;
  vi p(n);
  p[n - 1] = 1;
  vi q(n);
  q[n - 1] = 1;
  for (ll i = n - 2; i >= 0; i--) {
    if (a)
      p[i] = p[i + 1] * 2 % a;
    if (b)
      q[i] = q[i + 1] * 2 % b;
  }
  ll sp = 0, sm = 0;
  rep(i, n) if (s[i] == '1') sp += p[i], sm += q[i];
  sp %= a;
  sm %= b;

  rep(i, n) {
    if (s[i] == '1') {
      // 反転する箇所が1の場合
      if (b <= 0)
        cout << 0 << endl;
      else
        cout << (1 + Count((sm - q[i] + b) % b)) << endl;
    } else {
      // 反転する箇所が0の場合
      cout << (1 + Count((sp + p[i]) % a)) << endl;
    }
  }
}