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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}
const int N = 2e5 + 1;
int f[N], pow2[3][N], res[3];
int cntBit(int n) {
  if (n == 0)
    return 0;
  return 1 + cntBit(n - (n & -n));
}
int main() {
  run();
  for (int i = 1; i < N; i++)
    f[i] = 1 + f[i % cntBit(i)];
  int n;
  string s;
  cin >> n >> s;
  int MOD = 0;
  for (int i = 0; i < n; i++)
    MOD += (s[i] == '1');
  for (int j = 0; j < 3; j++) {
    int mod = MOD + j - 1;
    if (mod <= 0)
      continue;
    pow2[j][0] = 1;
    for (int i = 1; i < n; i++)
      pow2[j][i] = (pow2[j][i - 1] << 1) % mod;
    for (int i = 0; i < n; i++)
      res[j] = (res[j] + (s[i] == '1') * pow2[j][n - 1 - i]) % mod;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      int mod = MOD - 1;
      if (mod == 0)
        cout << 0 << endl;
      else {
        int cur = (((res[0] - pow2[0][n - 1 - i]) % mod) + mod) % mod;
        cout << 1 + f[cur] << endl;
      }
    } else {
      int mod = MOD + 1;
      int cur = (res[2] + pow2[2][n - 1 - i]) % mod;
      cout << 1 + f[cur] << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}
const int N = 2e5 + 1;
int f[N], pow2[3][N], res[3];
int cntBit(int n) {
  if (n == 0)
    return 0;
  return 1 + cntBit(n - (n & -n));
}
int main() {
  run();
  for (int i = 1; i < N; i++)
    f[i] = 1 + f[i % cntBit(i)];
  int n;
  string s;
  cin >> n >> s;
  int MOD = 0;
  for (int i = 0; i < n; i++)
    MOD += (s[i] == '1');
  for (int j = 0; j < 3; j++) {
    int mod = MOD + j - 1;
    if (mod <= 0)
      continue;
    pow2[j][0] = 1;
    for (int i = 1; i < n; i++)
      pow2[j][i] = (pow2[j][i - 1] << 1) % mod;
    for (int i = 0; i < n; i++)
      res[j] = (res[j] + (s[i] == '1') * pow2[j][n - 1 - i]) % mod;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      int mod = MOD - 1;
      int cur = (((res[0] - pow2[0][n - 1 - i]) % mod) + mod) % mod;
      cout << 1 + f[cur] << endl;
    } else {
      int mod = MOD + 1;
      int cur = (res[2] + pow2[2][n - 1 - i]) % mod;
      cout << 1 + f[cur] << endl;
    }
  }
}