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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, int>
#define T tuple<int, int, int>
using namespace std;

ll ni(ll a, ll b, ll c) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return ((ni(a, b / 2, c) % c) * (ni(a, b / 2, c) % c)) % c;
  else
    return ni(a, b - 1, c) * a;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll one = 0;
  rep(i, n) {
    if (s[i] == '1')
      one++;
  }
  vector<ll> amari0(n, 0);
  vector<ll> amari1(n, 0);
  amari0[n - 1] = 1 % (one + 1);
  if (one > 1)
    amari1[n - 1] = 1 % (one - 1);

  rep(i, n - 1) {
    amari0[n - i - 2] = (amari0[n - 1] * ni(2, i + 1, one + 1)) % (one + 1);
    if (one > 1)
      amari1[n - i - 2] = (amari1[n - 1] * ni(2, i + 1, one - 1)) % (one - 1);
  }

  ll fund0, fund1;
  fund0 = fund1 = 0;
  if (one == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }
  if (one == 1) {
    rep(i, n) {
      if (s[i] == '1') {
        cout << 0 << endl;
        continue;
      } else {
        if (s[n - 1] == '1' || i == n - 1)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
    return 0;
  }
  rep(i, n) {
    if (s[i] == '1') {
      fund0 += amari0[i];
      fund1 += amari1[i];
    }
  }
  rep(i, n) {
    ll tone;
    ll tmp = 0;
    ll fund00 = fund0;
    ll fund11 = fund1;
    if (s[i] == '0') {
      fund00 += amari0[i];
      fund00 %= (one + 1);
      while (fund00) {
        tmp++;
        ll now = fund00;
        tone = 0;
        while (now) {
          if (now % 2 == 1)
            tone++;
          now /= 2;
        }
        fund00 %= tone;
      }
    } else {
      fund11 -= amari1[i];
      fund11 %= (one - 1);
      while (fund11) {
        tmp++;
        ll now = fund11;
        tone = 0;
        while (now) {
          if (now % 2 == 1)
            tone++;
          now /= 2;
        }
        fund11 %= tone;
      }
    }
    cout << tmp + 1 << endl;
    ;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, int>
#define T tuple<int, int, int>
using namespace std;

ll ni(ll a, ll b, ll c) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return ((ni(a, b / 2, c) % c) * (ni(a, b / 2, c) % c)) % c;
  else
    return ni(a, b - 1, c) * a;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll one = 0;
  rep(i, n) {
    if (s[i] == '1')
      one++;
  }
  vector<ll> amari0(n, 0);
  vector<ll> amari1(n, 0);
  amari0[n - 1] = 1 % (one + 1);
  if (one > 1)
    amari1[n - 1] = 1 % (one - 1);

  rep(i, n - 1) {
    amari0[n - i - 2] = (amari0[n - 1] * ni(2, i + 1, one + 1)) % (one + 1);
    if (one > 1)
      amari1[n - i - 2] = (amari1[n - 1] * ni(2, i + 1, one - 1)) % (one - 1);
  }

  ll fund0, fund1;
  fund0 = fund1 = 0;
  if (one == 0) {
    rep(i, n) cout << 1 << endl;
    return -1;
  }
  if (one == 1) {
    rep(i, n) {
      if (s[i] == '1') {
        cout << 1 << endl;
        continue;
      } else {
        if (s[n - 1] == '1')
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
    return -1;
  }
  rep(i, n) {
    if (s[i] == '1') {
      fund0 += amari0[i];
      fund1 += amari1[i];
    }
  }
  rep(i, n) {
    ll tone;
    ll tmp = 0;
    ll fund00 = fund0;
    ll fund11 = fund1;
    if (s[i] == '0') {
      fund00 += amari0[i];
      fund00 %= (one + 1);
      while (fund00) {
        tmp++;
        ll now = fund00;
        tone = 0;
        while (now) {
          if (now % 2 == 1)
            tone++;
          now /= 2;
        }
        fund00 %= tone;
      }
    } else {
      fund11 -= amari1[i];
      fund11 %= (one - 1);
      while (fund11) {
        tmp++;
        ll now = fund11;
        tone = 0;
        while (now) {
          if (now % 2 == 1)
            tone++;
          now /= 2;
        }
        fund11 %= tone;
      }
    }
    cout << tmp + 1 << endl;
    ;
  }

  return 0;
}