Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios                                                                    \
  ;                                                                            \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 998244353;
const int N = 1e6 + 5;
const int M = 1e3 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int n, m, k;
char s[N];
int a[N];
ll qsm(ll x, ll y, ll MOD) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return res;
}

int main() {
  ios;
  cin >> n;
  cin >> s + 1;
  int res = 0, cnt = 0;
  rep(i, 1, n) {
    if (s[i] == '1')
      ++cnt;
    a[n - i + 1] = s[i] - '0';
  }
  int res1 = res, res2 = res;
  int cnt1 = cnt - 1, cnt2 = cnt + 1;
  rep(i, 1, n) {
    if (a[i]) {
      res1 = (res1 + qsm(2, i - 1, cnt1)) % cnt1;
      res2 = (res2 + qsm(2, i - 1, cnt2)) % cnt2;
    }
  }
  per(i, n, 1) {
    int tmp;
    if (a[i]) {
      if (cnt1 == 0) {
        cout << "0\n";
        continue;
      }
      tmp = (res1 - qsm(2, i - 1, cnt1) + cnt1) % cnt1;
    } else {
      tmp = (res2 + qsm(2, i - 1, cnt2) + cnt2) % cnt2;
    }
    int ans = 1;
    while (tmp) {
      cnt = __builtin_popcount(tmp);
      tmp %= cnt;
      ++ans;
    }
    cout << ans << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios                                                                    \
  ;                                                                            \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 998244353;
const int N = 1e6 + 5;
const int M = 1e3 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int n, m, k;
char s[N];
int a[N];
ll qsm(ll x, ll y, ll MOD) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return res;
}

int main() {
  ios;
  cin >> n;
  cin >> s + 1;
  int res = 0, cnt = 0;
  rep(i, 1, n) {
    if (s[i] == '1')
      ++cnt;
    a[n - i + 1] = s[i] - '0';
  }
  int res1 = res, res2 = res;
  int cnt1 = cnt - 1, cnt2 = cnt + 1;
  rep(i, 1, n) {
    if (a[i]) {
      if (cnt1 > 0)
        res1 = (res1 + qsm(2, i - 1, cnt1)) % cnt1;
      res2 = (res2 + qsm(2, i - 1, cnt2)) % cnt2;
    }
  }
  per(i, n, 1) {
    int tmp;
    if (a[i]) {
      if (cnt1 == 0) {
        cout << "0\n";
        continue;
      }
      tmp = (res1 - qsm(2, i - 1, cnt1) + cnt1) % cnt1;
    } else {
      tmp = (res2 + qsm(2, i - 1, cnt2) + cnt2) % cnt2;
    }
    int ans = 1;
    while (tmp) {
      cnt = __builtin_popcount(tmp);
      tmp %= cnt;
      ++ans;
    }
    cout << ans << "\n";
  }
  return 0;
}