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

// The moment you think of giving up you have already lost the battle
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI 3.14159265358979323846
#define M 1000000007
ll fast(ll n, ll a, ll m) {
  if (n == 0)
    return 1;
  else if (n == 1)
    return a;
  else {
    ll r = fast(n / 2, a, m);
    if (n % 2 == 0) {
      return (((r % m) * (r % m)) % m);
    } else {
      return (((((r % m) * a) % m) * r) % m);
    }
  }
}
const ll N = 2 * 1e5 + 5;
ll po2[N + 1][2];
ll z[N + 1];
int main() {
  memset(po2, 0, sizeof(po2));
  z[0] = 0;
  ll i, j;
  for (i = 1; i <= N; i++) {
    ll r = 0;
    ll u = i;
    while (u > 0) {
      r += (u % 2);
      u /= 2;
    }
    z[i] = z[i % r] + 1;
  }
  ll n;
  cin >> n;
  string S;
  cin >> S;
  ll X = 0;
  for (i = 0; i < n; i++) {
    X += (S[i] - '0');
  }
  if (X == 0) {
    for (i = 0; i < n; i++)
      cout << 1 << "\\n";
  } else if (X == 1) {
    if (n == 1) {
      cout << 0 << "\\n";
    } else {
      if (S[n - 1] == '1') {
        for (i = 0; i < n - 1; i++)
          cout << 2 << "\\n";

        cout << 0 << "\\n";
      } else {
        for (i = 0; i < n; i++) {
          if (S[i] == '1') {
            cout << 0 << "\\n";
          } else {
            if (i == n - 1)
              cout << 2 << "\\n";
            else
              cout << 1 << "\\n";
          }
        }
      }
    }

  } else {
    ll Y = X + 1;
    X = X - 1;
    po2[0][0] = 1 % X;
    po2[0][1] = 1 % Y;
    for (i = 1; i <= n; i++) {
      po2[i][0] = (po2[i - 1][0] * 2) % X;
      po2[i][1] = (po2[i - 1][1] * 2) % Y;
    }
    ll pr[n + 1][2];
    memset(pr, 0, sizeof(pr));
    ll sf[n + 1][2];
    memset(sf, 0, sizeof(sf));
    for (i = 1; i <= n; i++) {
      ll d = S[i - 1] - '0';
      pr[i][0] = (pr[i - 1][0] % X + (d * po2[n - i][0]) % X) % X;
      pr[i][1] = (pr[i - 1][1] % Y + (d * po2[n - i][1]) % Y) % Y;
    }
    ll j = 1;
    for (i = n; i >= 1; i--) {
      ll d = S[i - 1] - '0';
      sf[j][0] = (sf[j - 1][0] % X + (d * po2[n - i][0]) % X) % X;
      sf[j][1] = (sf[j - 1][1] % Y + (d * po2[n - i][1] % Y)) % Y;
      j++;
    }
    j = n - 1;
    for (i = 1; i <= n; i++) {
      if (S[i - 1] == '0') {
        ll p = (pr[i - 1][1] % Y + sf[j][1] % Y) % Y;
        p = (p % Y + po2[n - i][1] % Y) % Y;
        cout << z[p] + 1 << "\\n";
      } else {
        ll p = (pr[i - 1][0] % X + sf[j][0] % X) % X;

        cout << z[p] + 1 << "\\n";
      }
      j--;
    }
  }
}


Error: 0

Fixed submission:

// The moment you think of giving up you have already lost the battle
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI 3.14159265358979323846
#define M 1000000007
ll fast(ll n, ll a, ll m) {
  if (n == 0)
    return 1;
  else if (n == 1)
    return a;
  else {
    ll r = fast(n / 2, a, m);
    if (n % 2 == 0) {
      return (((r % m) * (r % m)) % m);
    } else {
      return (((((r % m) * a) % m) * r) % m);
    }
  }
}
const ll N = 2 * 1e5 + 5;
ll po2[N + 1][2];
ll z[N + 1];
int main() {
  memset(po2, 0, sizeof(po2));
  z[0] = 0;
  ll i, j;
  for (i = 1; i <= N; i++) {
    ll r = 0;
    ll u = i;
    while (u > 0) {
      r += (u % 2);
      u /= 2;
    }
    z[i] = z[i % r] + 1;
  }
  ll n;
  cin >> n;
  string S;
  cin >> S;
  ll X = 0;
  for (i = 0; i < n; i++) {
    X += (S[i] - '0');
  }
  if (X == 0) {
    for (i = 0; i < n; i++)
      cout << 1 << "\\n";
  }

  else {
    ll Y = X + 1;
    X = X - 1;
    po2[0][0] = 1 % X;
    po2[0][1] = 1 % Y;
    for (i = 1; i <= n; i++) {
      po2[i][0] = (po2[i - 1][0] * 2) % X;
      po2[i][1] = (po2[i - 1][1] * 2) % Y;
    }
    ll pr[n + 1][2];
    memset(pr, 0, sizeof(pr));
    ll sf[n + 1][2];
    memset(sf, 0, sizeof(sf));
    for (i = 1; i <= n; i++) {
      ll d = S[i - 1] - '0';
      pr[i][0] = (pr[i - 1][0] % X + (d * po2[n - i][0]) % X) % X;
      pr[i][1] = (pr[i - 1][1] % Y + (d * po2[n - i][1]) % Y) % Y;
    }
    ll j = 1;
    for (i = n; i >= 1; i--) {
      ll d = S[i - 1] - '0';
      sf[j][0] = (sf[j - 1][0] % X + (d * po2[n - i][0]) % X) % X;
      sf[j][1] = (sf[j - 1][1] % Y + (d * po2[n - i][1] % Y)) % Y;
      j++;
    }
    j = n - 1;
    for (i = 1; i <= n; i++) {
      if (S[i - 1] == '0') {
        ll p = (pr[i - 1][1] % Y + sf[j][1] % Y) % Y;
        p = (p % Y + po2[n - i][1] % Y) % Y;
        cout << z[p] + 1 << "\\n";
      } else {
        ll p = (pr[i - 1][0] % X + sf[j][0] % X) % X;

        cout << z[p] + 1 << "\\n";
      }
      j--;
    }
  }
}