Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IFOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define RFOR(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR((i), 0, (n))

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int calc(ll x) {
  if (x == 0)
    return 0;
  int cnt = 0;
  ll tmp = x;
  while (tmp > 0) {
    if (tmp & 1)
      cnt++;
    tmp >>= 1;
  }

  return calc(x % cnt) + 1;
}
int calc(string x, int cnt, int bit, vector<ll> &p2_plus, vector<ll> &p2_minus,
         ll base_plus, ll base_minus) {
  ll now;
  if (x[bit] == '0') {
    cnt++;
    now = (base_plus + p2_plus[x.size() - 1 - bit]) % cnt;
  } else {
    cnt--;
    if (cnt == 0)
      return 0;
    now = (base_minus + (cnt - p2_minus[x.size() - 1 - bit])) % cnt;
  }

  return calc(now) + 1;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int popcount = count(x.begin(), x.end(), '1');

  vector<ll> p2_minus(n + 1, 1);
  vector<ll> p2_plus(n + 1, 1);
  IFOR(i, 1, n) {
    p2_plus[i] = (p2_plus[i - 1] * 2) % (popcount + 1);
    if (popcount >= 2)
      p2_minus[i] = (p2_minus[i - 1] * 2) % (popcount - 1);
  }

  ll base_plus = 0;
  ll base_minus = 0;
  REP(i, x.size()) {
    base_plus *= 2;
    base_minus *= 2;
    int toadd = x[i] - '0';
    base_plus = (base_plus + toadd) % (popcount + 1);
    base_minus = (base_minus + toadd) % (popcount - 1);
  }

  REP(i, n) {
    cout << calc(x, popcount, i, p2_plus, p2_minus, base_plus, base_minus)
         << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IFOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define RFOR(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR((i), 0, (n))

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int calc(ll x) {
  if (x == 0)
    return 0;
  int cnt = 0;
  ll tmp = x;
  while (tmp > 0) {
    if (tmp & 1)
      cnt++;
    tmp >>= 1;
  }

  return calc(x % cnt) + 1;
}
int calc(string x, int cnt, int bit, vector<ll> &p2_plus, vector<ll> &p2_minus,
         ll base_plus, ll base_minus) {
  ll now;
  if (x[bit] == '0') {
    cnt++;
    now = (base_plus + p2_plus[x.size() - 1 - bit]) % cnt;
  } else {
    cnt--;
    if (cnt == 0)
      return 0;
    now = (base_minus + (cnt - p2_minus[x.size() - 1 - bit])) % cnt;
  }

  return calc(now) + 1;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int popcount = count(x.begin(), x.end(), '1');

  vector<ll> p2_minus(n + 1, 1);
  vector<ll> p2_plus(n + 1, 1);
  IFOR(i, 1, n) {
    p2_plus[i] = (p2_plus[i - 1] * 2) % (popcount + 1);
    if (popcount >= 2)
      p2_minus[i] = (p2_minus[i - 1] * 2) % (popcount - 1);
  }

  ll base_plus = 0;
  ll base_minus = 0;
  REP(i, x.size()) {
    base_plus *= 2;
    base_minus *= 2;
    int toadd = x[i] - '0';
    base_plus = (base_plus + toadd) % (popcount + 1);
    if (popcount >= 2)
      base_minus = (base_minus + toadd) % (popcount - 1);
  }

  REP(i, n) {
    cout << calc(x, popcount, i, p2_plus, p2_minus, base_plus, base_minus)
         << endl;
  }

  return 0;
}