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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx9[9] = {0, 1, 0, -1, 1, -1, 1, 0, -1};
int dy9[9] = {0, 1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;
// << fixed << setprecision
// --------------------------------------
int n;
string s;

int pcnt(int x) { return __builtin_popcount(x); }

int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> s;
  vector<int> x(n);
  REP(i, n) x[i] = s[i] - '0';
  int pc = 0;
  REP(i, n) if (s[i] == '1') pc++;

  vector<int> ans(n, 0);
  REP(i, 2) {
    int npc = pc;
    if (i == 0)
      npc++;
    else
      npc--;
    if (npc <= 0)
      continue;

    int r0 = 0;
    REP(j, n) r0 = (r0 * 2 + x[j]) % npc;

    int k = 1;
    RREP(j, n) {
      if (x[j] == i) {
        int r = r0;
        if (i == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[j] = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  REP(i, n) cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx9[9] = {0, 1, 0, -1, 1, -1, 1, 0, -1};
int dy9[9] = {0, 1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;
// << fixed << setprecision
// --------------------------------------
int n;
string s;

ll bigMod(string s, ll mod) {
  ll tmp = 0;
  FOR(i, 0, s.size()) { tmp = (tmp * 2 + s[i] - '0') % mod; }
  return tmp;
}

ll bit_count(ll t) {
  ll c = 0;
  while (t > 0) {
    if (t % 2)
      c++;
    t /= 2;
  }

  return c;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> s;

  int mod = 0;
  REP(i, n) if (s[i] == '1') mod++;

  ll p_mod = bigMod(s, mod + 1);
  ll n_mod = bigMod(s, mod - 1);

  REP(i, s.size()) {
    ll t_mod;
    ll t;
    string t_s(s.size(), '0');
    if (s[i] == ('1')) {
      t_s[i] = '1';
      t_mod = mod - 1;
      t = n_mod - bigMod(t_s, t_mod);
      if (t < 0)
        t += t_mod;
    } else {
      t_s[i] = '1';
      t_mod = mod + 1;
      t = (p_mod + bigMod(t_s, t_mod)) % t_mod;
    }

    int ans = 1;

    while (t > 0) {
      t = t % bit_count(t);
      ans++;
    }

    printf("%d\\n", ans);
  }
}