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

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

#define fa(x, v) for (auto x : v)
#define f(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define spc " "
#define endl '\\n'
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(s) (ll) int(s.length())
#define sz(v) (ll) int(v.size())
#define MOD 1000000007

void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
  freopen("debug.out", "w", stderr);
#endif
}

// Code Here

ll bit_ones(ll n) {
  ll ret = 0;
  while (n > 0) {
    if (n % 2)
      ret++;
    n /= 2;
  }
  return ret;
}

ll calc(ll x) {
  ll ret = 1;
  while (x > 0) {
    ll shitproblem = bit_ones(x);
    x %= shitproblem;
    ret++;
  }
  return ret;
}

int main() {
  Fast_IO();
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popcount = 0;
  f(i, 0, n - 1) if (s[i] == '1') popcount++;
  ll org_p1 = 0, org_m1 = 0;
  ll pow2_p1[n], pow2_m1[n];
  ll p1 = popcount + 1, m1 = popcount - 1;
  pow2_p1[0] = pow2_m1[0] = 1;
  f(i, 1, n - 1) {
    if (m1 > 0)
      pow2_m1[i] = (pow2_m1[i - 1] * 2) % m1;
    pow2_p1[i] = (pow2_p1[i - 1] * 2) % p1;
  }
  rf(i, n - 1, 0) {
    ll Power = n - 1 - i;
    if (s[i] == '1') {
      org_p1 = (org_p1 + pow2_p1[Power]) % p1;
      if (m1 > 0)
        org_m1 = (org_m1 + pow2_m1[Power]) % m1;
    }
  }
  f(i, 0, n - 1) {
    if (s[i] == '0') {
      ll xi = org_p1;
      ll Power = n - 1 - i;
      xi = (xi + pow2_p1[Power]) % p1;
      cout << calc(xi) << endl;
    } else {
      if (m1 == 0) {
        cout << 0 << endl;
        continue;
      }
      ll xi = org_m1;
      ll Power = n - 1 - i;
      xi = (xi - pow2_m1[Power] + m1) % m1;
      cout << calc(xi) << endl;
    }
  }
  return 0;
}


Error: WA

Fixed submission:

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

#define fa(x, v) for (auto x : v)
#define f(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define spc " "
#define endl '\\n'
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(s) (ll) int(s.length())
#define sz(v) (ll) int(v.size())
#define MOD 1000000007

void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
  freopen("debug.out", "w", stderr);
#endif
}

// Code Here

int main() {
  Fast_IO();
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popcount = 0;
  f(i, 0, n - 1) if (s[i] == '1') popcount++;
  ll org_p1 = 0, org_m1 = 0;
  ll pow2_p1[n], pow2_m1[n];
  ll p1 = popcount + 1, m1 = popcount - 1;
  pow2_p1[0] = pow2_m1[0] = 1;
  f(i, 1, n - 1) {
    pow2_m1[i] = (pow2_m1[i - 1] * 2) % m1;
    pow2_p1[i] = (pow2_p1[i - 1] * 2) % p1;
  }
  rf(i, n - 1, 0) {
    ll Power = n - 1 - i;
    if (s[i] == '1') {
      org_p1 = (org_p1 + pow2_p1[Power]) % p1;
      org_m1 = (org_m1 + pow2_m1[Power]) % m1;
    }
  }
  f(i, 0, n - 1) {
    if (s[i] == '0') {
      ll xi = org_p1;
      ll Power = n - 1 - i;
      xi = (xi + pow2_p1[Power]) % p1;
      if (xi == 0)
        cout << 1 << endl;
      else if (xi == 1)
        cout << 2 << endl;
      else if (xi % 2 == 0)
        cout << 2 << endl;
      else
        cout << 3 << endl;
    } else {
      ll xi = org_m1;
      ll Power = n - 1 - i;
      xi = (xi - pow2_m1[Power] + m1) % m1;
      if (xi == 0)
        cout << 1 << endl;
      else if (xi == 1)
        cout << 2 << endl;
      else if (xi % 2 == 0)
        cout << 2 << endl;
      else
        cout << 3 << endl;
    }
  }
  return 0;
}