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

#include <bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < (x); i++)
#define chmax(x, a)                                                            \\
  do {                                                                         \\
    x = max(x, a);                                                             \\
  } while (0)

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

// x < 3 * 10^5
ll f(ull x) {
  static vector<ll> memo(300000, -1);
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];

  ll popcnt = 0;
  rep(i, 30) if ((x >> i) & 1) popcnt++;
  return memo[x] = 1 + f(x % popcnt);
}

ll ans[300000];

int main(void) {
  ll N;
  string s;
  cin >> N >> s;
  reverse(begin(s), end(s));
  ll popcnt = 0;
  for (auto c : s)
    if (c == '1')
      popcnt++;
  ll orig_p = 0, orig_m = 0;
  ll ten_p = 1, ten_m = 1;
  rep(i, N) {
    if (s[i] == '1')
      (orig_p += ten_p) %= popcnt + 1;
    if (s[i] == '1' && popcnt > 1)
      (orig_m += ten_m) %= popcnt - 1; // popcnt > 1
    (ten_p *= 2) %= popcnt + 1;
    if (popcnt > 1)
      (ten_m *= 2) %= popcnt - 1;
  }

  ten_p = 1, ten_m = 1;
  rep(i, N) {
    ll nxt;
    if (s[i] == '0') {
      nxt = (popcnt + 1 + orig_p + ten_p) % (popcnt + 1);
    }
    if (s[i] == '1') {
      if (popcnt == 1) {
        ans[i] = 0;
        continue;
      }
      if (popcnt > 1)
        nxt = (popcnt - 1 + orig_m - ten_m) % (popcnt - 1); // popcnt > 1
    }
    ans[i] = 1 + f(nxt);
    (ten_p *= 2) %= popcnt + 1;
    if (popcnt > 1)
      (ten_m *= 2) %= popcnt - 1;
  }
  for (ll i = N - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < (x); i++)
#define chmax(x, a)                                                            \\
  do {                                                                         \\
    x = max(x, a);                                                             \\
  } while (0)

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

// x < 3 * 10^5
ll f(ull x) {
  static vector<ll> memo(300000, -1);
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];

  ll popcnt = 0;
  rep(i, 30) if ((x >> i) & 1) popcnt++;
  return memo[x] = 1 + f(x % popcnt);
}

ll ans[300000];

int main(void) {
  ll N;
  string s;
  cin >> N >> s;
  reverse(begin(s), end(s));
  ll popcnt = 0;
  for (auto c : s)
    if (c == '1')
      popcnt++;
  ll orig_p = 0, orig_m = 0;
  ll ten_p = 1, ten_m = 1;
  rep(i, N) {
    if (s[i] == '1')
      (orig_p += ten_p) %= popcnt + 1;
    if (s[i] == '1')
      (orig_m += ten_m) %= popcnt - 1; // popcnt > 1
    (ten_p *= 2) %= popcnt + 1;
    (ten_m *= 2) %= popcnt - 1;
  }

  ten_p = 1, ten_m = 1;
  rep(i, N) {
    ll nxt;
    if (s[i] == '0') {
      nxt = (popcnt + 1 + orig_p + ten_p) % (popcnt + 1);
    } else {
      nxt = (popcnt - 1 + orig_m - ten_m) % (popcnt - 1);
    }
    ans[i] = 1 + f(nxt);
    (ten_p *= 2) %= popcnt + 1;
    (ten_m *= 2) %= popcnt - 1;
  }
  for (ll i = N - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }
  return 0;
}