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

#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#define input                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#else
#define debug(...) 4
#define input 4
#endif

using ll = long long;
constexpr int N = 2e5 + 5;
constexpr int mod = 1e9 + 7;
int ans[N], modulo1[N], modulo2[N];
int main() {
  input;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= 200000; i++) {
    int ii = i, cnt = 0;
    while (ii) {
      ii = ii % __builtin_popcount(ii);
      cnt++;
    }
    int p = __builtin_popcount(i);
    ans[i] = cnt;
  }
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int pop = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      pop++;
  }
  int pop1 = pop + 1;
  int pop2 = pop - 1;
  modulo1[0] = 1 % pop1;
  if (pop2)
    modulo2[0] = 1 % pop2;
  for (int i = 1; i < n; i++) {
    modulo1[i] = (modulo1[i - 1] * (2 % pop1)) % pop1;
    if (pop2)
      modulo2[i] = (modulo2[i - 1] * (2 % pop2)) % pop2;
  }
  int total_mod1 = 0, total_mod2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      total_mod1 += modulo1[i];
      total_mod2 += modulo2[i];
      total_mod1 %= pop1;
      if (pop2)
        total_mod2 %= pop2;
    }
  }
  vector<int> v;
  for (int i = 0; i < n; i++) {

    if (s[i] == '1') {
      int x = total_mod2;
      x -= modulo2[i];
      x += pop2;
      if (pop2)
        x %= pop2;
      if (pop2)
        v.push_back(1 + ans[x]);
      else
        v.push_back(0);
    } else {
      int x = total_mod1;
      x += modulo1[i];
      x %= pop1;
      v.push_back(1 + ans[x]);
    }
  }
  reverse(v.begin(), v.end());
  for (int x : v)
    cout << x << "\\n";
}
// asdasdlkjl

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#define input                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#else
#define debug(...) 4
#define input 4
#endif

using ll = long long;
constexpr int N = 2e5 + 5;
constexpr int mod = 1e9 + 7;
int ans[N], modulo1[N], modulo2[N];
int main() {
  input;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= 200000; i++) {
    int ii = i, cnt = 0;
    while (ii) {
      ii = ii % __builtin_popcount(ii);
      cnt++;
    }
    int p = __builtin_popcount(i);
    ans[i] = cnt;
  }
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int pop = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      pop++;
  }
  int pop1 = pop + 1;
  int pop2 = pop - 1;
  modulo1[0] = 1 % pop1;
  modulo2[0] = 1 % pop2;

  for (int i = 1; i < n; i++) {
    modulo1[i] = (modulo1[i - 1] * (2 % pop1)) % pop1;
    modulo2[i] = (modulo2[i - 1] * (2 % pop2)) % pop2;
  }
  int total_mod1 = 0, total_mod2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      total_mod1 += modulo1[i];
      total_mod2 += modulo2[i];
      total_mod1 %= pop1;
      total_mod2 %= pop2;
    }
  }
  vector<int> v;
  for (int i = 0; i < n; i++) {

    if (s[i] == '1') {
      int x = total_mod2;
      x -= modulo2[i];
      x += pop2;
      x %= pop2;
      v.push_back(1 + ans[x]);
    } else {
      int x = total_mod1;
      x += modulo1[i];
      x %= pop1;
      v.push_back(1 + ans[x]);
    }
  }
  reverse(v.begin(), v.end());
  for (int x : v)
    cout << x << "\\n";
}
// asdasdlkjl