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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll ama1[200010], ama2[200010];
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  ll cnt1 = cnt + 1;
  ll cnt2 = cnt - 1;
  string t = s;
  ama1[0] = 1 % cnt1;
  for (ll i = 1; i < 200010; i++) {
    ll now = ama1[i - 1] * 2;
    ama1[i] = now % cnt1;
  }
  if (cnt2 != 0) {
    ama2[0] = 1 % cnt2;
  }
  for (ll i = 1; i < 200010; i++) {
    if (cnt2 == 0) {
      continue;
    }
    ll now = ama2[i - 1] * 2;
    ama2[i] = now % cnt2;
  }
  reverse(t.begin(), t.end());
  ll ans1 = 0, ans2 = 0;
  for (ll i = 0; i < n; i++) {
    if (t[i] == '1') {
      ans1 += ama1[i];
      ans1 %= cnt1;
    }
  }
  for (ll i = 0; i < n; i++) {
    if (cnt2 == 0) {
      continue;
    }
    if (t[i] == '1') {
      ans2 += ama2[i];
      ans2 %= cnt2;
    }
  }
  //	cout <<ans1;
  ll fi[200010];
  for (ll i = 0; i < n; i++) {
    ll now1 = 0;
    if (t[i] == '0') {
      now1 = ans1;
      now1 += ama1[i];
      now1 %= cnt1;
    } else {
      if (cnt2 == 0) {
        fi[i] = 0;
        continue;
      }
      now1 = ans2;
      now1 -= ama2[i];
      now1 %= cnt2;
      if (now1 < 0) {
        now1 += cnt2;
      }
    }
    if (now1 == 0) {
      fi[i] = 1;
      continue;
    }
    ll aa = 1;
    while (now1 != 0) {
      ll cnnt = 0;
      ll now = now1;
      while (now != 0) {
        if (now % 2 != 0) {
          cnnt++;
        }
        now /= 2;
      }
      now1 %= cnnt;
      aa++;
    }
    fi[i] = aa;
  }
  for (ll i = n - 1; i >= 0; i--) {
    cout << fi[i] << endl;
  }
  //	cout <<cnt;
  // your code goes here
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll ama1[200010], ama2[200010];
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  ll cnt1 = cnt + 1;
  ll cnt2 = cnt - 1;
  string t = s;
  ama1[0] = 1 % cnt1;
  for (ll i = 1; i < 200010; i++) {
    ll now = ama1[i - 1] * 2;
    ama1[i] = now % cnt1;
  }
  ama2[0] = 1 % cnt2;
  for (ll i = 1; i < 200010; i++) {
    ll now = ama2[i - 1] * 2;
    ama2[i] = now % cnt2;
  }
  reverse(t.begin(), t.end());
  ll ans1 = 0, ans2 = 0;
  for (ll i = 0; i < n; i++) {
    if (t[i] == '1') {
      ans1 += ama1[i];
      ans1 %= cnt1;
    }
  }
  for (ll i = 0; i < n; i++) {
    if (t[i] == '1') {
      ans2 += ama2[i];
      ans2 %= cnt2;
    }
  }
  //	cout <<ans1;
  ll fi[200010];
  for (ll i = 0; i < n; i++) {
    ll now1 = 0;
    if (t[i] == '0') {
      now1 = ans1;
      now1 += ama1[i];
      now1 %= cnt1;
    } else {
      now1 = ans2;
      now1 -= ama2[i];
      now1 %= cnt2;
      if (now1 < 0) {
        now1 += cnt2;
      }
    }
    if (now1 == 0) {
      fi[i] = 1;
      continue;
    }
    ll aa = 1;
    while (now1 != 0) {
      ll cnnt = 0;
      ll now = now1;
      while (now != 0) {
        if (now % 2 != 0) {
          cnnt++;
        }
        now /= 2;
      }
      now1 %= cnnt;
      aa++;
    }
    fi[i] = aa;
  }
  for (ll i = n - 1; i >= 0; i--) {
    cout << fi[i] << endl;
  }
  //	cout <<cnt;
  // your code goes here
  return 0;
}