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

#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;

int calc_pcnt(int val) {
  int res = 0;
  while (val != 0) {
    if ((val & 1) == 1) {
      res++;
    }
    val = val >> 1;
  }
  return res;
}

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

  int32_t N;
  cin >> N;
  string X;
  cin >> X;
  int32_t bc = 0;
  for (int i = 0; i < (int)X.size(); i++) {
    if (X[i] == '1') {
      bc++;
    }
  }

  vector<vector<int32_t>> mod_vect(N, vector<int32_t>(2, 0));
  mod_vect[0][0] = bc > 1 ? 1 % (bc - 1) : -1;
  mod_vect[0][1] = 1 % (bc + 1);
  for (int shift = 1; shift < N; shift++) {
    mod_vect[shift][1] = (mod_vect[shift - 1][1] << 1) % (bc + 1);
    if (bc > 1) {
      mod_vect[shift][0] = (mod_vect[shift - 1][0] << 1) % (bc - 1);
    }
  }

  int32_t base_1st_modp = 0;
  int32_t cur_val = 0;
  int32_t idx = 0;
  while (idx < N && X[idx] == '0')
    idx++;
  while (idx < N) {
    cur_val = (cur_val << 1);
    if (X[idx] == '1') {
      cur_val += 1;
    }
    if (cur_val >= bc + 1) {
      // cout << "cur_val:" << cur_val << ", idx:" << idx << endl;
      cur_val %= (bc + 1);
    }
    idx++;
  }
  base_1st_modp = cur_val;
  // cout << "base modp: " << cur_val << endl;

  int32_t base_1st_modm = 0;
  if (bc > 1) {
    cur_val = 0;
    idx = 0;
    while (idx < N && X[idx] == '0')
      idx++;
    while (idx < N) {
      cur_val = (cur_val << 1);
      if (X[idx] == '1') {
        cur_val += 1;
      }
      if (cur_val >= bc - 1) {
        cur_val %= (bc - 1);
      }
      idx++;
    }
    base_1st_modm = cur_val;
  }

  // cout << "base modm: " << cur_val << endl;

  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      cur_val = base_1st_modp + mod_vect[N - i - 1][1];
      cur_val %= (bc + 1);
    } else {
      if (bc > 1) {
        cur_val = base_1st_modm - mod_vect[N - i - 1][0];
        cur_val %= (bc - 1);
        if (cur_val < 0)
          cur_val += bc - 1;
      } else {
        cout << 0 << endl;
        continue;
      }
    }
    int32_t cnt = 1;
    int32_t pcnt = 0;
    while (cur_val != 0) {
      pcnt = calc_pcnt(cur_val);
      cur_val %= pcnt;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;

int calc_pcnt(int val) {
  int res = 0;
  while (val != 0) {
    if ((val & 1) == 1) {
      res++;
    }
    val = val >> 1;
  }
  return res;
}

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

  int32_t N;
  cin >> N;
  string X;
  cin >> X;
  int32_t bc = 0;
  for (int i = 0; i < (int)X.size(); i++) {
    if (X[i] == '1') {
      bc++;
    }
  }

  vector<vector<int32_t>> mod_vect(N, vector<int32_t>(2, 0));
  mod_vect[0][0] = bc > 1 ? 1 % (bc - 1) : -1;
  mod_vect[0][1] = 1 % (bc + 1);
  for (int shift = 1; shift < N; shift++) {
    mod_vect[shift][1] = (mod_vect[shift - 1][1] << 1) % (bc + 1);
    if (bc > 1) {
      mod_vect[shift][0] = (mod_vect[shift - 1][0] << 1) % (bc - 1);
    }
  }

  int32_t base_1st_modp = 0;
  int32_t cur_val = 0;
  int32_t idx = 0;
  while (idx < N && X[idx] == '0')
    idx++;
  while (idx < N) {
    cur_val = (cur_val << 1);
    if (X[idx] == '1') {
      cur_val += 1;
    }
    if (cur_val >= bc + 1) {
      // cout << "cur_val:" << cur_val << ", idx:" << idx << endl;
      cur_val %= (bc + 1);
    }
    idx++;
  }
  base_1st_modp = cur_val;
  // cout << "base modp: " << cur_val << endl;

  int32_t base_1st_modm = 0;
  cur_val = 0;
  idx = 0;
  while (idx < N && X[idx] == '0')
    idx++;
  while (idx < N) {
    cur_val = (cur_val << 1);
    if (X[idx] == '1') {
      cur_val += 1;
    }
    if (cur_val >= bc - 1) {
      cur_val %= (bc - 1);
    }
    idx++;
  }
  base_1st_modm = cur_val;

  // cout << "base modm: " << cur_val << endl;

  for (int i = 0; i < N; i++) {
    int32_t cnt = 1;
    if (X[i] == '0') {
      cur_val = base_1st_modp + mod_vect[N - i - 1][1];
      cur_val %= (bc + 1);
    } else {
      cur_val = base_1st_modm - mod_vect[N - i - 1][0];
      cur_val %= (bc - 1);
      if (cur_val < 0)
        cur_val += bc - 1;
    }
    int32_t pcnt = 0;
    while (cur_val != 0) {
      pcnt = calc_pcnt(cur_val);
      cur_val %= pcnt;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}