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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<int, int> mii;
typedef vector<char> vc;
typedef vector<bool> vb;
#define endl '\\n'
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define forne(i, n) for (int i = 1; i <= n; i++)
#define rforne(i, n) for (int i = n; i >= 1; i--)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define rforse(i, s, e) for (int i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pf push_front
#define rb pop_back
#define rf pop_front
#define in insert
#define mp make_pair
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
const ll INFL = 1e18;
const int INFI = 1e9;
#ifdef KARAN
#include "../../trace.h"
#else
#define trace(args...)
#endif

ll div_floor(const ll &a, const ll &b) {
  return a / b - (((a ^ b) < 0) and a % b);
}
ll div_ceil(const ll &a, const ll &b) {
  return a / b + (((a ^ b) >= 0) and a % b);
}
ll modpow(ll a, ll b, ll m = MOD) {
  a %= m;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll bpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
ll modinv(ll a, ll m = MOD) { return modpow(a, m - 2, m); }
void graph(vvi &adj, int m) {
  int x, y;
  forn(i, m) {
    cin >> x >> y;
    adj[--x].pb(--y);
    adj[y].pb(x);
  }
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> process(n, 0);
  forne(i, n - 1) {
    bitset<20> b(i);
    process[i] = process[i % (__builtin_popcount(i))] + 1;
  }

  ll cntr = 0;
  forn(i, n) cntr += s[i] == '1';
  trace(cntr);
  ll cntr1 = cntr + 1;
  ll cntr2 = cntr - 1;
  ll rep1 = 0;
  forn(i, n) if (s[i] == '1') {
    rep1 += modpow(2, n - i - 1, cntr + 1);
    rep1 %= cntr + 1;
  }
  ll rep2 = 0;
  forn(i, n) if (s[i] == '1' && cntr2 != 0) {
    rep2 += modpow(2, n - i - 1, cntr - 1);
    rep2 %= cntr - 1;
  }
  trace(rep1, rep2);
  forn(i, n) {
    if (s[i] == '1') {
      if (cntr2 == 0)
        cout << 0 << endl;
      else {
        int crr = modpow(2, n - i - 1, cntr2);
        cout << 1 + process[((rep2 - crr) % cntr2 + cntr2) % cntr2] << endl;
      }
    } else {
      int crr = modpow(2, n - i - 1, cntr1);
      cout << 1 + process[((rep1 + crr) % cntr1 + cntr1) % cntr1] << endl;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef KARAN
  freopen(".inp.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
  freopen(".err.txt", "w", stderr);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<int, int> mii;
typedef vector<char> vc;
typedef vector<bool> vb;
#define endl '\\n'
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define forne(i, n) for (int i = 1; i <= n; i++)
#define rforne(i, n) for (int i = n; i >= 1; i--)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define rforse(i, s, e) for (int i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pf push_front
#define rb pop_back
#define rf pop_front
#define in insert
#define mp make_pair
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
const ll INFL = 1e18;
const int INFI = 1e9;
#ifdef KARAN
#include "../../trace.h"
#else
#define trace(args...)
#endif

ll div_floor(const ll &a, const ll &b) {
  return a / b - (((a ^ b) < 0) and a % b);
}
ll div_ceil(const ll &a, const ll &b) {
  return a / b + (((a ^ b) >= 0) and a % b);
}
ll modpow(ll a, ll b, ll m = MOD) {
  a %= m;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll bpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
ll modinv(ll a, ll m = MOD) { return modpow(a, m - 2, m); }
void graph(vvi &adj, int m) {
  int x, y;
  forn(i, m) {
    cin >> x >> y;
    adj[--x].pb(--y);
    adj[y].pb(x);
  }
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> process(n, 0);
  forne(i, n - 1) {
    bitset<20> b(i);
    process[i] = process[i % (__builtin_popcount(i))] + 1;
  }

  ll cntr = 0;
  forn(i, n) cntr += s[i] == '1';
  trace(cntr);
  ll cntr1 = cntr + 1;
  ll cntr2 = cntr - 1;
  ll rep1 = 0;
  forn(i, n) if (s[i] == '1') {
    rep1 += modpow(2, n - i - 1, cntr + 1);
    rep1 %= cntr + 1;
  }
  ll rep2 = 0;
  forn(i, n) if (s[i] == '1') {
    rep2 += modpow(2, n - i - 1, cntr - 1);
    rep2 %= cntr - 1;
  }
  trace(rep1, rep2);
  forn(i, n) {
    if (s[i] == '1') {
      if (cntr2 == 0)
        cout << 0 << endl;
      else {
        int crr = modpow(2, n - i - 1, cntr2);
        cout << 1 + process[((rep2 - crr) % cntr2 + cntr2) % cntr2] << endl;
      }
    } else {
      int crr = modpow(2, n - i - 1, cntr1);
      cout << 1 + process[((rep1 + crr) % cntr1 + cntr1) % cntr1] << endl;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef KARAN
  freopen(".inp.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
  freopen(".err.txt", "w", stderr);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}