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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  string S;
  cin >> S;
  reverse(all(S));
  int K = S.size();
  int pcnt = 0;
  rep(i, K) if (S[i] == '1') pcnt++;
  int X = pcnt - 1;
  int Y = pcnt + 1;
  vi dp1(N + 1, 1), dp2(N + 1, 1);
  if (X > 0) {
    rep(i, N) {
      dp1[i + 1] = dp1[i] * 2;
      dp1[i + 1] %= X;
    }
  }
  rep(i, N) {
    dp2[i + 1] = dp2[i] * 2;
    dp2[i + 1] %= Y;
  }
  vi a(N);
  int A = 0, B = 0;
  if (X > 0) {
    rep(i, N) {
      if (S[i] == '1') {
        A += dp1[i];
        A %= X;
      }
    }
  }
  rep(i, N) {
    if (S[i] == '1') {
      B += dp2[i];
      B %= Y;
    }
  }
  rep(i, N) {
    if (S[i] == '1' && X > 0) {
      a[i] = A;
      a[i] -= dp1[i];
      a[i] += X;
      a[i] %= X;
    } else {
      a[i] = B;
      a[i] += dp2[i];
      a[i] %= Y;
    }
  }
  RFOR(i, N - 1, 0) {
    if (X == 0 && S[i] == '1') {
      cout << "0\\n";
      continue;
    }
    int ans = 1;
    while (a[i]) {
      a[i] %= __builtin_popcount(a[i]);
      ans++;
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  string S;
  cin >> S;
  reverse(all(S));
  int K = S.size();
  int pcnt = 0;
  rep(i, K) if (S[i] == '1') pcnt++;
  int X = pcnt - 1;
  int Y = pcnt + 1;
  vi dp1(N + 1, 1), dp2(N + 1, 1);
  rep(i, N) {
    dp1[i + 1] = dp1[i] * 2;
    dp1[i + 1] %= X;
  }
  rep(i, N) {
    dp2[i + 1] = dp2[i] * 2;
    dp2[i + 1] %= Y;
  }
  vi a(N);
  int A = 0, B = 0;
  rep(i, N) {
    if (S[i] == '1') {
      A += dp1[i];
      A %= X;
    }
  }
  rep(i, N) {
    if (S[i] == '1') {
      B += dp2[i];
      B %= Y;
    }
  }
  rep(i, N) {
    if (S[i] == '1') {
      a[i] = A;
      a[i] -= dp1[i];
      a[i] += X;
      a[i] %= X;
    } else {
      a[i] = B;
      a[i] += dp2[i];
      a[i] %= Y;
    }
  }
  RFOR(i, N - 1, 0) {
    int ans = 1;
    while (a[i]) {
      a[i] %= __builtin_popcount(a[i]);
      ans++;
    }
    cout << ans << "\\n";
  }
  return 0;
}