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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

long long modpow(long long x, long long n, int mod) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = (ret * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll p = 0;
  rep(i, n) p += (int)(x[i] - '0');
  ll m1 = p + 1, m2 = p - 1;
  ll c1 = 1;
  ll c2 = 1;
  ll now1 = 0;
  ll now2 = 0;
  // cout << m2 << endl;
  for (ll i = n - 1; i >= 0; i--) {
    now1 = (now1 + (c1 * (x[i] - '0'))) % m1;
    if (m2 > 0)
      now2 = (now2 + (c2 * (x[i] - '0'))) % m2;
    c1 = (c1 * 2) % m1;
    if (m2 > 0)
      c2 = (c2 * 2) % m2;
  }
  /*
  cout << now1 << "  "<< now2 << endl;
  cout << m1 <<  " "<< m2 << endl;
  */

  vector<ll> ans(n);
  rep(i, n) {
    ll res = 1;
    if (x[i] == '1') {
      ll now = 0;
      if (m2 > 0)
        now = (now2 - modpow(2, n - i - 1, m2) + m2) % m2;
      else
        res = 0;
      // cout << now <<" " << i << endl;
      while (now > 0 && m2 > 0) {
        now %= (__builtin_popcount(now));
        ++res;
      }
    } else {
      ll now = (now1 + modpow(2, n - i - 1, m1)) % m1;
      // cout << now <<" " << i << endl;
      while (now > 0) {
        now %= (__builtin_popcount(now));
        ++res;
      }
    }
    ans[i] = res;
  }

  rep(i, n) cout << ans[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

long long modpow(long long x, long long n, int mod) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = (ret * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll p = 0;
  rep(i, n) p += (x[i] - '0');
  ll m1 = p + 1, m2 = p - 1;
  ll c1 = 1;
  ll c2 = 1;
  ll now1 = 0;
  ll now2 = 0;
  for (ll i = n - 1; i >= 0; i--) {
    now1 = (now1 + (c1 * (x[i] - '0'))) % m1;
    now2 = (now2 + (c2 * (x[i] - '0'))) % m2;
    c1 = (c1 * 2) % m1;
    c2 = (c2 * 2) % m2;
  }
  /*
  cout << now1 << "  "<< now2 << endl;
  cout << m1 <<  " "<< m2 << endl;
  */

  vector<ll> ans(n);
  rep(i, n) {
    ll res = 1;
    if (x[i] == '1') {
      ll now = (now2 - modpow(2, n - i - 1, m2) + m2) % m2;
      // cout << now <<" " << i << endl;
      while (now > 0) {
        now %= __builtin_popcount(now);
        ++res;
      }
    } else {
      ll now = (now1 + modpow(2, n - i - 1, m1)) % m1;
      // cout << now <<" " << i << endl;
      while (now > 0) {
        now %= __builtin_popcount(now);
        ++res;
      }
    }
    ans[i] = res;
  }

  rep(i, n) cout << ans[i] << endl;

  return 0;
}