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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll modpow(ll b, ll e, ll mod) {
  ll ans = 1;
  for (; e; b = b * b % mod, e /= 2)
    if (e & 1)
      ans = ans * b % mod;
  return ans;
}

ll solve(ll x) {
  ll ans = 0;
  while (__builtin_popcountll(x) != 0) {
    x %= __builtin_popcountll(x);
    ++ans;
  }
  return ans;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  string x;
  cin >> n >> x;

  int pcnt = 0;
  for (auto c : x)
    if (c == '1')
      ++pcnt;

  vi lo(n), hi(n);

  rep(i, 0, n) {
    if (x[i] == '1') {
      if (pcnt - 1 != 0)
        lo[i] = modpow(2, n - i - 1, pcnt - 1);
      hi[i] = modpow(2, n - i - 1, pcnt + 1);
    }
  }

  ll L = accumulate(all(lo), 0ll), H = accumulate(all(hi), 0ll);

  rep(i, 0, n) {
    if (x[i] == '1') {
      if (pcnt - 1 == 0) {
        cout << 0 << '\\n';
        continue;
      }
      ll rem = (L - lo[i]) % (pcnt - 1);
      rem += pcnt - 1;
      rem %= pcnt - 1;
      ll ans = 1 + solve(rem);
      cout << ans << '\\n';
    } else {
      ll rem = (H + modpow(2, n - i - 1, pcnt + 1)) % (pcnt + 1);
      rem += pcnt + 1;
      rem %= pcnt + 1;
      ll ans = 1 + solve(rem);
      cout << ans << '\\n';
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll modpow(ll b, ll e, ll mod) {
  ll ans = 1;
  for (; e; b = b * b % mod, e /= 2)
    if (e & 1)
      ans = ans * b % mod;
  return ans;
}

ll solve(ll x) {
  ll ans = 0;
  while (__builtin_popcountll(x) != 0) {
    x %= __builtin_popcountll(x);
    ++ans;
  }
  return ans;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  string x;
  cin >> n >> x;

  int pcnt = 0;
  for (auto c : x)
    if (c == '1')
      ++pcnt;

  vi lo(n), hi(n);

  rep(i, 0, n) {
    if (x[i] == '1') {
      lo[i] = modpow(2, n - i - 1, pcnt - 1);
      hi[i] = modpow(2, n - i - 1, pcnt + 1);
    }
  }

  ll L = accumulate(all(lo), 0ll), H = accumulate(all(hi), 0ll);

  rep(i, 0, n) {
    if (x[i] == '1') {
      ll rem = (L - lo[i]) % (pcnt - 1);
      rem += pcnt - 1;
      rem %= pcnt - 1;
      ll ans = 1 + solve(rem);
      cout << ans << '\\n';
    } else {
      ll rem = (H + modpow(2, n - i - 1, pcnt + 1)) % (pcnt + 1);
      rem += pcnt + 1;
      rem %= pcnt + 1;
      ll ans = 1 + solve(rem);
      cout << ans << '\\n';
    }
  }
}