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

#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T div_floor(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
  int n;
  string s;
  cin >> n >> s;

  int dp[MAX];
  dp[0] = 0;
  For(i, 1, MAX) dp[i] = 1 + dp[i % __builtin_popcount(i)];

  int p = count_if(s.begin(), s.end(), [](char c) { return c == '1'; }) + 1;
  int q = p - 2;
  int pow2_p[n], pow2_q[n], x = 0, y = 0;
  pow2_p[0] = pow2_q[0] = 1;
  For(i, 1, n) {
    pow2_p[i] = pow2_p[i - 1] * 2 % p;
    if (q > 0)
      pow2_q[i] = pow2_q[i - 1] * 2 % q;
  }
  rep(i, n) {
    x = (x * 2 + (s[i] - '0')) % p;
    if (q > 0)
      y = (y * 2 + (s[i] - '0')) % q;
  }

  rep(i, n) {
    if (s[i] == '0') {
      int tmp = (x + pow2_p[n - 1 - i]) % p;
      printf("%d\\n", 1 + dp[tmp]);
    } else {
      if (q == 0)
        printf("%d\\n", 0);
      else {
        int tmp = (y + q - pow2_q[n - 1 - i]) % q;
        printf("%d\\n", 1 + dp[tmp]);
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T div_floor(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
  int n;
  string s;
  cin >> n >> s;

  int dp[MAX];
  dp[0] = 0;
  For(i, 1, MAX) dp[i] = 1 + dp[i % __builtin_popcount(i)];

  int p = count_if(s.begin(), s.end(), [](char c) { return c == '1'; }) + 1;
  int q = p - 2;
  int pow2_p[n], pow2_q[n], x = 0, y = 0;
  pow2_p[0] = pow2_q[0] = 1;
  For(i, 1, n) {
    pow2_p[i] = pow2_p[i - 1] * 2 % p;
    if (q > 0)
      pow2_q[i] = pow2_q[i - 1] * 2 % q;
  }
  rep(i, n) {
    x = (x * 2 + (s[i] - '0')) % p;
    if (q > 0)
      y = (y * 2 + (s[i] - '0')) % q;
  }

  rep(i, n) {
    if (s[i] == '0') {
      int tmp = (x + pow2_p[n - 1 - i]) % p;
      printf("%d\\n", 1 + dp[tmp]);
    } else {
      int tmp = (y + q - pow2_q[n - 1 - i]) % q;
      printf("%d\\n", 1 + dp[tmp]);
    }
  }
}