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

#include <bits/stdc++.h>

using namespace std;

const int N = 9e5 + 2;

int mod;

int n;
char x[N];

// 0 (mod is number of ones - 1)
// 1 (mod is number of ones + 1)
int prf[N][2];
int suf[N][2];

int dp[N];

int f(int r) {
  if (r == 0)
    return 0;
  return 1 + f(r % __builtin_popcount(r));
}

int fastP(int b, int e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * 1ll * fastP(b * 1ll * b % mod, e >> 1) % mod;
  return fastP(b * 1ll * b % mod, e >> 1) % mod;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    mod += x[i] - '0';
  }
  if (mod - 1 > 0) {
    long long res = 0;
    mod--;
    for (int i = 1; i <= n; i++) {
      prf[i][0] = prf[i - 1][0] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
      prf[i][0] %= mod;
    }
    for (int i = n; i >= 1; i--) {
      suf[i][0] = suf[i + 1][0] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
      suf[i][0] %= mod;
    }
    mod++;
  }
  mod++;
  for (int i = 1; i <= n; i++) {
    prf[i][1] = prf[i - 1][1] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
    prf[i][1] %= mod;
  }
  for (int i = n; i >= 1; i--) {
    suf[i][1] = suf[i + 1][1] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
    suf[i][1] %= mod;
  }
  mod--;

  for (int i = 1; i <= n; i++) {
    if (x[i] == '0') {
      mod++;
      long long res = fastP(2, n - i);
      res += suf[i + 1][1] + prf[i - 1][1];
      cout << f(res % mod) + 1 << "\\n";
      mod--;
    } else {
      mod--;
      long long res = (suf[i + 1][0] + prf[i - 1][0]);
      if (mod <= 0) {
        cout << 0 << "\\n";
      } else {
        cout << f(res % mod) + 1 << "\\n";
      }
      mod++;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 9e5 + 2;

int mod;

int n;
char x[N];

// 0 (mod is number of ones - 1)
// 1 (mod is number of ones + 1)
int prf[N][2];
int suf[N][2];

int dp[N];

int f(int r) {
  if (r == 0)
    return 0;
  return 1 + f(r % __builtin_popcount(r));
}

int fastP(int b, int e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * 1ll * fastP(b * 1ll * b % mod, e >> 1) % mod;
  return fastP(b * 1ll * b % mod, e >> 1) % mod;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    mod += x[i] - '0';
  }
  if (mod) {
    long long res = 0;
    mod--;
    for (int i = 1; i <= n; i++) {
      prf[i][0] = prf[i - 1][0] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
    }
    for (int i = n; i >= 1; i--) {
      suf[i][0] = suf[i + 1][0] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
    }
    mod += 2;
    for (int i = 1; i <= n; i++) {
      prf[i][1] = prf[i - 1][1] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
    }
    for (int i = n; i >= 1; i--) {
      suf[i][1] = suf[i + 1][1] + ((x[i] == '1') ? (fastP(2, n - i)) : 0);
    }
    mod--;
  }
  for (int i = 1; i <= n; i++) {
    if (x[i] == '0') {
      mod++;
      long long res = fastP(2, n - i);
      res += suf[i + 1][1] + prf[i - 1][1];
      cout << f(res % mod) + 1 << "\\n";
      mod--;
    } else {
      mod--;
      long long res = (suf[i + 1][0] + prf[i - 1][0]);
      if (mod <= 0) {
        cout << 0 << "\\n";
      } else {
        cout << f(res % mod) + 1 << "\\n";
      }
      mod++;
    }
  }
}