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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define INF (1LL << 50)
#define MOD 1000000007

ll popcount2(ll x) {
  ll ret = 0;
  while (x) {
    x &= x - 1;
    ++ret;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    rep(i, N) cout << 1 << endl;
    return 0;
  }
  vector<ll> amari_p(N), amari_m(N);
  ll num_p = 1;
  ll num_m = 1;
  ll cntp = cnt + 1;
  ll cntm = cnt - 1;
  for (int i = 0; i < N; i++) {
    amari_p[i] = num_p % cntp;
    num_p = (num_p % cntp) * 2;
    if (cntm <= 0)
      continue;
    amari_m[i] = num_m % cntm;
    num_m = (num_m % cntm) * 2;
  }
  reverse(amari_p.begin(), amari_p.end());
  reverse(amari_m.begin(), amari_m.end());
  // rep(i,N) cout << amari_p[i] << "-" << amari_m[i] << endl;
  // 元の数を割った余りを出しておく
  ll ori_p, ori_m;
  ll tmp_p = 0, tmp_m = 0;
  rep(i, N) {
    if (S[i] == '1') {
      tmp_p += amari_p[i];
      tmp_m += amari_m[i];
      tmp_p %= cntp;
      if (cntm <= 0)
        continue;
      tmp_m %= cntm;
    }
  }
  ori_p = tmp_p;
  ori_m = tmp_m;
  // cout << ori_p << "--" << ori_m << endl;

  for (int i = 0; i < N; i++) {
    ll ans = 0;
    string G = S;
    ll cnt2 = cnt;
    ll num;
    ll t_p = ori_p;
    ll t_m = ori_m;
    if (G[i] == '0') {
      t_p += amari_p[i];
      t_p %= cntp;
      num = t_p;
      ans++;
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      t_m -= amari_m[i];
      t_m += cntm;
      t_m %= cntm;
      num = t_m;
      ans++;
    }
    cnt2 = popcount2(num);
    while (num > 0) {
      num = num % cnt2;
      cnt2 = popcount2(num);
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define INF (1LL << 50)
#define MOD 1000000007

ll popcount2(ll x) {
  ll ret = 0;
  while (x) {
    x &= x - 1;
    ++ret;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    rep(i, N) cout << 1 << endl;
    return 0;
  }
  vector<ll> amari_p(N), amari_m(N);
  ll num_p = 1;
  ll num_m = 1;
  ll cntp = cnt + 1;
  ll cntm = cnt - 1;
  for (int i = 0; i < N; i++) {
    amari_p[i] = num_p % cntp;
    num_p = (num_p % cntp) * 2;
    if (cntm == 0)
      continue;
    amari_m[i] = num_m % cntm;
    num_m = (num_m % cntm) * 2;
  }
  reverse(amari_p.begin(), amari_p.end());
  reverse(amari_m.begin(), amari_m.end());
  // rep(i,N) cout << amari_p[i] << "-" << amari_m[i] << endl;
  // 元の数を割った余りを出しておく
  ll ori_p, ori_m;
  ll tmp_p = 0, tmp_m = 0;
  rep(i, N) {
    if (S[i] == '1') {
      tmp_p += amari_p[i];
      tmp_m += amari_m[i];
      tmp_p %= cntp;
      tmp_m %= cntm;
    }
  }
  ori_p = tmp_p;
  ori_m = tmp_m;
  // cout << ori_p << "-" << ori_m << endl;
  for (int i = 0; i < N; i++) {
    ll ans = 0;
    string G = S;
    ll cnt2 = cnt;
    ll num;
    ll t_p = ori_p;
    ll t_m = ori_m;
    if (G[i] == '0') {
      t_p += amari_p[i];
      t_p %= cntp;
      num = t_p;
      ans++;
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      t_m -= amari_m[i];
      t_m += cntm;
      t_m %= cntm;
      num = t_m;
      ans++;
    }
    cnt2 = popcount2(num);
    while (num > 0) {
      num = num % cnt2;
      cnt2 = popcount2(num);
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}