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

#include "bits/stdc++.h"
using namespace std;
using ll = unsigned long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------

ll N;
string S;
ll pc;
ll x;
ll rankarr[201010];

ll f(ll n) {
  ll ans = 0;
  while (n > 0) {
    bitset<64> bs(n);
    n %= bs.count();
    ++ans;
  }
  return ans;
}

int main() {
  //
  cin >> N;
  cin >> S;
  for (auto c : S) {
    if (c == '1') {
      ++pc;
    }
  }

  ll pci = pc + 1;
  ll pce = pc;
  ll pcd = pc - 1;

  ll mod;
  if (pcd == 0) {
    mod = pci;
  } else {
    mod = pci * pcd;
  }

  ll rank = 1;
  for (int i = S.size() - 1; i >= 0; --i) {
    if (S[i] == '1') {
      x += rank;
      x %= mod;
    }
    rankarr[i] = rank;

    rank *= 2;
    rank %= mod;
  }

  for (int i = 0; i < S.size(); ++i) {
    ll xi;
    if (S[i] == '0') {
      xi = (x + rankarr[i]) % mod;
      xi %= pci;
    } else {
      xi = (x + mod - rankarr[i]) % mod;

      if (pcd == 0) {

        cout << 0 << "\\n";
        continue;
      } else
        xi %= pcd;
    }
    cout << f(xi) + 1 << "\\n";
  }
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = unsigned long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------

ll N;
string S;
ll pc;
ll x;
ll rankarr[201010];

ll f(ll n) {
  ll ans = 0;
  while (n > 0) {
    bitset<64> bs(n);
    n %= bs.count();
    ++ans;
  }
  return ans;
}

int main() {
  //
  cin >> N;
  cin >> S;
  for (auto c : S) {
    if (c == '1') {
      ++pc;
    }
  }

  ll pci = pc + 1;
  ll pce = pc;
  ll pcd = pc - 1;

  ll mod = pci * pcd * pcd;

  ll rank = 1;
  for (int i = S.size() - 1; i >= 0; --i) {
    if (S[i] == '1') {
      x += rank;
      x %= mod;
    }
    rankarr[i] = rank;

    rank *= 2;
    rank %= mod;
  }

  for (int i = 0; i < S.size(); ++i) {
    ll xi;
    if (S[i] == '0') {
      xi = (x + rankarr[i]) % mod;
      xi %= pci;
    } else {
      xi = (x + mod - rankarr[i]) % mod;

      if (pcd == 0) {

        cout << 0 << "\\n";
        continue;
      } else
        xi %= pcd;
    }
    cout << f(xi) + 1 << "\\n";
  }
}