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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fsp(x) fixed << setprecision(x)
#define xout cerr
const ll inf = LLONG_MAX;
const long double pi = acosl(-1);
const char endk = '\\n';
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // const ll mod = 1e9 + 7;
  // const ll mod = 998244353;

  ll n;
  string s;
  cin >> n >> s;

  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }

  ll mod1 = cnt + 1, mod2 = cnt - 1;
  vector<ll> modpow1(n), modpow2(n);
  ll x = 1, y = 1, modsum1 = 0, modsum2 = 0;
  bool flag = false;
  if (mod2 == 0) {
    flag = true;
    for (ll i = 0; i < n; i++) {
      modpow1[i] = x % mod1;
      // modpow2[i] = y % mod2;
      if (s[n - 1 - i] == '1') {
        modsum1 += x;    // modsum2 += y;
        modsum1 %= mod1; // modsum2 %= mod2;
      }
      x *= 2;    // y *= 2;
      x %= mod1; // y %= mod2;
    }
  } else {
    for (ll i = 0; i < n; i++) {
      modpow1[i] = x % mod1;
      modpow2[i] = y % mod2;
      if (s[n - 1 - i] == '1') {
        modsum1 += x;
        modsum2 += y;
        modsum1 %= mod1;
        modsum2 %= mod2;
      }
      x *= 2;
      y *= 2;
      x %= mod1;
      y %= mod2;
    }
  }

  vector<ll> v(2e5 + 1, 0);
  for (ll i = 1; i <= 2e5; i++) {
    ll x = __builtin_popcount(i);
    v[i] = v[i % x] + 1;
  }

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (flag)
        cout << 0 << endl;
      else {
        ll x = modsum2 - modpow2[n - 1 - i];
        if (x < 0)
          x += mod2;
        cout << v[x % mod2] + 1 << endl;
      }
    } else {
      ll x = modsum1 + modpow1[n - 1 - i];
      x %= mod1;
      cout << v[x % mod1] + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fsp(x) fixed << setprecision(x)
#define xout cerr
const ll inf = LLONG_MAX;
const long double pi = acosl(-1);
const char endk = '\\n';
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // const ll mod = 1e9 + 7;
  // const ll mod = 998244353;

  ll n;
  string s;
  cin >> n >> s;

  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }

  ll mod1 = cnt + 1, mod2 = cnt - 1;
  vector<ll> modpow1(n), modpow2(n);
  ll x = 1, y = 1, modsum1 = 0, modsum2 = 0;
  for (ll i = 0; i < n; i++) {
    modpow1[i] = x % mod1;
    modpow2[i] = y % mod2;
    if (s[n - 1 - i] == '1') {
      modsum1 += x;
      modsum2 += y;
      modsum1 %= mod1;
      modsum2 %= mod2;
    }
    x *= 2;
    y *= 2;
    x %= mod1;
    y %= mod2;
  }

  vector<ll> v(2e5 + 1, 0);
  for (ll i = 1; i <= 2e5; i++) {
    ll x = __builtin_popcount(i);
    v[i] = v[i % x] + 1;
  }

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      ll x = modsum2 - modpow2[n - 1 - i];
      if (x < 0)
        x += mod2;
      cout << v[x % mod2] + 1 << endl;
    } else {
      ll x = modsum1 + modpow1[n - 1 - i];
      x %= mod1;
      cout << v[x % mod1] + 1 << endl;
    }
  }
}