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

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  string X;
  cin >> N >> X;
  int cnt = 0;
  REP(i, N) if (X[i] == '1') cnt++;

  int div_neg = max(1, cnt - 1), div_pos = cnt + 1;

  vector<ll> v_neg(N), v_pos(N);
  v_neg[0] = div_neg > 1 ? 1 : 0;
  v_pos[0] = div_pos > 1 ? 1 : 0;
  REP(i, N - 1) {
    v_neg[i + 1] = (v_neg[i] * 2) % div_neg;
    v_pos[i + 1] = (v_pos[i] * 2) % div_pos;
  }

  ll s_neg = 0, s_pos = 0;
  REP(i, N) {
    if (X[i] == '1') {
      s_neg = (s_neg + v_neg[N - i - 1]) % div_neg;
      s_pos = (s_pos + v_pos[N - i - 1]) % div_pos;
    }
  }

  REP(i, N) {
    ll s_cur;
    if (X[i] == '1') {
      if (cnt == 1) {
        print(0);
        continue;
      }
      s_cur = (s_neg - v_neg[N - i - 1] + div_neg) % div_neg;
    } else {
      s_cur = (s_pos + v_pos[N - i - 1]) % div_pos;
    }

    int ans = 1;
    while (s_cur > 0) {
      ans++;
      s_cur %= bcnt(s_cur);
    }

    print(ans);
  }
}


Error: 0

Fixed submission:


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  string X;
  cin >> N >> X;

  int cnt = 0;
  REP(i, N) if (X[i] == '1') cnt++;

  vector<ll> v_neg(N), v_pos(N);
  v_neg[0] = cnt > 1 ? 1 : 0;
  v_pos[0] = cnt > 0 ? 1 : 0;
  REP(i, N - 1) {
    if (cnt > 1)
      v_neg[i + 1] = (v_neg[i] * 2) % (cnt - 1);
    else
      v_neg[i + 1] = 0;

    v_pos[i + 1] = (v_pos[i] * 2) % (cnt + 1);
  }

  ll s_neg = 0, s_pos = 0;
  REP(i, N) {
    if (X[i] == '1') {
      s_neg = (s_neg + v_neg[N - i - 1]) % (cnt - 1);
      s_pos = (s_pos + v_pos[N - i - 1]) % (cnt + 1);
    }
  }

  REP(i, N) {
    ll s_cur;
    if (X[i] == '1') {
      s_cur = (s_neg - v_neg[N - i - 1] + cnt - 1) % (cnt - 1);
    } else {
      s_cur = (s_pos + v_pos[N - i - 1]) % (cnt + 1);
    }

    int ans = 1;
    while (s_cur > 0) {
      ans++;
      s_cur %= bcnt(s_cur);
    }

    print(ans);
  }
}