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

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll N;
  cin >> N;
  string X;
  cin >> X;

  ll pc = 0;
  for (int i = 0; i < N; i++) {
    if (X.at(i) == '1')
      pc++;
  }

  if (pc == 0) {
    for (int i = 0; i < N; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  if (pc == 1) {
    if (X.at(N - 1) == '0') {
      for (int i = 0; i < N - 1; i++) {
        if (X.at(i) == '0') {
          cout << 1 << endl;
        } else
          cout << 0 << endl;
      }
      cout << 2 << endl;
    } else {
      for (int i = 0; i < N - 1; i++) {
        cout << 2 << endl;
      }
      cout << 0 << endl;
    }
    return 0;
  }

  map<ll, ll> mp;
  mp[0] = 0;
  for (int i = 1; i <= 2 * 100000; i++) {
    ll popc = __builtin_popcount(i);
    if (i % popc == 0)
      mp[i] = 1;
    else {
      mp[i] = 1 + mp[i % popc];
    }
  }
  ll ppc = pc - 1, mpc = pc + 1;
  vll one(N, -1), zero(N, -1);

  ll psum = 0, msum = 0;
  ll tmp = 1LL;
  for (int i = 0; i < N; i++) {
    one.at(N - 1 - i) = tmp % ppc;
    tmp *= 2LL;
    tmp %= ppc;
    if (X.at(N - 1 - i) == '1')
      psum += one.at(N - 1 - i);
    psum = (psum + ppc) % ppc;
  }
  tmp = 1LL;
  for (int i = 0; i < N; i++) {
    zero.at(N - 1 - i) = tmp % mpc;
    tmp *= 2LL;
    tmp %= mpc;
    if (X.at(N - 1 - i) == '1')
      msum += zero.at(N - 1 - i);
    msum = (msum + mpc) % mpc;
  }

  for (int i = 0; i < N; i++) {
    if (X.at(i) == '1') {
      ll num = psum - one.at(i);
      num = (num + ppc) % ppc;
      cout << mp[num] + 1 << endl;
    } else {
      ll num = msum + zero.at(i);
      num = (num + mpc) % mpc;
      cout << mp[num] + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll N;
  cin >> N;
  string X;
  cin >> X;

  ll pc = 0;
  for (int i = 0; i < N; i++) {
    if (X.at(i) == '1')
      pc++;
  }

  if (pc == 0) {
    for (int i = 0; i < N; i++) {
      cout << 1 << endl;
    }
  }
  if (pc == 1) {
    if (X.at(N - 1) == '0') {
      for (int i = 0; i < N - 1; i++) {
        if (X.at(i) == '0') {
          cout << 1 << endl;
        } else
          cout << 0 << endl;
      }
      cout << 2 << endl;
    } else {
      for (int i = 0; i < N - 1; i++) {
        cout << 2 << endl;
      }
      cout << 0 << endl;
    }
  }

  map<ll, ll> mp;
  mp[0] = 0;
  for (int i = 1; i <= 2 * 100000; i++) {
    ll popc = __builtin_popcount(i);
    if (i % popc == 0)
      mp[i] = 1;
    else {
      mp[i] = 1 + mp[i % popc];
    }
  }
  ll ppc = pc - 1, mpc = pc + 1;
  vll one(N, -1), zero(N, -1);

  ll psum = 0, msum = 0;
  ll tmp = 1LL;
  for (int i = 0; i < N; i++) {
    one.at(N - 1 - i) = tmp % ppc;
    tmp *= 2LL;
    tmp %= ppc;
    if (X.at(N - 1 - i) == '1')
      psum += one.at(N - 1 - i);
    psum = (psum + ppc) % ppc;
  }
  tmp = 1LL;
  for (int i = 0; i < N; i++) {
    zero.at(N - 1 - i) = tmp % mpc;
    tmp *= 2LL;
    tmp %= mpc;
    if (X.at(N - 1 - i) == '1')
      msum += zero.at(N - 1 - i);
    msum = (msum + mpc) % mpc;
  }

  for (int i = 0; i < N; i++) {
    if (X.at(i) == '1') {
      ll num = psum - one.at(i);
      num = (num + ppc) % ppc;
      cout << mp[num] + 1 << endl;
    } else {
      ll num = msum + zero.at(i);
      num = (num + mpc) % mpc;
      cout << mp[num] + 1 << endl;
    }
  }
}