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

#include <bits/stdc++.h>

#define MOD 1000000007LL
#define ll long long

using namespace std;

ll popcntandmod(ll n) {
  ll nn = n;
  ll m = 0;
  while (n > 0) {
    if ((n & 1) == 1)
      m++;
    n >>= 1;
  }
  return nn % m;
}

int main(void) {
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll pX = 0;
  for (int i = 0; i < N; i++)
    pX += (X[i] == '1' ? 1 : 0);
  vector<ll> m_plus(N), m_minus(N);

  ll b;
  b = 1;
  ll pM_plus = 0;
  if (pX < N) {
    for (int i = 0; i < N; i++) {
      m_plus[i] = b % (pX + 1);
      if (X[N - i - 1] == '1') {
        pM_plus += m_plus[i];
        pM_plus %= pX + 1;
      }
      b <<= 1;
      b %= pX + 1;
    }
  }
  b = 1;
  ll pM_minus = 0;
  if (1 < pX) {
    for (int i = 0; i < N; i++) {
      m_minus[i] = b % (pX - 1);
      if (X[N - i - 1] == '1') {
        pM_minus += m_minus[i];
        pM_minus %= pX - 1;
      }
      b <<= 1;
      b %= pX - 1;
    }
  }

  // for(int i = 0; i < N; i++) cout << m_plus[i] << " " << m_minus[i] << endl;

  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll X = pM_plus + m_plus[N - i - 1];
      X %= pX + 1;
      ll ans = 1;
      while (X != 0) {
        // cout << "\\t" << X << endl;
        ans++;
        X = popcntandmod(X);
      }
      cout << ans << endl;
    } else {
      ll X = pM_minus - m_minus[N - i - 1] + pX - 1;
      if (pX - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      X %= pX - 1;
      ll ans = 1;
      while (X != 0) {
        // cout << "\\t" << X << endl;
        ans++;
        X = popcntandmod(X);
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define MOD 1000000007LL
#define ll long long

using namespace std;

ll popcntandmod(ll n) {
  ll nn = n;
  ll m = 0;
  while (n > 0) {
    if (n & 1 == 1)
      m++;
    n >>= 1;
  }
  return nn % m;
}

int main(void) {
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll pX = 0;
  for (int i = 0; i < N; i++)
    pX += (X[i] == '1' ? 1 : 0);
  vector<ll> m_plus(N), m_minus(N);

  ll b;
  b = 1;
  ll pM_plus = 0;
  if (pX < N) {
    for (int i = 0; i < N; i++) {
      m_plus[i] = b % (pX + 1);
      if (X[N - i - 1] == '1') {
        pM_plus += m_plus[i];
        pM_plus %= pX + 1;
      }
      b <<= 1;
      b %= pX + 1;
    }
  }
  b = 1;
  ll pM_minus = 0;
  if (0 < pX) {
    for (int i = 0; i < N; i++) {
      m_minus[i] = b % (pX - 1);
      if (X[N - i - 1] == '1') {
        pM_minus += m_minus[i];
        pM_minus %= pX - 1;
      }
      b <<= 1;
      b %= pX - 1;
    }
  }

  // for(int i = 0; i < N; i++) cout << m_plus[i] << " " << m_minus[i] << endl;

  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll X = pM_plus + m_plus[N - i - 1];
      X %= pX + 1;
      ll ans = 1;
      while (X != 0) {
        // cout << "\\t" << X << endl;
        ans++;
        X = popcntandmod(X);
      }
      cout << ans << endl;
    } else {
      ll X = pM_minus - m_minus[N - i - 1] + pX - 1;
      X %= pX - 1;
      ll ans = 1;
      while (X != 0) {
        // cout << "\\t" << X << endl;
        ans++;
        X = popcntandmod(X);
      }
      cout << ans << endl;
    }
  }

  return 0;
}