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

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define fo(i, n) for (long long i = 0; i < n; i++)
#define ll long long int
#define inf 999999999999999999
#define pb push_back
#define MOD 1000000007
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
using namespace std;
// cerr<< '\\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << "
// ms\\n" ; freopen("explicit.in", "r", stdin); freopen("explicit.out", "w",
// stdout);

void SieveOfEratosthenes() {
  bool prime[10000001];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= 10000000; p++) {
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= 10000000; i += p) {
        prime[i] = false;
      }
    }
  }
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b % a == 0)
    return a;
  return gcd(b % a, a);
}
ll nCrModpDP(ll n, ll r, ll p) {
  ll C[r + 1];
  memset(C, 0, sizeof(C));
  C[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, r); j > 0; j--)
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}
ll nCrModp(ll n, ll r, ll p) {
  // Base case
  if (r == 0)
    return 1;
  ll ni = n % p, ri = r % p;

  return (nCrModp(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p;
}
int32_t main() {
  fio ll j, i, n, t, l, r, z, two, f, c, h, o, k, m, maxi, d, x;
  string s, w;
  cin >> n >> s;
  w = s;
  c = 0;
  d = 0;
  o = 0;
  for (j = s.length() - 1; j >= 0; j--) {
    if (s[j] == '1') {
      o++;
    }
  }
  // cout<<o<<endl;
  two = 1;
  ll a[n], b[n];
  f = 0;
  for (j = s.length() - 1; j >= 0; j--) {
    if (s[j] == '1') {
      c = (c + two) % (o + 1);
    }
    a[d] = two;
    two = (two * 2) % (o + 1);
    d++;
  }
  d = 0;
  two = 1;
  f = 0;
  if (o != 1)
    for (j = s.length() - 1; j >= 0; j--) {
      if (s[j] == '1') {
        f = (f + two) % (o - 1);
      }
      b[d] = two;
      two = (two * 2) % (o - 1);
      d++;
    }
  k = c;
  r = o;
  z = f;

  for (i = 1; i <= n; i++) {
    c = k;
    o = r;
    f = z;
    if (s[i - 1] == '0') {
      o++;
      c = (c + a[n - i]) % o;
    } else {
      o--;
      if (o != 0)
        f = ((f - b[n - i]) % o + o) % o;
      c = f;
    }
    ll ans = 0;
    if (o == 0) {
      cout << ans << endl;
      continue;
    }
    while (c % o != 0) {
      c = c % o;
      o = 0;
      h = c;
      while (h != 0) {
        x = h % 2;
        if (x == 1)
          o++;
        h = h / 2;
      }
      ans++;
    }

    cout << ans + 1 << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define fo(i, n) for (long long i = 0; i < n; i++)
#define ll long long int
#define inf 999999999999999999
#define pb push_back
#define MOD 1000000007
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
using namespace std;
// cerr<< '\\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << "
// ms\\n" ; freopen("explicit.in", "r", stdin); freopen("explicit.out", "w",
// stdout);

void SieveOfEratosthenes() {
  bool prime[10000001];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= 10000000; p++) {
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= 10000000; i += p) {
        prime[i] = false;
      }
    }
  }
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b % a == 0)
    return a;
  return gcd(b % a, a);
}
ll nCrModpDP(ll n, ll r, ll p) {
  ll C[r + 1];
  memset(C, 0, sizeof(C));
  C[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, r); j > 0; j--)
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}
ll nCrModp(ll n, ll r, ll p) {
  // Base case
  if (r == 0)
    return 1;
  ll ni = n % p, ri = r % p;

  return (nCrModp(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p;
}
int32_t main() {
  fio ll j, i, n, t, l, r, c, h, o, k, m, maxi, d, x;
  string s, w;
  cin >> n >> s;
  w = s;
  c = 0;
  d = 0;
  o = 0;
  for (j = s.length() - 1; j >= 0; j--) {
    if (s[j] == '1') {
      c = c + pow(2, d);
      o++;
    }
    d++;
  }
  k = c;
  r = o;
  for (i = 1; i <= n; i++) {
    c = k;
    o = r;
    s = w;
    if (s[i - 1] == '0') {
      s[i - 1] = '1';
      o++;
      c = c + pow(2, n - i);
    } else {
      o--;
      c = c - pow(2, n - i);
      s[i - 1] = '0';
    }
    ll ans = 0;
    if (o == 0) {
      cout << ans << endl;
      continue;
    }
    while (c % o != 0) {
      c = c % o;
      o = 0;
      h = c;
      while (h != 0) {
        x = h % 2;
        if (x == 1)
          o++;
        h = h / 2;
      }
      ans++;
    }
    cout << ans + 1 << endl;
  }
}