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

#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e9;
const int mod = 1e9 + 7;
#define ll long long
int p[MX][2];
int a[MX][2];

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int pp = 0;
  rep(i, n) {
    if (s[i] == '1')
      pp++;
  }
  int now = 1;
  int tm = s[n - 1] - '0';
  if (pp > 1)
    p[n - 1][0] = tm % (pp - 1);
  p[n - 1][1] = tm % (pp + 1);
  // cout << tm%(pp-1)<<p[n-1][1] << endl;
  int k = 1;
  int kk = 1;
  a[n - 1][0] = k;
  a[n - 1][1] = kk;
  RFOR(i, n - 2, 0) {
    tm = s[i] - '0';
    k *= 2;
    kk *= 2;
    if (pp > 1)
      k %= (pp - 1);
    kk %= (pp + 1);
    a[i][0] = k;
    a[i][1] = kk;
    if (pp > 1)
      p[i][0] = (tm * k + p[i + 1][0]) % (pp - 1);
    p[i][1] = (tm * kk + p[i + 1][1]) % (pp + 1);
    // cout << i<<" "<<p[i][1] << endl;
  }
  // cout << pp << endl;
  rep(i, n) {
    int ans = 1;
    int y = 0;
    if (s[i] == '0') {
      y = p[0][1] + a[i][1];
      y %= (pp + 1);
      // cout << "y "<<p[0][1]<<" + "<<a[i][1]<<" "<<y << endl;
    } else {
      if (pp > 1) {
        y = p[0][0] - a[i][0];
        y %= pp - 1;
        if (y < 0)
          y += pp - 1;
      } else {
        ans = 0;
      }
      //  cout << "y "<<p[0][0]<<" - "<<a[i][0]<<" "<<y << endl;
    }

    while (y > 0) {
      int cy = y;
      int ppp = 0;
      while (cy > 0) {
        ppp += cy % 2;
        cy /= 2;
      }
      y = y % ppp;
      ans++;
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e9;
const int mod = 1e9 + 7;
#define ll long long
int p[MX][2];
int a[MX][2];

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int pp = 0;
  rep(i, n) {
    if (s[i] == '1')
      pp++;
  }
  int now = 1;
  int tm = s[n - 1] - '0';
  if (pp > 1)
    p[n - 1][0] = tm % (pp - 1);
  p[n - 1][1] = tm % (pp + 1);
  // cout << tm%(pp-1)<<p[n-1][1] << endl;
  int k = 1;
  int kk = 1;
  a[n - 1][0] = k;
  a[n - 1][1] = kk;
  RFOR(i, n - 2, 0) {
    tm = s[i] - '0';
    k *= 2;
    kk *= 2;
    if (pp > 1)
      k %= (pp - 1);
    kk %= (pp + 1);
    a[i][0] = k;
    a[i][1] = kk;
    if (pp > 1)
      p[i][0] = (tm * k + p[i + 1][0]) % (pp - 1);
    p[i][1] = (tm * kk + p[i + 1][1]) % (pp + 1);
    // cout << i<<" "<<p[i][1] << endl;
  }
  // cout << pp << endl;
  rep(i, n) {
    int ans = 1;
    int y = 0;
    if (s[i] == '0') {
      y = p[0][1] + a[i][1];
      y %= (pp + 1);
      // cout << "y "<<p[0][1]<<" + "<<a[i][1]<<" "<<y << endl;
    } else {
      y = p[0][0] - a[i][0];
      y %= pp - 1;
      if (y < 0)
        y += pp - 1;
      //  cout << "y "<<p[0][0]<<" - "<<a[i][0]<<" "<<y << endl;
    }

    while (y > 0) {
      int cy = y;
      int ppp = 0;
      while (cy > 0) {
        ppp += cy % 2;
        cy /= 2;
      }
      y = y % ppp;
      ans++;
    }

    cout << ans << endl;
  }
  return 0;
}