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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define popcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
const ll INF = 1e18;
template <typename T>

T pow(T a, long long n, ll mod, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    ret %= mod;
    a %= mod;
    n >>= 1;
  }
  return ret % mod;
}

int solve(int n) {
  if (n == 0)
    return 0;
  return solve(n % popcnt(n)) + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  string s;
  cin >> n >> s;
  ll cnt = 0;
  REP(i, n) if (s[i] == '1') cnt++;
  ll psum = 0, msum = 0;
  REP(i, n) if (s[i] == '1') {
    psum += pow((ll)2, n - i - 1, cnt + 1);
    psum %= (cnt + 1);
  }
  if (cnt - 1)
    REP(i, n) if (s[i] == '1') {
      msum += pow((ll)2, n - i - 1, cnt - 1);
      msum %= (cnt - 1);
    }
  REP(i, n) {
    if (s[i] == '0') {
      ll x = psum + pow((ll)2, n - i - 1, cnt + 1);
      x %= (cnt + 1);
      cout << solve(x) + 1 << endl;
    } else {
      if (!(cnt - 1)) {
        cout << 0 << endl;
        continue;
      }
      ll y = msum - pow((ll)2, n - i - 1, cnt - 1);
      y %= (cnt - 1);
      if (y < 0)
        y += (cnt - 1);
      cout << solve(y) + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
const ll INF = 1e18;
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}

int solve(int n) {
  if (n == 0)
    return 0;
  return solve(n % __builtin_popcount(n)) + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  string s;
  cin >> n >> s;
  ll onec = 0;
  REP(i, n) if (s[i] == '1') onec++;
  ll pres = 0, mres = 0;
  REP(i, n) {
    pres *= 2;
    if (s[i] == '1')
      pres++;
    pres %= (onec + 1);
  }
  if (onec - 1)
    REP(i, n) {
      mres *= 2;
      if (s[i] == '1')
        mres++;
      mres %= (onec - 1);
    }
  V<> pt(n, 0), mt(n, 0);
  ll pk = 1, mk = 1;
  RREP(i, n) {
    pt[i] = pk;
    mt[i] = mk;
    pk *= 2;
    mk *= 2;
    pk %= (onec + 1);
    mk %= (onec - 1);
  }
  if (onec - 1)
    RREP(i, n) {
      mt[i] = mk;
      mk *= 2;
      mk %= (onec - 1);
    }
  REP(i, n) {
    if (s[i] == '0') {
      ll x = pres + pt[i];
      x %= (onec + 1);
      cout << solve(x) + 1 << endl;
    } else {
      if (onec - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      ll y = mres - mt[i];
      if (y < 0)
        y += (onec - 1);
      y %= (onec - 1);
      cout << solve(y) + 1 << endl;
    }
  }
}