Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

int main() {
  int N;
  string S;
  cin >> N >> S;
  int cnt = 0;
  rep(i, N) cnt += S[i] - '0';
  int a[2];
  a[0] = cnt - 1, a[1] = cnt + 1;
  int x[N][2], sum[2];
  rep(i, 2) {
    if (a[i] <= 0)
      continue;
    x[N - 1][i] = 1 % a[i], sum[i] = 0;
    if (S[N - 1] == '1')
      sum[i] = 1;
    rep3(j, N - 2, 0) {
      x[j][i] = (x[j + 1][i] * 2) % a[i];
      if (S[j] == '1')
        sum[i] += x[j][i], sum[i] %= a[i];
    }
  }
  rep(i, N) {
    int j = S[i] == '0';
    int now = sum[j], ans = 1;
    if (!j)
      now += (a[j] - x[i][j]);
    else
      now += x[i][j];
    now %= a[j];
    while (now) {
      now %= (__builtin_popcount(now));
      ans++;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

int main() {
  int N;
  string S;
  cin >> N >> S;
  int cnt = 0;
  rep(i, N) cnt += S[i] - '0';
  int a[2];
  a[0] = cnt - 1, a[1] = cnt + 1;
  int x[N][2], sum[2];
  rep(i, 2) {
    if (a[i] <= 0)
      continue;
    x[N - 1][i] = 1 % a[i], sum[i] = 0;
    if (S[N - 1] == '1')
      sum[i] = 1;
    rep3(j, N - 2, 0) {
      x[j][i] = (x[j + 1][i] * 2) % a[i];
      if (S[j] == '1')
        sum[i] += x[j][i], sum[i] %= a[i];
    }
  }
  rep(i, N) {
    int j = S[i] == '0';
    if (a[j] == 0) {
      cout << 0 << endl;
      continue;
    }
    int now = sum[j], ans = 1;
    if (!j)
      now += (a[j] - x[i][j]);
    else
      now += x[i][j];
    now %= a[j];
    while (now) {
      now %= (__builtin_popcount(now));
      ans++;
    }
    cout << ans << endl;
  }
}