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

// created by mtnshh

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

#define ll long long
#define ld long double

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)

#define err() cout << "--------------------------" << endl;
#define errA(A)                                                                \\
  for (auto i : A)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c)                                                          \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << endl
#define err4(a, b, c, d)                                                       \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << " " << #d << " " << d << endl

#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second

#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>

#define endl "\\n"

const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265

const ll N = 200005;

ll A[N];

ll popcount(ll x) {
  ll cnt = 0;
  rep(i, 0, logN) {
    if (x & (1LL << i))
      cnt++;
  }
  return cnt;
}

ll power(ll a, ll n, ll m = M) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a;
    a = a * a;
    n = n >> 1;
    ans = ans % m;
    a = a % m;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  rep(i, 1, N) {
    ll y = i;
    ll cnt = 0;
    while (y) {
      ll g = popcount(y);
      cnt++;
      y %= g;
    }
    A[i] = cnt;
  }
  ll n;
  string s;
  cin >> n >> s;
  ll cnt = 0;
  rep(i, 0, n) {
    if (s[i] == '1')
      cnt++;
  }
  ll a = 0, b = 0;
  cnt--;
  rep(i, 0, n) {
    if (cnt > 0 and s[i] == '1') {
      a += power(2, n - i - 1, cnt);
    }
  }
  if (cnt > 0)
    a %= cnt;
  cnt += 2;
  rep(i, 0, n) {
    if (cnt > 0 and s[i] == '1') {
      b += power(2, n - i - 1, cnt);
    }
  }
  if (cnt > 0)
    b %= cnt;
  cnt--;
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (cnt > 1) {
        cnt--;
        a -= power(2, n - i - 1, cnt);
        a = (a % cnt + cnt) % cnt;
        cout << 1 + A[a] << endl;
        a += power(2, n - i - 1, cnt);
        a %= cnt;
        cnt++;
      } else {
        cout << 0 << endl;
      }
    } else {
      cnt++;
      b += power(2, n - i - 1, cnt);
      b = (b % cnt + cnt) % cnt;
      cout << 1 + A[b] << endl;
      b -= power(2, n - i - 1, cnt);
      b %= cnt;
      cnt--;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

// created by mtnshh

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

#define ll long long
#define ld long double

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)

#define err() cout << "--------------------------" << endl;
#define errA(A)                                                                \\
  for (auto i : A)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c)                                                          \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << endl
#define err4(a, b, c, d)                                                       \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << " " << #d << " " << d << endl

#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second

#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>

#define endl "\\n"

const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265

const ll N = 200005;

ll A[N];

ll popcount(ll x) {
  ll cnt = 0;
  rep(i, 0, logN) {
    if (x & (1LL << i))
      cnt++;
  }
  return cnt;
}

ll power(ll a, ll n, ll m = M) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a;
    a = a * a;
    n = n >> 1;
    ans = ans % m;
    a = a % m;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  rep(i, 1, N) {
    ll y = i;
    ll cnt = 0;
    while (y) {
      ll g = popcount(y);
      cnt++;
      y %= g;
    }
    A[i] = cnt;
  }
  ll n;
  string s;
  cin >> n >> s;
  ll cnt = 0;
  rep(i, 0, n) {
    if (s[i] == '1')
      cnt++;
  }
  ll a = 0, b = 0;
  cnt--;
  rep(i, 0, n) {
    if (cnt > 0 and s[i] == '1') {
      a += power(2, n - i - 1, cnt);
    }
  }
  if (cnt > 0)
    a %= cnt;
  cnt += 2;
  rep(i, 0, n) {
    if (cnt > 0 and s[i] == '1') {
      b += power(2, n - i - 1, cnt);
    }
  }
  if (cnt > 0)
    b %= cnt;
  cnt--;
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (cnt > 0) {
        cnt--;
        a -= power(2, n - i - 1, cnt);
        a = (a % cnt + cnt) % cnt;
        cout << 1 + A[a] << endl;
        a += power(2, n - i - 1, cnt);
        a %= cnt;
        cnt++;
      } else {
        cout << 0 << endl;
      }
    } else {
      cnt++;
      b -= power(2, n - i - 1, cnt);
      b = (b % cnt + cnt) % cnt;
      cout << 1 + A[b] << endl;
      a += power(2, n - i - 1, cnt);
      b %= cnt;
      cnt--;
    }
  }
  return 0;
}