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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

vector<ll> bitconversion(ll i, ll n, ll N) {
  ll x = 1;
  rep(j, N) { x *= n; }
  vector<ll> vec(N);
  rep(j, N) {
    x /= n;
    vec[j] = i / x;
    i -= x * vec[j];
  }
  return vec;
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> popcount(200004, 0);
  for (ll i = 1; i <= 200003; i++) {
    vector<ll> binary = bitconversion(i, 2, 20);
    ll count = 0;
    rep(j, 20) {
      if (binary[j] == 1)
        count++;
    }
    popcount[i] = count;
  }
  vector<ll> popcount_time(200004, 0);
  popcount_time[1] = 1;
  popcount_time[2] = 1;
  popcount_time[3] = 2;
  popcount_time[4] = 1;
  popcount_time[5] = 2;
  popcount_time[6] = 1;
  popcount_time[7] = 2;
  popcount_time[8] = 1;
  popcount_time[9] = 2;
  popcount_time[10] = 1;
  popcount_time[11] = 2;
  popcount_time[12] = 1;
  popcount_time[13] = 2;
  popcount_time[14] = 2;
  popcount_time[15] = 3;
  popcount_time[16] = 1;
  popcount_time[17] = 2;
  popcount_time[18] = 1;
  popcount_time[19] = 2;
  popcount_time[20] = 1;
  for (ll i = 21; i <= 200003; i++) {
    popcount_time[i] = (popcount_time[i % popcount[i]]) + 1;
  }
  ll cnt = 0;
  rep(i, N) {
    if (X[i] == '1')
      cnt++;
  }
  ll cnt_minus = cnt - 1;
  ll cnt_plus = cnt + 1;
  if (cnt_minus == 0)
    cnt_minus = 2;
  vector<ll> bit_minus(N, 0);
  vector<ll> bit_plus(N, 0);
  ll num_minus = 1;
  rep(i, N) {
    bit_minus[N - i - 1] = num_minus % cnt_minus;
    num_minus = (num_minus * 2) % cnt_minus;
  }
  ll num_minus_all = 0;
  rep(i, N) {
    if (X[i] == '1')
      num_minus_all = (num_minus_all + bit_minus[i]) % cnt_minus;
  }
  ll num_plus = 1;
  rep(i, N) {
    bit_plus[N - 1 - i] = num_plus % cnt_plus;
    num_plus = (num_plus * 2) % cnt_plus;
  }
  ll num_plus_all = 0;
  rep(i, N) {
    if (X[i] == '1')
      num_plus_all = (num_plus_all + bit_plus[i]) % cnt_plus;
  }
  /*rep(i,N){
      if (X[i] == '1') cout << (num_minus_all + cnt_minus - bit_minus[i]) %
  cnt_minus << endl; else cout << (num_plus_all + cnt_plus + bit_plus[i]) %
  cnt_plus << endl;
  }*/
  rep(i, N) {
    if (X[i] == '1') {
      if (cnt_plus == 2)
        cout << 0 << endl;
      else
        cout << popcount_time[(num_minus_all + cnt_minus * 1000000 -
                               bit_minus[i]) %
                              cnt_minus] +
                    1
             << endl;
    } else {
      cout
          << popcount_time[(num_plus_all + cnt_plus + bit_plus[i]) % cnt_plus] +
                 1
          << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

vector<ll> bitconversion(ll i, ll n, ll N) {
  ll x = 1;
  rep(j, N) { x *= n; }
  vector<ll> vec(N);
  rep(j, N) {
    x /= n;
    vec[j] = i / x;
    i -= x * vec[j];
  }
  return vec;
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> popcount(200004, 0);
  for (ll i = 1; i <= 200003; i++) {
    vector<ll> binary = bitconversion(i, 2, 20);
    ll count = 0;
    rep(j, 20) {
      if (binary[j] == 1)
        count++;
    }
    popcount[i] = count;
  }
  vector<ll> popcount_time(200004, 0);
  popcount_time[1] = 1;
  popcount_time[2] = 1;
  popcount_time[3] = 2;
  popcount_time[4] = 1;
  popcount_time[5] = 2;
  popcount_time[6] = 1;
  popcount_time[7] = 2;
  popcount_time[8] = 1;
  popcount_time[9] = 2;
  popcount_time[10] = 1;
  popcount_time[11] = 2;
  popcount_time[12] = 1;
  popcount_time[13] = 2;
  popcount_time[14] = 2;
  popcount_time[15] = 3;
  popcount_time[16] = 1;
  popcount_time[17] = 2;
  popcount_time[18] = 1;
  popcount_time[19] = 2;
  popcount_time[20] = 1;
  for (ll i = 21; i <= 200003; i++) {
    popcount_time[i] = (popcount_time[i % popcount[i]]) + 1;
  }
  ll cnt = 0;
  rep(i, N) {
    if (X[i] == '1')
      cnt++;
  }
  ll cnt_minus = cnt - 1;
  ll cnt_plus = cnt + 1;
  vector<ll> bit_minus(N, 0);
  vector<ll> bit_plus(N, 0);
  ll num_minus = 1;
  rep(i, N) {
    bit_minus[N - i - 1] = num_minus % cnt_minus;
    num_minus = (num_minus * 2) % cnt_minus;
  }
  ll num_minus_all = 0;
  rep(i, N) {
    if (X[i] == '1')
      num_minus_all = (num_minus_all + bit_minus[i]) % cnt_minus;
  }
  ll num_plus = 1;
  rep(i, N) {
    bit_plus[N - 1 - i] = num_plus % cnt_plus;
    num_plus = (num_plus * 2) % cnt_plus;
  }
  ll num_plus_all = 0;
  rep(i, N) {
    if (X[i] == '1')
      num_plus_all = (num_plus_all + bit_plus[i]) % cnt_plus;
  }
  /*rep(i,N){
      if (X[i] == '1') cout << (num_minus_all + cnt_minus - bit_minus[i]) %
  cnt_minus << endl; else cout << (num_plus_all + cnt_plus + bit_plus[i]) %
  cnt_plus << endl;
  }*/
  rep(i, N) {
    if (X[i] == '1') {
      cout << popcount_time[(num_minus_all + cnt_minus * 1000000 -
                             bit_minus[i]) %
                            cnt_minus] +
                  1
           << endl;
    } else {
      cout
          << popcount_time[(num_plus_all + cnt_plus + bit_plus[i]) % cnt_plus] +
                 1
          << endl;
    }
  }
}