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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;

ll powmod(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n % 2)
    return x * powmod(x * x % mod, n / 2, mod) % mod;
  return powmod(x * x % mod, n / 2, mod) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll count = 0;
  rep(i, n) if (s[i] == '1') count++;
  ll x_p = 0, x_m = 0;
  rep(i, n) {
    if (s[i] == '1') {
      x_p += powmod(2, n - i - 1, count + 1);
      x_p %= count + 1;
    }
  }
  if (count >= 2) {
    rep(i, n) {
      if (s[i] == '1') {
        x_m += powmod(2, n - i - 1, count - 1);
        x_m %= count - 1;
      }
    }
  }
  rep(i, n) {
    if (s[i] == '1') {
      if (count == 1) {
        cout << 0 << endl;
        continue;
      }
      ll x_ = (x_m - powmod(2, n - i - 1, count - 1) + count - 1) % (count - 1);
      ll ans = 1;
      while (x_ != 0) {
        ll p = 0, r = x_;
        while (r != 0) {
          if (r % 2 != 0)
            p++;
          r /= 2;
        }
        x_ %= p;
        ans++;
      }
      cout << ans << endl;
    } else {
      ll x_ = (x_p + powmod(2, n - i - 1, count + 1)) % (count + 1);
      ll ans = 1;
      while (x_ != 0) {
        ll p = 0, r = x_;
        while (r != 0) {
          if (r % 2 != 0)
            p++;
          r /= 2;
        }
        x_ %= p;
        ans++;
      }
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;

ll powmod(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n % 2)
    return x * powmod(x * x % mod, n / 2, mod) % mod;
  return powmod(x * x % mod, n / 2, mod) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll count = 0;
  rep(i, n) if (s[i] == '1') count++;
  ll x_p = 0, x_m = 0;
  rep(i, n) {
    if (s[i] == '1') {
      x_p += powmod(2, n - i - 1, count + 1);
      x_p %= count + 1;
    }
  }
  if (count != 0) {
    rep(i, n) {
      if (s[i] == '1') {
        x_m += powmod(2, n - i - 1, count - 1);
        x_m %= count - 1;
      }
    }
  }
  rep(i, n) {
    if (s[i] == '1') {
      if (count == 1) {
        cout << 0 << endl;
        break;
      }
      ll x_ = (x_m - powmod(2, n - i - 1, count - 1) + count - 1) % (count - 1);
      ll ans = 1;
      while (x_ != 0) {
        ll p = 0, r = x_;
        while (r != 0) {
          if (r % 2 != 0)
            p++;
          r /= 2;
        }
        x_ %= p;
        ans++;
      }
      cout << ans << endl;
    } else {
      ll x_ = (x_p + powmod(2, n - i - 1, count + 1)) % (count + 1);
      ll ans = 1;
      while (x_ != 0) {
        ll p = 0, r = x_;
        while (r != 0) {
          if (r % 2 != 0)
            p++;
          r /= 2;
        }
        x_ %= p;
        ans++;
      }
      cout << ans << endl;
    }
  }
}