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

// #pragma warning(disable:4996)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>
// #include <stdio.h>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e15;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

int popcount(int N) {
  int ans = 0;
  int n = 0;
  while ((N >> n) > 0) {
    ans += (N >> n) & 1;
    n++;
  }
  return ans;
}

int calc(int N) {
  int ans = 0;
  while (N > 0) {
    int p = popcount(N);
    N %= p;
    ans++;
  }
  return ans;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N;
  cin >> N;
  string S;
  VI X(N);
  char chr;
  cin.get(chr);
  for (int n = 0; n < N; n++) {
    cin.get(chr);
    // int c = (S[n] == '0' ? 0 : 1);
    X[N - 1 - n] = chr - 48;
  }
  int N0 = 0;
  for (int n = 0; n < N; n++)
    N0 += X[n];
  VI bnp(N, 0);
  bnp[0] = 1 % (N0 + 1);
  for (int n = 1; n < N; n++) {
    bnp[n] = (bnp[n - 1] * 2) % (N0 + 1);
  }
  VI bnm(N, 0);
  if (N0 - 1 != 0) {
    bnm[0] = 1 % (N0 - 1);
    for (int n = 1; n < N; n++) {
      bnm[n] = (bnm[n - 1] * 2) % (N0 - 1);
    }
  }
  int xnp = 0;
  for (int n = 0; n < N; n++) {
    if (X[n] == 1) {
      xnp += bnp[n];
      xnp %= (N0 + 1);
    }
  }
  int xnm = 0;
  if (N0 > 1) {
    for (int n = 0; n < N; n++) {
      if (X[n] == 1) {
        xnm += bnm[n];
        xnm %= (N0 - 1);
      }
    }
  }
  for (int n = N - 1; n >= 0; n--) {
    if (X[n] == 0) {
      int XX = (xnp + bnp[n]) % (N0 + 1);
      cout << calc(XX) + 1 << "\\n";
    } else {
      if (N0 == 1) {
        cout << 0 << "\\n";
        continue;
      }
      int XX = (xnm - bnm[n]) % (N0 - 1);
      XX = (XX + N0 - 1) % (N0 - 1);
      cout << calc(XX) + 1 << "\\n";
    }
  }
  return 0;
}


Error: 0

Fixed submission:

// #pragma warning(disable:4996)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>
// #include <stdio.h>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e15;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

int popcount(int N) {
  int ans = 0;
  int n = 0;
  while ((N >> n) > 0) {
    ans += (N >> n) & 1;
    n++;
  }
  return ans;
}

int calc(int N) {
  int ans = 0;
  while (N > 0) {
    int p = popcount(N);
    N %= p;
    ans++;
  }
  return ans;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N;
  cin >> N;
  string S;
  VI X(N);
  char chr;
  cin.get(chr);
  for (int n = 0; n < N; n++) {
    cin.get(chr);
    // int c = (S[n] == '0' ? 0 : 1);
    X[N - 1 - n] = chr - 48;
  }
  int N0 = 0;
  for (int n = 0; n < N; n++)
    N0 += X[n];
  VI bnp(N, 0);
  bnp[0] = 1 % (N0 + 1);
  for (int n = 1; n < N; n++) {
    bnp[n] = (bnp[n - 1] * 2) % (N0 + 1);
  }
  VI bnm(N, 0);
  if (N0 - 1 != 0) {
    bnm[0] = 1 % (N0 - 1);
    for (int n = 1; n < N; n++) {
      bnm[n] = (bnm[n - 1] * 2) % (N0 - 1);
    }
  }
  int xnp = 0;
  for (int n = 0; n < N; n++) {
    if (X[n] == 1) {
      xnp += bnp[n];
      xnp %= (N0 + 1);
    }
  }
  int xnm = 0;
  if (N0 > 1) {
    for (int n = 0; n < N; n++) {
      if (X[n] == 1) {
        xnm += bnm[n];
        xnm %= (N0 - 1);
      }
    }
  }
  for (int n = N - 1; n >= 0; n--) {
    if (X[n] == 0) {
      int XX = (xnp + bnp[n]) % (N0 + 1);
      cout << calc(XX) + 1 << "\\n";
    } else {
      int XX = (xnm - bnm[n]) % (N0 - 1);
      XX = (XX + N0 - 1) % (N0 - 1);
      cout << calc(XX) + 1 << "\\n";
    }
  }
  return 0;
}