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

#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize ("-O3")
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long double PI = 3.14159265358979323846; /* pi */
vector<int> ans(5e5, -1);

int findans(int n) {
  if (ans[n] != -1)
    return ans[n];
  int bc = __builtin_popcount(n);
  ans[n] = 1 + ans[n % bc];
  return ans[n];
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bc = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      bc++;
  }
  int mod1 = bc + 1;
  int mod2 = bc - 1;

  vector<int> valmod1(n + 1), valmod2(n + 1);
  int v1 = 1;
  int v2 = 1;
  for (int i = 0; i < n; i++) {
    valmod1[i] = v1 % mod1;
    if (mod2 != 0)
      valmod2[i] = v2 % mod2;
    v1 = (v1 << 1) % mod1;
    if (mod2 != 0)
      v2 = (v2 << 1) % mod2;
  }
  ans[0] = 0;
  for (int i = 0; i < 5e5; i++) {
    if (ans[i] == -1)
      findans(i);
  }
  int current_ans1 = 0;
  int current_ans2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[n - 1 - i] == '1') {
      current_ans1 = (current_ans1 + valmod1[i]) % mod1;
      if (mod2 != 0)
        current_ans2 = (current_ans2 + valmod2[i]) % mod2;
    }
  }
  for (int i = 0; i < n; i++) {
    int p = n - 1 - i;
    if (s[i] == '1') {
      // use mod2
      int fval = 0;
      if (mod2 != 0) {
        fval = (current_ans2 - valmod2[p]) % mod2;
      } else {
        cout << "0\\n";
        continue;
      }
      while (fval < 0)
        fval += mod2;
      cout << 1 + ans[fval] << "\\n";
    } else {
      // use mod1
      int fval = (current_ans1 + valmod1[p]) % mod1;
      while (fval < 0)
        fval += mod1;
      cout << 1 + ans[fval] << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize ("-O3")
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long double PI = 3.14159265358979323846; /* pi */
vector<int> ans(5e5, -1);

int findans(int n) {
  if (ans[n] != -1)
    return ans[n];
  int bc = __builtin_popcount(n);
  ans[n] = 1 + ans[n % bc];
  return ans[n];
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bc = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      bc++;
  }
  int mod1 = bc + 1;
  int mod2 = bc - 1;

  vector<int> valmod1(n + 1), valmod2(n + 1);
  int v1 = 1;
  int v2 = 1;
  for (int i = 0; i < n; i++) {
    valmod1[i] = v1 % mod1;
    valmod2[i] = v2 % mod2;
    v1 = (v1 << 1) % mod1;
    v2 = (v2 << 1) % mod2;
  }
  ans[0] = 0;
  for (int i = 0; i < 5e5; i++) {
    if (ans[i] == -1)
      findans(i);
  }
  int current_ans1 = 0;
  int current_ans2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[n - 1 - i] == '1') {
      current_ans1 = (current_ans1 + valmod1[i]) % mod1;
      current_ans2 = (current_ans2 + valmod2[i]) % mod2;
    }
  }
  for (int i = 0; i < n; i++) {
    int p = n - 1 - i;
    if (s[i] == '1') {
      // use mod2
      int fval = (current_ans2 - valmod2[p]) % mod2;
      while (fval < 0)
        fval += mod2;
      cout << 1 + ans[fval] << "\\n";
    } else {
      // use mod1
      int fval = (current_ans1 + valmod1[p]) % mod1;
      while (fval < 0)
        fval += mod1;
      cout << 1 + ans[fval] << "\\n";
    }
  }
  return 0;
}