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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);
#define INF LONG_MAX
#define PI 3.1415926535897932384626
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(x, j, n) for (ll x = j; x < n; x++)
#define repr(x, j, n) for (ll x = j; x > n; x--)
#define mod 1000000007
typedef vector<pair<ll, ll>> vpi;
typedef vector<ll> vi;
typedef vector<vi> vvi;

ll modexp(ll a, ll b, ll m) {
  ll r = 1;
  a = a % m;
  while (b > 0) {
    if (b & 1ll)
      r = (r * a) % m;
    b = b / 2ll;
    a = (a * a) % m;
  }
  return r % m;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  vi dp(200001, 0);
  rep(i, 1, 200001) {
    ll j = i;
    ll c = 0;
    while (j > 0) {
      if (j % 2 != 0)
        c++;
      j /= 2;
    }

    dp[i] = 1 + dp[i % c];
  }
  ll m1 = 0, m2 = 0;
  ll o = 0;
  rep(i, 0, n) if (x[i] == '1') o++;
  if (o == 0) {
    rep(i, 0, n) cout << "1\\n";
    return 0;
  }
  if (o != 1) {
    rep(i, 0, n) {
      if (x[i] == '1')
        m1 += modexp(2ll, n - 1 - i, o - 1),
            m2 += modexp(2ll, n - 1 - i, o + 1);
      m1 %= (o - 1);
      m2 %= (o + 1);
    }
  } else {
    rep(i, 0, n) {
      if (x[i] == '1')
        m2 += modexp(2ll, n - 1 - i, o + 1);

      m2 %= (o + 1);
    }
  }
  // cout<<o<<" "<<m1<<" "<<m2<<endl;
  rep(i, 0, n) {
    if (x[i] == '1' && o != 1)
      cout << 1ll + dp[(m1 - modexp(2ll, n - 1 - i, o - 1) + (o - 1)) % (o - 1)]
           << endl;
    else if (x[i] == '1')
      cout << "0\\n";
    else
      cout << 1ll + dp[(m2 + modexp(2ll, n - 1 - i, o + 1) + (o + 1)) % (o + 1)]
           << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);
#define INF LONG_MAX
#define PI 3.1415926535897932384626
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(x, j, n) for (ll x = j; x < n; x++)
#define repr(x, j, n) for (ll x = j; x > n; x--)
#define mod 1000000007
typedef vector<pair<ll, ll>> vpi;
typedef vector<ll> vi;
typedef vector<vi> vvi;

ll modexp(ll a, ll b, ll m) {
  ll r = 1;
  a = a % m;
  while (b > 0) {
    if (b & 1ll)
      r = (r * a) % m;
    b = b / 2ll;
    a = (a * a) % m;
  }
  return r % m;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  vi dp(200001, 0);
  rep(i, 1, 200001) {
    ll j = i;
    ll c = 0;
    while (j > 0) {
      if (j % 2 != 0)
        c++;
      j /= 2;
    }

    dp[i] = 1 + dp[i % c];
  }
  ll m1 = 0, m2 = 0;
  ll o = 0;
  rep(i, 0, n) if (x[i] == '1') o++;

  rep(i, 0, n) {
    if (x[i] == '1')
      m1 += modexp(2ll, n - 1 - i, o - 1), m2 += modexp(2ll, n - 1 - i, o + 1);
    m1 %= (o - 1);
    m2 %= (o + 1);
  }
  // cout<<o<<" "<<m1<<" "<<m2<<endl;
  rep(i, 0, n) {
    if (x[i] == '1')
      cout << 1ll + dp[(m1 - modexp(2ll, n - 1 - i, o - 1) + (o - 1)) % (o - 1)]
           << endl;
    else
      cout << 1ll + dp[(m2 + modexp(2ll, n - 1 - i, o + 1) + (o + 1)) % (o + 1)]
           << endl;
  }
}