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

/*
        Author: Dinesh Verra
        College: ABV-IIITM
        Date: 10/07/2020
*/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define cu continue
#define br break
#define pb push_back
#define eb emplace_back
// #define mod 1000000007
#define inf LLONG_MAX
#define pll pair<ll, ll>
#define min_pq priority_queue<pll>, vector<pll>, greater<pll> >>
#define F first
#define S second
#define vll vector<ll>
#define vpll vector<pll>
#define dbg(n) cout << #n << ' ' << n << endl;
#define all(v) v.begin(), v.end()
#define nl cout << '\\n'

template <typename A1> void prn(A1 &&arg) { cout << arg << '\\n'; }
template <typename A1, typename... A> void prn(A1 &&arg, A &&...args) {
  cout << arg << ' ';
  prn(args...);
}

ll mpow(ll x, ll y, ll mod) {
  if (mod == 0)
    return 0;
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("/home/dinesh_verra/cpp/input.txt", "r", stdin);
// freopen("/home/dinesh_verra/cpp/output.txt","w",stdout);
#endif

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll x = 0;
  for (ll i = 0; i < n; i++)
    if (s[i] == '1')
      x++;
  ll a[3][n + 5], idx = -1;
  for (ll i = 0; i <= n; i++)
    a[0][i] = 0, a[1][i] = 0, a[2][i] = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      a[0][i] = mpow(2ll, (n - i - 1), x - 1);
      a[1][i] = mpow(2ll, (n - i - 1), x);
      a[2][i] = mpow(2ll, (n - i - 1), x + 1);
      idx = i;
    } else {
      a[0][i] = 0;
      a[1][i] = 0;
      a[2][i] = 0;
    }
  }
  for (ll i = 1; i < n; i++) {
    a[0][i] = (a[0][i - 1] + a[0][i]);
    if (x != 1)
      a[0][i] %= (x - 1);
    a[1][i] = (a[1][i - 1] + a[1][i]);
    if (x != 0)
      a[1][i] %= x;
    a[2][i] = (a[2][i - 1] + a[2][i]) % (x + 1);
  }
  ll ans[n + 5], tmp;
  ll b[n + 5], y;
  for (ll i = 0; i < n; i++) {
    b[i] = 1;
    if (x == 1 && idx == i) {
      b[i] = 0;
    }
    if (s[i] == '1' && (x != 1)) {
      tmp = a[0][n - 1];
      tmp = (tmp - mpow(2ll, n - i - 1, x - 1) + (x - 1)) % (x - 1);
      ans[i] = tmp;
    } else {
      tmp = a[2][n - 1];
      tmp = (tmp + mpow(2ll, n - i - 1, x + 1)) % (x + 1);
      ans[i] = tmp;
    }
  }
  for (ll i = 0; i < n; i++) {
    tmp = ans[i];
    while (tmp > 0) {
      y = __builtin_popcountll(tmp);
      tmp %= y;
      b[i]++;
    }
    prn(b[i]);
  }
}

Error: WA

Fixed submission:

/*
        Author: Dinesh Verra
        College: ABV-IIITM
        Date: 10/07/2020
*/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define cu continue
#define br break
#define pb push_back
#define eb emplace_back
// #define mod 1000000007
#define inf LLONG_MAX
#define pll pair<ll, ll>
#define min_pq priority_queue<pll>, vector<pll>, greater<pll> >>
#define F first
#define S second
#define vll vector<ll>
#define vpll vector<pll>
#define dbg(n) cout << #n << ' ' << n << endl;
#define all(v) v.begin(), v.end()
#define nl cout << '\\n'

template <typename A1> void prn(A1 &&arg) { cout << arg << '\\n'; }
template <typename A1, typename... A> void prn(A1 &&arg, A &&...args) {
  cout << arg << ' ';
  prn(args...);
}

ll mpow(ll x, ll y, ll mod) {
  if (mod == 0)
    return 0;
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("/home/dinesh_verra/cpp/input.txt", "r", stdin);
// freopen("/home/dinesh_verra/cpp/output.txt","w",stdout);
#endif

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll x = 0;
  for (ll i = 0; i < n; i++)
    if (s[i] == '1')
      x++;
  ll a[3][n + 5], idx = -1;
  for (ll i = 0; i <= n; i++)
    a[0][i] = 0, a[1][i] = 0, a[2][i] = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      a[0][i] = mpow(2ll, (n - i - 1), x - 1);
      a[1][i] = mpow(2ll, (n - i - 1), x);
      a[2][i] = mpow(2ll, (n - i - 1), x + 1);
      idx = i;
    } else {
      a[0][i] = 0;
      a[1][i] = 0;
      a[2][i] = 0;
    }
  }
  for (ll i = 1; i < n; i++) {
    a[0][i] = (a[0][i - 1] + a[0][i]) % (x - 1);
    a[1][i] = (a[1][i - 1] + a[1][i]) % (x);
    a[2][i] = (a[2][i - 1] + a[2][i]) % (x + 1);
  }
  ll ans[n + 5], tmp;
  ll b[n + 5], y;
  for (ll i = 0; i < n; i++) {
    b[i] = 1;
    if (x == 1 && idx == i) {
      b[i] = 0;
    }
    if (s[i] == '1' && (x != 1)) {
      tmp = a[0][n - 1];
      tmp = (tmp - mpow(2ll, n - i - 1, x - 1) + (x - 1)) % (x - 1);
      ans[i] = tmp;
    } else {
      tmp = a[2][n - 1];
      tmp = (tmp + mpow(2ll, n - i - 1, x + 1)) % (x + 1);
      ans[i] = tmp;
    }
  }
  for (ll i = 0; i < n; i++) {
    tmp = ans[i];
    while (tmp > 0) {
      y = __builtin_popcountll(tmp);
      tmp %= y;
      b[i]++;
    }
    prn(b[i]);
  }
}