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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll n;
  cin >> n;
  bitset<200005> x;
  cin >> x;
  ll sum = 0;
  rep(i, n) {
    if (x[i] == 1)
      sum++;
  }
  vector<ll> A(200005), B(200005);
  rep(i, 200005) {
    if (i == 0) {
      A[i] = 1;
      B[i] = 1;
    } else {
      A[i] = (A[i - 1] * 2) % (sum + 1);
      if ((sum - 1) != 0)
        B[i] = (B[i - 1] * 2) % (sum - 1);
    }
  }
  ll p = 0;
  ll q = 0;
  rep(i, n) {
    if (x[i] == 1) {
      p = (p + A[i]) % (sum + 1);
      if ((sum - 1) != 0)
        q = (q + B[i]) % (sum - 1);
    }
  }
  rep(i, n) {
    ll ans = 0;
    ans++;
    ll ke = n - 1 - i;
    ll next = 0;
    if (x[ke] == 0) {
      next = (p + A[ke]) % (sum + 1);
    }
    if (x[ke] == 1) {
      if (sum - 1 == 0) {
        cout << 0 << endl;
        continue;
      } else
        next = (q - B[ke] + sum - 1) % (sum - 1);
    }

    if (next == 0) {
      cout << ans << endl;
    } else {
      while (next != 0) {
        ans++;
        bitset<20> s(next);
        ll cum = 0;
        rep(i, 20) { cum += s[i]; }
        next = next % cum;
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll n;
  cin >> n;
  bitset<200005> x;
  cin >> x;
  ll sum = 0;
  rep(i, n) {
    if (x[i] == 1)
      sum++;
  }
  vector<ll> A(200005), B(200005);
  rep(i, 200005) {
    if (i == 0) {
      A[i] = 1;
      B[i] = 1;
    } else {
      A[i] = (A[i - 1] * 2) % (sum + 1);
      B[i] = (B[i - 1] * 2) % (sum - 1);
    }
  }
  ll p = 0;
  ll q = 0;
  rep(i, n) {
    if (x[i] == 1) {
      p = (p + A[i]) % (sum + 1);
      q = (q + B[i]) % (sum - 1);
    }
  }
  rep(i, n) {
    ll ans = 0;
    ans++;
    ll ke = n - 1 - i;
    ll next = 0;
    if (x[ke] == 0) {
      next = (p + A[ke]) % (sum + 1);
    }
    if (x[ke] == 1) {
      next = (q - B[ke] + sum - 1) % (sum - 1);
    }

    if (next == 0) {
      cout << ans << endl;
    } else {
      while (next != 0) {
        ans++;
        bitset<20> s(next);
        ll cum = 0;
        rep(i, 20) { cum += s[i]; }
        next = next % cum;
      }
      cout << ans << endl;
    }
  }
  return 0;
}