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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef pair<l_l, ll> lll;
typedef vector<string> vs;
const ll dx[4] = {1, -1, 0, 0};
const ll dy[4] = {0, 0, 1, -1};
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
const int INF = 1001001000;
const int mINF = -1001001000;
const ll LINF = 1010010010010010000;
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;
}
ll dp[200200], p[2][200200], mod[2], sum[2];
int main() {
  ll n;
  string s;
  cin >> n >> s;
  dp[0] = 0;
  for (int i = 1; i < 200200; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }
  int cnt = 0;
  rep(i, n) cnt += (s[i] == '1');
  mod[0] = cnt + 1;
  mod[1] = cnt - 1;
  rep(i, 2) {
    if (mod[i] == 0) {
      continue;
    }
    p[i][0] = 1;
    for (int j = 1; j < 200100; j++) {
      p[i][j] = 2ll * p[i][j - 1] % mod[i];
    }
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        sum[i] += p[i][n - 1 - j];
        sum[i] %= mod[i];
      }
    }
  }
  rep(i, n) {
    int ans = 0;
    if (s[i] == '0') {
      ans = 1 + dp[(sum[0] + p[0][n - 1 - i]) % mod[0]];
    } else if (mod[1]) {
      ans = 1 + dp[(sum[1] + mod[1] - p[1][n - 1 - i]) % mod[1]];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef pair<l_l, ll> lll;
typedef vector<string> vs;
const ll dx[4] = {1, -1, 0, 0};
const ll dy[4] = {0, 0, 1, -1};
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
const int INF = 1001001000;
const int mINF = -1001001000;
const ll LINF = 1010010010010010000;
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;
}
ll dp[200200], p[2][200200], mod[2], sum[2];
int main() {
  ll n;
  string s;
  cin >> n >> s;
  dp[0] = 0;
  for (int i = 1; i <= 200100; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }
  int cnt = 0;
  rep(i, n) cnt += (s[i] == '1');
  mod[0] = cnt + 1;
  mod[1] = cnt - 1;
  rep(i, 2) {
    if (mod[0] == 0) {
      continue;
    }
    p[i][0] = 1;
    for (int j = 1; j < 200100; j++) {
      p[i][j] = 2ll * p[i][j - 1] % mod[i];
    }
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        sum[i] += p[i][n - 1 - j];
        sum[i] %= mod[i];
      }
    }
  }
  rep(i, n) {
    int ans = 0;
    if (s[i] == '0') {
      ans = 1 + dp[(sum[0] + p[0][n - 1 - i]) % mod[0]];
    } else if (mod[1]) {
      ans = 1 + dp[(sum[1] + mod[1] - p[1][n - 1 - i]) % mod[1]];
    }
    cout << ans << endl;
  }
  return 0;
}