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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;

  vector<vector<ll>> R(N, vector<ll>(2, 0)); // cnt-1, cnt+1

  vector<char> X_reverse(N);
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
    X_reverse[N - 1 - i] = X[i];
  }

  ll num1 = 1, num2 = 1;
  if (cnt != 0 && cnt != 1)
    R[0][0] = num1 % (cnt - 1);
  R[0][1] = num2 % (cnt + 1);
  for (int i = 1; i < N; i++) {
    if (cnt != 0 && cnt != 1) {
      num1 = num1 * 2 % (cnt - 1);
      R[i][0] = num1;
    }
    num2 = num2 * 2 % (cnt + 1);
    R[i][1] = num2;
  }

  ll div1 = 0, div2 = 0;
  for (int i = 0; i < N; i++) {
    if (X_reverse[i] == '1') {
      if (cnt != 0 && cnt != 1)
        div1 = (div1 + R[i][0]) % (cnt - 1);
      div2 = (div2 + R[i][1]) % (cnt + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    ll now;
    int ans = 1;
    if (X[i] == '1' && cnt == 1) {
      ans = 0;
      now = 0;
    } else if (X[i] == '1') {
      now = (div1 + cnt - 1 - R[N - 1 - i][0]) % (cnt - 1);
    } else {
      now = (div2 + cnt + 1 + R[N - 1 - i][1]) % (cnt + 1);
    }
    while (now != 0) {
      ll tmp = now;
      int c = 0;
      while (tmp != 0) {
        if (tmp % 2 == 1)
          c++;
        tmp /= 2;
      }
      now %= c;
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;

  vector<vector<ll>> R(N, vector<ll>(2, 0)); // cnt-1, cnt+1

  vector<char> X_reverse(N);
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
    X_reverse[N - 1 - i] = X[i];
  }

  ll num1 = 1, num2 = 1;
  if (cnt != 0)
    R[0][0] = num1 % (cnt - 1);
  R[0][1] = num2 % (cnt + 1);
  for (int i = 1; i < N; i++) {
    if (cnt != 0) {
      num1 = num1 * 2 % (cnt - 1);
      R[i][0] = num1;
    }
    num2 = num2 * 2 % (cnt + 1);
    R[i][1] = num2;
  }

  ll div1 = 0, div2 = 0;
  for (int i = 0; i < N; i++) {
    if (X_reverse[i] == '1') {
      if (cnt != 0)
        div1 = (div1 + R[i][0]) % (cnt - 1);
      div2 = (div2 + R[i][1]) % (cnt + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    ll now;
    int ans = 1;
    if (X[i] == '1') {
      now = (div1 + cnt - 1 - R[N - 1 - i][0]) % (cnt - 1);
    } else {
      now = (div2 + cnt + 1 + R[N - 1 - i][1]) % (cnt + 1);
    }
    while (now != 0) {
      ll tmp = now;
      int c = 0;
      while (tmp != 0) {
        if (tmp % 2 == 1)
          c++;
        tmp /= 2;
      }
      now %= c;
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}