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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, n) for (int i = 2; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

ll dp[200010];
ll amari1[200010];
ll amari2[200010];

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popn = 0;
  rep(i, n) {
    if (s[i] == '1')
      popn++;
  }
  dp[0] = 0;
  rep1(i, 200000) { dp[i] = dp[i % (__builtin_popcount(i))] + 1; }
  amari1[0] = 1 % (popn + 1);
  if (popn - 1 != 0)
    amari2[0] = 1 % (popn - 1);
  rep1(i, 200000) { amari1[i] = amari1[i - 1] * 2 % (popn + 1); }
  rep1(i, 200000) {
    if (popn - 1 != 0)
      amari2[i] = amari2[i - 1] * 2 % (popn - 1);
  }
  ll x1 = 0;
  ll x2 = 0;
  reverse(all(s));
  rep(i, n) {
    if (s[i] == '1') {
      x1 = (x1 + amari1[i]) % (popn + 1);
      if (popn - 1 != 0)
        x2 = (x2 + amari2[i]) % (popn - 1);
    }
  }
  reverse(all(s));
  vector<ll> ans(n);
  rep(i, n) {
    if (s[i] == '1' && popn - 1 != 0)
      ans[i] = dp[(x2 - amari2[n - 1 - i] + (popn - 1)) % (popn - 1)];
    else if (s[i] == '1')
      ans[i] = -1;
    else
      ans[i] = dp[(x1 + amari1[n - 1 - i]) % (popn + 1)];
  }
  rep(i, n) { cout << ans[i] + 1 << endl; }
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, n) for (int i = 2; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

ll dp[31];
ll amari1[31];
ll amari2[31];

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popn = 0;
  rep(i, n) {
    if (s[i] == '1')
      popn++;
  }
  dp[0] = 0;
  rep1(i, 30) { dp[i] = dp[i % (__builtin_popcount(i))] + 1; }
  amari1[0] = 1 % (popn + 1);
  if (popn - 1 != 0)
    amari2[0] = 1 % (popn - 1);
  rep1(i, 30) { amari1[i] = amari1[i - 1] * 2 % (popn + 1); }
  rep1(i, 30) {
    if (popn - 1 != 0)
      amari2[i] = amari2[i - 1] * 2 % (popn - 1);
  }
  ll x1 = 0;
  ll x2 = 0;
  reverse(all(s));
  rep(i, n) {
    if (s[i] == '1') {
      x1 = (x1 + amari1[i]) % (popn + 1);
      if (popn - 1 != 0)
        x2 = (x2 + amari2[i]) % (popn - 1);
    }
  }
  reverse(all(s));
  vector<ll> ans(n);
  rep(i, n) {
    if (s[i] == '1' && popn - 1 != 0)
      ans[i] = dp[(x2 - amari2[n - 1 - i] + (popn - 1)) % (popn - 1)];
    else if (s[i] == '1')
      ans[i] = -1;
    else
      ans[i] = dp[(x1 + amari1[n - 1 - i]) % (popn + 1)];
  }
  rep(i, n) { cout << ans[i] + 1 << endl; }
}