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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
typedef long long ll;
struct edge {
  ll s, t, x;
};
typedef pair<ll, ll> P;
typedef pair<double, P> event;
const ll MAX = 510000;
const ll MOD = 1000000007;
#define INF (ll)10000000000000000

int solve(int n) {
  int st = 0;
  int a = n;
  while (a != 0) {
    if (a % 2 == 1) {
      a -= 1;
      st++;
    }
    a /= 2;
  }
  if (st == 0) {
    return 0;
  } else {
    return 1 + solve(n % st);
  }
}

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll count = 0ll;
  rep(i, X.size()) {
    if (X[i] == '1') {
      count += 1ll;
    }
  }
  if (count == 0) {
    rep(i, N) { cout << 1 << endl; }
    return 0;
  }
  ll p = count + 1ll, q = count - 1ll;
  ll memo[N][3];
  memo[0][0] = 1ll % p;
  memo[0][1] = 1ll % count;
  if (q != 0) {
    memo[0][2] = 1 % q;
  } else {
    memo[0][2] = -1;
  }
  rep(i, N - 1) {
    memo[i + 1][0] = (memo[i][0] * 2ll) % p;
    memo[i + 1][1] = (memo[i][1] * 2ll) % count;
    if (q != 0) {
      memo[i + 1][2] = (memo[i][2] * 2ll) % q;
    } else {
      memo[i + 1][2] = -1;
    }
  }
  ll sum[3];
  rep(i, 3) { sum[i] = 0ll; }
  rep(i, N) {
    if (X[N - 1 - i] == '1') {
      sum[0] += memo[i][0];
      sum[1] += memo[i][1];
      sum[2] += memo[i][2];
    }
  }
  rep(i, N) {
    if (X[i] == '1') {
      ll g = sum[2] - memo[N - 1 - i][2];
      if (q == 0) {
        cout << 0 << endl;
        continue;
      }
      g %= q;
      if (g == 0) {
        cout << 1 << endl;
        continue;
      }
      cout << 1 + solve(g) << endl;
    }
    if (X[i] == '0') {
      ll g = sum[0] + memo[N - 1 - i][0];
      g %= p;
      if (g == 0) {
        cout << 1 << endl;
        continue;
      }
      cout << 1 + solve(g) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
typedef long long ll;
struct edge {
  ll s, t, x;
};
typedef pair<ll, ll> P;
typedef pair<double, P> event;
const ll MAX = 510000;
const ll MOD = 1000000007;
#define INF (ll)10000000000000000

int solve(int n) {
  int st = 0;
  int a = n;
  while (a != 0) {
    if (a % 2 == 1) {
      a -= 1;
      st++;
    }
    a /= 2;
  }
  if (st == 0) {
    return 0;
  } else {
    return 1 + solve(n % st);
  }
}

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll count = 0ll;
  rep(i, X.size()) {
    if (X[i] == '1') {
      count += 1ll;
    }
  }
  ll p = count + 1ll, q = count - 1ll;
  ll memo[N][3];
  memo[0][0] = 1ll % p;
  memo[0][1] = 1ll % count;
  if (q <= 0) {
    memo[0][2] = 1 % q;
  } else {
    memo[0][2] = -1;
  }
  rep(i, N - 1) {
    memo[i + 1][0] = (memo[i][0] * 2ll) % p;
    memo[i + 1][1] = (memo[i][1] * 2ll) % count;
    if (q <= 0) {
      memo[i + 1][2] = (memo[i][2] * 2ll) % q;
    } else {
      memo[i + 1][2] = -1;
    }
  }
  ll sum[3];
  rep(i, 3) { sum[i] = 0ll; }
  rep(i, N) {
    if (X[N - 1 - i] == '1') {
      sum[0] += memo[i][0];
      sum[1] += memo[i][1];
      sum[2] += memo[i][2];
    }
  }
  rep(i, N) {
    if (X[i] == '1') {
      ll g = sum[2] - memo[N - 1 - i][2];
      if (q <= 0) {
        cout << 0 << endl;
        continue;
      }
      g %= q;
      if (g == 0) {
        cout << 1 << endl;
        continue;
      }
      cout << 1 + solve(g) << endl;
    }
    if (X[i] == '0') {
      ll g = sum[0] + memo[N - 1 - i][0];
      g %= p;
      if (g <= 0) {
        cout << 1 << endl;
        continue;
      }
      cout << 1 + solve(g) << endl;
    }
  }
}