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

// 2020-07-11 21:09:47
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

ll rec(ll a) {
  if (a == 0)
    return 0;
  return 1 + rec(a % __builtin_popcount(a));
}
void answer() {
  ll n;
  string x;
  cin >> n >> x;
  ll orig = 0;
  rep(i, n) if (x[i] == '1') orig++;
  map<ll, ll> mp[2];
  ll tot[2];
  string rx = x;
  reverse(ALL(rx));
  rep(k, 2) {
    ll mod = orig;
    if (k == 0)
      mod--;
    else
      mod++;
    if (mod <= 0)
      continue;
    tot[k] = 0;
    ll cur = 1 % mod;
    rep(i, n) {
      if (i)
        cur = (2 * cur) % mod;
      mp[k][i] = cur;
      if (rx[i] == '1') {
        tot[k] += mp[k][i];
        tot[k] %= mod;
      }
    }
  }
  vector<ll> res;
  rep(xi, n) {
    int i = n - xi - 1;
    int d = x[xi] - '0';
    if (d == 0) {
      ll mod = orig + 1;
      ll st = (tot[1] + mp[1][i]) % mod;
      res.emplace_back(rec(st) + 1);
    } else {
      ll mod = orig - 1;
      if (mod == 0) {
        res.emplace_back(0);
        continue;
      }
      ll st = (mod + tot[0] - mp[0][i]) % mod;
      if (st == 0) {
        res.emplace_back(1);
        continue;
      }
      res.emplace_back(rec(st) + 1);
    }
  }
  for (const auto &a : res)
    cout << a << '\\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}


Error: 0

Fixed submission:

// 2020-07-11 21:09:47
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

ll rec(ll a) {
  if (a == 0)
    return 0;
  return 1 + rec(a % __builtin_popcount(a));
}
void answer() {
  ll n;
  string x;
  cin >> n >> x;
  ll orig = 0;
  rep(i, n) if (x[i] == '1') orig++;
  map<ll, ll> mp[2];
  ll tot[2];
  string rx = x;
  reverse(ALL(rx));
  rep(k, 2) {
    ll mod = orig;
    if (k == 0)
      mod--;
    else
      mod++;
    if (mod <= 0)
      continue;
    tot[k] = 0;
    ll cur = 1 % mod;
    rep(i, n) {
      if (i)
        cur = (2 * cur) % mod;
      mp[k][i] = cur;
      if (rx[i] == '1') {
        tot[k] += mp[k][i];
        tot[k] %= mod;
      }
    }
  }
  vector<ll> res;
  rep(xi, n) {
    int i = n - xi - 1;
    int d = x[xi] - '0';
    if (d == 0) {
      ll mod = orig + 1;
      ll st = (tot[1] + mp[1][i]) % mod;
      res.emplace_back(rec(st) + 1);
    } else {
      ll mod = orig - 1;
      ll st = (mod + tot[0] - mp[0][i]) % mod;
      res.emplace_back(rec(st) + 1);
    }
  }
  for (const auto &a : res)
    cout << a << '\\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}