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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

VI v(301010);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  v[0] = 0;
  for (i = 1; i < 301010; i++) {
    int cnt = 0;
    for (j = 0; j < 25; j++) {
      cnt += ((i >> j) & 1);
    }
    v[i] = 1 + v[i % cnt];
    // cout << v[i] << endl;
  }
  int n;
  string x;
  cin >> n >> x;
  int p = 0;
  for (i = 0; i < n; i++) {
    if (x[i] == '1')
      p++;
  }
  int p1 = p + 1;
  int p2 = p - 1;
  int k1 = 1;
  int k2 = 1;
  int u1 = 0;
  int u2 = 0;
  VI r1(n);
  VI r2(n);
  for (i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      u1 += k1;
      u1 %= p1;
      u2 += k2;
      if (p2 != 0)
        u2 %= p2;
    }
    r1[i] = k1;
    r2[i] = k2;
    k1 *= 2;
    k1 %= p1;
    k2 *= 2;
    if (p2 != 0)
      k2 %= p2;
  }
  for (i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (p2 == 0) {
        cout << 0 << endl;
      } else {
        int b = (u2 + p2 - r2[i]) % p2;
        cout << 1 + v[b] << endl;
      }
    } else {
      int b = (u1 + r1[i]) % p1;
      cout << 1 + v[b] << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

VI v(301010);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  v[0] = 0;
  for (i = 1; i < 301010; i++) {
    int cnt = 0;
    for (j = 0; j < 25; j++) {
      cnt += ((i >> j) & 1);
    }
    v[i] = 1 + v[i % cnt];
    // cout << v[i] << endl;
  }
  int n;
  string x;
  cin >> n >> x;
  int p = 0;
  for (i = 0; i < n; i++) {
    if (x[i] == '1')
      p++;
  }
  int p1 = p + 1;
  int p2 = p - 1;
  int k1 = 1;
  int k2 = 1;
  int u1 = 0;
  int u2 = 0;
  VI r1(n);
  VI r2(n);
  for (i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      u1 += k1;
      u1 %= p1;
      u2 += k2;
      u2 %= p2;
    }
    r1[i] = k1;
    r2[i] = k2;
    k1 *= 2;
    k1 %= p1;
    k2 *= 2;
    k2 %= p2;
  }
  for (i = 0; i < n; i++) {
    if (x[i] == '1') {
      int b = (u2 + p2 - r2[i]) % p2;
      cout << 1 + v[b] << endl;
    } else {
      int b = (u1 + r1[i]) % p1;
      cout << 1 + v[b] << endl;
    }
  }
}