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

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, n, m) for (ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair<ll, ll> P;
static const ll dx[8] = {0, 1, 0, -1, 1, -1, 1, -1},
                dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i)
    os << v[i] << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (auto p : m)
    os << "<" << p.first << ", " << p.second << "> ";
  return os;
}

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll pop_cnt = 0;
  REP(i, N) {
    if (X[i] == '1')
      pop_cnt++;
  }

  vector<ll> plus_pows(N);
  plus_pows[0] = 1 % (pop_cnt + 1);
  REP(i, N - 1) { plus_pows[i + 1] = (plus_pows[i] * 2) % (pop_cnt + 1); }
  ll plus_tmp = 0;
  REP(i, N) {
    if (X[i] == '1') {
      plus_tmp += plus_pows[N - 1 - i];
      plus_tmp %= (pop_cnt + 1);
    }
  }
  if (pop_cnt == 0) {
    REP(i, N) { PRINT(1); }
    return 0;
  }
  if (pop_cnt == 1) {
    REP(i, N) {
      ll ans = 1;
      if (X[i] == '0') {
        unsigned long long t = plus_tmp;
        t += plus_pows[N - 1 - i];
        t %= (pop_cnt + 1);
        while (t != 0) {
          ll pcnt = __builtin_popcountll(t);
          t %= pcnt;
          ans++;
        }
        PRINT(ans);
      } else {
        PRINT(0);
      }
    }
    return 0;
  }
  vector<ll> minus_pows(N);
  minus_pows[0] = 1;
  REP(i, N - 1) { minus_pows[i + 1] = (minus_pows[i] * 2) % (pop_cnt - 1); }
  ll minus_tmp = 0;
  REP(i, N) {
    if (X[i] == '1') {
      minus_tmp += minus_pows[N - 1 - i];
      minus_tmp %= (pop_cnt - 1);
    }
  }
  REP(i, N) {
    ll ans = 0;
    if (X[i] == '1') {
      ll tmp = minus_tmp;
      tmp -= minus_pows[N - 1 - i];
      if (tmp < 0)
        tmp += (pop_cnt - 1);
      ans++;
      unsigned long long t = tmp;
      while (t != 0) {
        ll pcnt = __builtin_popcountll(t);
        t %= pcnt;
        ans++;
      }
    } else {
      ll tmp = plus_tmp;
      tmp += plus_pows[N - 1 - i];
      tmp %= (pop_cnt + 1);
      ans++;
      unsigned long long t = tmp;
      while (t != 0) {
        ll pcnt = __builtin_popcountll(t);
        t %= pcnt;
        ans++;
      }
    }
    PRINT(ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, n, m) for (ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair<ll, ll> P;
static const ll dx[8] = {0, 1, 0, -1, 1, -1, 1, -1},
                dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i)
    os << v[i] << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (auto p : m)
    os << "<" << p.first << ", " << p.second << "> ";
  return os;
}

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll pop_cnt = 0;
  REP(i, N) {
    if (X[i] == '1')
      pop_cnt++;
  }
  vector<ll> plus_pows(N);
  vector<ll> minus_pows(N);
  plus_pows[0] = 1;
  minus_pows[0] = 1;
  REP(i, N - 1) {
    plus_pows[i + 1] = (plus_pows[i] * 2) % (pop_cnt + 1);
    minus_pows[i + 1] = (minus_pows[i] * 2) % (pop_cnt - 1);
  }
  ll plus_tmp = 0;
  ll minus_tmp = 0;
  REP(i, N) {
    if (X[i] == '1') {
      plus_tmp += plus_pows[N - 1 - i];
      plus_tmp %= (pop_cnt + 1);
      minus_tmp += minus_pows[N - 1 - i];
      minus_tmp %= (pop_cnt - 1);
    }
  }
  REP(i, N) {
    ll ans = 0;
    if (X[i] == '1') {
      ll tmp = minus_tmp;
      tmp -= minus_pows[N - 1 - i];
      if (tmp < 0)
        tmp += (pop_cnt - 1);
      ans++;
      unsigned long long t = tmp;
      while (t != 0) {
        ll pcnt = __builtin_popcountll(t);
        t %= pcnt;
        ans++;
      }
    } else {
      ll tmp = plus_tmp;
      tmp += plus_pows[N - 1 - i];
      tmp %= (pop_cnt + 1);
      ans++;
      unsigned long long t = tmp;
      while (t != 0) {
        ll pcnt = __builtin_popcountll(t);
        t %= pcnt;
        ans++;
      }
    }
    PRINT(ans);
  }
  return 0;
}