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

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

#define int long long
#define double long double

#define override_rep(i, l, r, mes, ...) mes
#define rep1(i, n) for (int i = 0; i < n; i++)
#define rep2(i, l, r) for (int i = l; i < r; i++)
#define rep(...) override_rep(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

using namespace std;
using P = pair<int, int>;
using ll = long long;
using ld = double;

using bint = boost::multiprecision::cpp_int;

constexpr int MOD = 1e9 + 7;

template <typename T, typename U>
istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}

// https://twitter.com/vane11ope ←フォローしましょう
// (ノ)>◡<(ヾ)ﾓﾁﾓﾁ
signed main(signed argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, popcnt = 0;
  string s;
  cin >> n >> s;
  vector<int> memo(200001);
  memo[0] = 0;
  rep(i, 1, 200001) {
    if (i < n + 1 && s[i - 1] == '1')
      popcnt++;
    memo[i] = memo[i % __builtin_popcountll(i)] + 1;
  }
  int p1 = popcnt + 1, p2 = popcnt - 1;
  int b1 = 0, b2 = 0;
  vector<int> m1(n), m2(n);
  m1[0] = 1 % p1;
  if (p2)
    m2[0] = 1 % p2;
  b1 = (s[n - 1] - '0') ? m1[0] : 0;
  if (p2)
    b2 = (s[n - 1] - '0') ? m2[0] : 0;
  rep(i, 1, n) {
    m1[i] = m1[i - 1] * 2 % p1;
    if (p2)
      m2[i] = m2[i - 1] * 2 % p2;
    b1 += (s[n - i - 1] - '0') ? m1[i] : 0;
    if (p2)
      b2 += (s[n - i - 1] - '0') ? m2[i] : 0;
    b1 %= p1;
    if (p2)
      b2 %= p2;
  }
  rep(i, n) {
    if (s[i] - '0') {
      if (p2)
        cout << memo[(b2 + p2 - m2[n - i - 1]) % p2] + 1 << endl;
      else
        cout << 0 << endl;
    } else {
      cout << memo[(b1 + m1[n - i - 1]) % p1] + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

#define int long long
#define double long double

#define override_rep(i, l, r, mes, ...) mes
#define rep1(i, n) for (int i = 0; i < n; i++)
#define rep2(i, l, r) for (int i = l; i < r; i++)
#define rep(...) override_rep(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

using namespace std;
using P = pair<int, int>;
using ll = long long;
using ld = double;

using bint = boost::multiprecision::cpp_int;

constexpr int MOD = 1e9 + 7;

template <typename T, typename U>
istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}

// https://twitter.com/vane11ope ←フォローしましょう
// (ノ)>◡<(ヾ)ﾓﾁﾓﾁ
signed main(signed argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, popcnt = 0;
  string s;
  cin >> n >> s;
  vector<int> memo(200001);
  memo[0] = 0;
  rep(i, 1, 200001) {
    if (i < n + 1 && s[i - 1] == '1')
      popcnt++;
    memo[i] = memo[i % __builtin_popcountll(i)] + 1;
  }
  int p1 = popcnt + 1, p2 = popcnt - 1;
  int b1 = 0, b2 = 0;
  vector<int> m1(n), m2(n);
  m1[0] = 1 % p1;
  if (p2)
    m2[0] = 1 % p2;
  b1 = (s[n - 1] - '0') ? m1[0] : 0;
  if (p2)
    b2 = (s[n - 1] - '0') ? m2[0] : 0;
  rep(i, 1, n) {
    m1[i] = m1[i - 1] * 2 % p1;
    if (p2)
      m2[i] = m2[i - 1] * 2 % p2;
    b1 += (s[n - i - 1] - '0') ? m1[i] : 0;
    if (p2)
      b2 += (s[n - i - 1] - '0') ? m2[i] : 0;
    b1 %= p1;
    if (p2)
      b2 %= p2;
  }
  rep(i, n) {
    if (s[i] - '0') {
      cout << memo[(b2 + p2 - m2[n - i - 1]) % p2] + 1 << endl;
    } else {
      if (p2)
        cout << memo[(b1 + m1[n - i - 1]) % p1] + 1 << endl;
      else
        cout << 0 << endl;
    }
  }
}