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

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
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 (b < a) {
    a = b;
    return true;
  }
  return false;
}

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 2223450
#else
#define SIZE 2223450
#endif

int N, Q, K;
string X;
// -1したとき+1したとき,2^jを割った→あまり
int DP[2][SIZE];

int calc(int n) {
  bitset<30> b(n);
  int ret = 0;
  while (n) {
    n %= b.count();
    b = bitset<30>(n);
    ret++;
  }
  return ret;
}
int M[2];
int v[2] = {0, 0};

void solve() {
  cin >> N >> X;
  int cnt = 0;
  REP(i, N) if (X[i] == '1') cnt++;
  REP(i, 2) {
    int m = (i == 0) ? cnt - 1 : cnt + 1;
    M[i] = m;
    if (m <= 0)
      continue;
    DP[i][0] = 1 % m;
    REP(j, N) { (DP[i][j + 1] = DP[i][j] * 2) %= m; }
  }
  reverse(ALL(X));
  REP(k, N) {
    if (X[k] == '1') {
      REP(i, 2) v[i] += DP[i][k];
    }
  }
  RREP(k, N) {
    if (X[k] == '1') {
      int m = M[0];
      if (m <= 0) {
        out("0\\n");
      } else {
        int val = (v[0] - DP[0][k] + m) % m;
        out("%lld\\n", calc(val) + 1);
      }
    } else {
      int m = M[1];
      int val = (v[1] + DP[1][k]) % m;
      out("%lld\\n", calc(val) + 1);
    }
  }
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
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 (b < a) {
    a = b;
    return true;
  }
  return false;
}

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 2223450
#else
#define SIZE 2223450
#endif

int N, Q, K;
string X;
// -1したとき+1したとき,2^jを割った→あまり
int DP[2][SIZE];

int calc(int n) {
  bitset<30> b(n);
  int ret = 0;
  while (n) {
    n %= b.count();
    b = bitset<30>(n);
    ret++;
  }
  return ret;
}

void solve() {
  cin >> N >> X;
  int cnt = 0;
  REP(i, N) if (X[i] == '1') cnt++;
  int M[2];
  REP(i, 2) {
    int m = (i == 0) ? cnt - 1 : cnt + 1;
    M[i] = m;
    if (m <= 0)
      continue;
    DP[i][0] = 1 % m;
    REP(j, N) { (DP[i][j + 1] = DP[i][j] * 2) %= m; }
  }
  reverse(ALL(X));
  int v[2] = {0, 0};
  REP(k, N) {
    if (X[k] == '1') {
      REP(i, 2) v[i] += DP[i][k];
    }
  }
  RREP(k, N) {
    if (X[k] == '1') {
      int m = M[0];
      int val = (v[0] - DP[0][k] + m) % m;
      out("%lld\\n", calc(val) + 1);
    } else {
      int m = M[1];
      int val = (v[1] + DP[1][k]) % m;
      out("%lld\\n", calc(val) + 1);
    }
  }
}