Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (ri i = (a); i <= (b); ++i)
#define per(i, a, b) for (ri i = (a); i >= (b); --i)
#define ls (x << 1)
#define rs (x << 1 | 1)
#define mid (l + r >> 1)
#define de(x) cout << #x << " = " << x << endl
#define dd() cout << "Out Put!\n"
const int maxn = 1000010;
const double eps = 1e-8, PI = acos(-1.0);
const int inf = 0x7f7f7f7f;
using namespace std;
int a[maxn];
char s[maxn];
il int qpow(int base, int m, int mod) {
  int res = 1;
  while (m) {
    if (m & 1)
      (res *= base) %= mod;
    (base *= base) %= mod;
    m >>= 1ll;
  }
  return res % mod;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n >> s;
  int l = strlen(s) - 1;
  int sum = 0;
  rep(i, 0, l) sum += s[i] - '0';
  int d = 0, e = 0, f = 0;
  per(i, l, 0) if (s[i] == '1') {
    if (sum != 1)
      d += qpow(2, l - i, sum - 1);
    f += qpow(2, l - i, sum + 1);
  }
  a[0] = 0;
  rep(i, 1, 200010) {
    int x = i;
    int ans = 0;
    int f = 0;
    while (x) {
      int cnt = 0;
      rep(j, 0, 25) if ((1ll << j) & x) cnt++;
      if (!f)
        f = cnt;
      x = x % cnt;
      ans++;
    }
    a[i] = ans;
  }
  rep(i, 0, l) {
    if (s[i] == '1') {
      if (sum == 1)
        cout << 1 << endl;
      int p = d;
      p -= qpow(2, l - i, sum - 1);
      p = (p + sum - 1) % (sum - 1);
      cout << a[p] + 1 << endl;
    } else {
      int p = f;
      p += qpow(2, l - i, sum + 1);
      p %= (sum + 1);
      cout << a[p] + 1 << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (ri i = (a); i <= (b); ++i)
#define per(i, a, b) for (ri i = (a); i >= (b); --i)
#define ls (x << 1)
#define rs (x << 1 | 1)
#define mid (l + r >> 1)
#define de(x) cout << #x << " = " << x << endl
#define dd() cout << "Out Put!\n"
const int maxn = 1000010;
const double eps = 1e-8, PI = acos(-1.0);
const int inf = 0x7f7f7f7f;
using namespace std;
int a[maxn];
char s[maxn];
il int qpow(int base, int m, int mod) {
  int res = 1;
  while (m) {
    if (m & 1)
      (res *= base) %= mod;
    (base *= base) %= mod;
    m >>= 1ll;
  }
  return res % mod;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n >> s;
  int l = strlen(s) - 1;
  int sum = 0;
  rep(i, 0, l) sum += s[i] - '0';
  int d = 0, e = 0, f = 0;
  per(i, l, 0) if (s[i] == '1') {
    if (sum != 1)
      d += qpow(2, l - i, sum - 1);
    f += qpow(2, l - i, sum + 1);
  }
  a[0] = 0;
  rep(i, 1, 200010) {
    int x = i;
    int ans = 0;
    int f = 0;
    while (x) {
      int cnt = 0;
      rep(j, 0, 25) if ((1ll << j) & x) cnt++;
      if (!f)
        f = cnt;
      x = x % cnt;
      ans++;
    }
    a[i] = ans;
  }
  rep(i, 0, l) {
    if (s[i] == '1') {
      if (sum == 1) {
        cout << 0 << endl;
        continue;
      }
      int p = d;
      p -= qpow(2, l - i, sum - 1);
      p = (p + sum - 1) % (sum - 1);
      cout << a[p] + 1 << endl;
    } else {
      int p = f;
      p += qpow(2, l - i, sum + 1);
      p %= (sum + 1);
      cout << a[p] + 1 << endl;
    }
  }
  return 0;
}