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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<tuple<ll, ll, ll>>>;

// MOD確認

ll N;
string X;

ll A[200010];
ll B[200010];

int main() {
  cin >> N >> X;
  ll cnt = 0;
  rep(i, N) if (X[i] == '1') cnt++;
  ll MODA = cnt + 1, MODB = cnt - 1;
  if (cnt == 0) {
    rep(i, N) cout << 1 << endl;
    return 0;
  }
  if (MODB == 0) {
    rep(i, N) {
      if (X[i] == '1')
        cout << 0 << endl;
      else {
        if (X.back() == '0' && i != N - 1)
          cout << 1 << endl;
        else
          cout << 2 << endl;
      }
    }

    return 0;
  }
  if (MODA != 1)
    A[0] = 1;
  if (MODB != 1)
    B[0] = 1;
  ll sumA = 0, sumB = 0;
  rep(i, N) {
    A[i + 1] = A[i] * 2;
    A[i + 1] %= MODA;
  }
  rep(i, N) {
    B[i + 1] = B[i] * 2;
    B[i + 1] %= MODB;
  }

  rep(i, N) if (X[i] == '1') sumA += A[N - i - 1];
  rep(i, N) if (X[i] == '1') sumB += B[N - i - 1];
  sumA %= MODA;
  sumB %= MODB;

  /*rep(i,N) cout << A[i] << ' ';
  cout << endl;
  rep(i,N) cout << B[i] << ' ';
  cout << endl;
  cout << MODA << ' ' << MODB << endl;
  cout << sumA << ' ' << sumB << endl;*/
  rep(i, N) {
    ll n = 0;
    ll num = 0;
    if (X[i] == '0') {
      num = MODA;
      n = sumA + A[N - i - 1];
    } else {
      num = MODB;
      n = sumB - B[N - i - 1];
      while (n < 0)
        n += num * 10000000LL;
    }
    n %= num;
    ll ans = 1;
    while (n) {
      ans++;
      n %= __builtin_popcount(n);
    }
    cout << ans << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<tuple<ll, ll, ll>>>;

// MOD確認

ll N;
string X;

ll A[200010];
ll B[200010];

int main() {
  cin >> N >> X;
  ll cnt = 0;
  rep(i, N) if (X[i] == '1') cnt++;
  ll MODA = cnt + 1, MODB = cnt - 1;
  if (cnt == 0) {
    rep(i, N) cout << 1 << endl;
    return 0;
  }
  /*if(MODB == 0){
     rep(i,N){
        if(X[i] == '1') cout << 0 << endl;
        else{
           if(X.back() == '0') cout << 1 << endl;
           else cout << 2 << endl;
        }
     }

     return 0;

  }*/
  if (MODA != 1)
    A[0] = 1;
  if (MODB != 1)
    B[0] = 1;
  ll sumA = 0, sumB = 0;
  rep(i, N) {
    A[i + 1] = A[i] * 2;
    A[i + 1] %= MODA;
  }
  rep(i, N) {
    B[i + 1] = B[i] * 2;
    B[i + 1] %= MODB;
  }

  rep(i, N) if (X[i] == '1') sumA += A[N - i - 1];
  rep(i, N) if (X[i] == '1') sumB += B[N - i - 1];
  sumA %= MODA;
  sumB %= MODB;

  /*rep(i,N) cout << A[i] << ' ';
  cout << endl;
  rep(i,N) cout << B[i] << ' ';
  cout << endl;
  cout << MODA << ' ' << MODB << endl;
  cout << sumA << ' ' << sumB << endl;*/
  rep(i, N) {
    ll n = 0;
    ll num = 0;
    if (X[i] == '0') {
      num = MODA;
      n = sumA + A[N - i - 1];
    } else {
      num = MODB;
      n = sumB - B[N - i - 1];
      while (n < 0)
        n += num * 10000000LL;
    }
    if (n == 0) {
      cout << 0 << endl;
      continue;
    }
    n %= num;
    ll ans = 1;
    while (n) {
      ans++;
      n %= __builtin_popcount(n);
    }
    cout << ans << endl;
  }
}