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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9 + 7;
const int N = 2e5 + 6;
ll p2p[N];
ll p2n[N];
int popcnt(int x) {
  int ret = 0;
  while (x) {
    ret += x % 2;
    x /= 2;
  }
  return ret;
}
int calc(int x, int y) {
  if (x == 0)
    return 0;
  return 1 + calc(x % y, popcnt(x % y));
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << endl;
    return;
  }
  ll cntp = cnt + 1;
  ll cntn = cnt - 1;
  ll modp = 0;
  ll modn = 0;
  reverse(s.begin(), s.end());
  p2p[0] = p2n[0] = 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      modp += p2p[i];
      modp %= cntp;
      if (cntn)
        modn += p2n[i];
      if (cntn)
        modn %= cntn;
    }
    p2p[i + 1] = p2p[i] * 2;
    if (cntn)
      p2n[i + 1] = p2n[i] * 2;
    p2p[i + 1] %= cntp;
    if (cntn)
      p2n[i + 1] %= cntn;
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cntn == 0)
        ans[i] = 0;
      else {
        ll trt = modn;
        trt = ((trt - p2n[i]) % cntn + cntn) % cntn;
        ans[i] = calc(trt, popcnt(trt)) + 1;
      }
    } else {

      ll trt = (modp + p2p[i]) % cntp;
      ans[i] = calc(trt, popcnt(trt)) + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  for (int x : ans)
    cout << x << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9 + 7;
const int N = 1e5 + 6;
ll p2p[N];
ll p2n[N];
int popcnt(int x) {
  int ret = 0;
  while (x) {
    ret += x % 2;
    x /= 2;
  }
  return ret;
}
int calc(int x, int y) {
  if (x == 0)
    return 0;
  return 1 + calc(x % y, popcnt(x % y));
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  ll cntp = cnt + 1;
  ll cntn = cnt - 1;
  ll modp = 0;
  ll modn = 0;
  reverse(s.begin(), s.end());
  p2p[0] = p2n[0] = 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      modp += p2p[i];
      modp %= cntp;
      if (cntn)
        modn += p2n[i];
      if (cntn)
        modn %= cntn;
    }
    p2p[i + 1] = p2p[i] * 2;
    if (cntn)
      p2n[i + 1] = p2n[i] * 2;
    p2p[i + 1] %= cntp;
    if (cntn)
      p2n[i + 1] %= cntn;
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cntn == 0)
        ans[i] = 0;
      else {
        ll trt = modn;
        trt = ((trt - p2n[i]) % cntn + cntn) % cntn;
        ans[i] = calc(trt, popcnt(trt)) + 1;
      }
    } else {

      ll trt = (modp + p2p[i]) % cntp;
      ans[i] = calc(trt, popcnt(trt)) + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  for (int x : ans)
    cout << x << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}