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 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;
}

ll memo[200001];

int dfs(int n) {
  if (n == 0)
    return 0;
  if (memo[n])
    return memo[n];
  return memo[n] = dfs(n % __builtin_popcount(n)) + 1;
}

void make_table() { REPS(i, 200000) dfs(i); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  string s;
  cin >> n >> s;
  make_table();
  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;
    pk *= 2;
    pk %= (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 << memo[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 << memo[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;
}

ll memo[200001];

int dfs(int n) {
  if (n == 0)
    return 0;
  if (memo[n])
    return memo[n];
  return memo[n] = dfs(n % __builtin_popcount(n)) + 1;
}

void make_table() { REPS(i, 200000) dfs(i); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  string s;
  cin >> n >> s;
  make_table();
  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);
  }
  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);
  }
  REP(i, n) {
    if (s[i] == '0') {
      ll x = pres + pt[i];
      x %= (onec + 1);
      cout << memo[x] + 1 << endl;
    } else {
      ll y = mres - mt[i];
      if (y < 0)
        y += (onec - 1);
      y %= (onec - 1);
      cout << memo[y] + 1 << endl;
    }
  }
}