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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int N;
  cin >> N;
  string s;
  cin >> s;

  int one = count(s.begin(), s.end(), '1');
  vector<int> modP(N), modM(N);
  int powP = 1, powM = 1;
  int modPP = 0, modMM = 0;
  for (int i = N - 1; i >= 0; --i) {
    modP[i] = powP;
    modM[i] = powM;
    if (s[i] == '1') {
      (modPP += modP[i]) %= (one + 1);
      if (one > 1)
        (modMM += modM[i]) %= (one - 1);
    }
    (powP *= 2) %= (one + 1);
    if (one > 1)
      (powM *= 2) %= (one - 1);
  }

  for (int i = 0; i < N; ++i) {
    if (s[i] == '0') {
      int mod = one + 1;
      int res = 1;
      int cur = (modPP + modP[i] + mod) % mod;
      while (cur > 0) {
        cur %= __builtin_popcount(cur);
        ++res;
      }
      cout << res << '\\n';
    } else {
      if (one == 1) {
        cout << 0 << '\\n';
        continue;
      }
      int mod = one - 1;
      int res = 1;
      int cur = (modMM - modM[i] + mod) % mod;
      while (cur > 0) {
        cur %= __builtin_popcount(cur);
        ++res;
      }
      cout << res << '\\n';
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int N;
  cin >> N;
  string s;
  cin >> s;

  int one = count(s.begin(), s.end(), '1');
  vector<int> modP(N), modM(N);
  int powP = 1, powM = 1;
  int modPP = 0, modMM = 0;
  for (int i = N - 1; i >= 0; --i) {
    modP[i] = powP;
    modM[i] = powM;
    if (s[i] == '1') {
      (modPP += modP[i]) %= (one + 1);
      (modMM += modM[i]) %= (one - 1);
    }
    (powP *= 2) %= (one + 1);
    (powM *= 2) %= (one - 1);
  }

  for (int i = 0; i < N; ++i) {
    if (s[i] == '0') {
      int mod = one + 1;
      int res = 1;
      int cur = (modPP + modP[i] + mod) % mod;
      while (cur > 0) {
        cur %= __builtin_popcount(cur);
        ++res;
      }
      cout << res << '\\n';
    } else {
      int mod = one - 1;
      int res = 1;
      int cur = (modMM - modM[i] + mod) % mod;
      while (cur > 0) {
        cur %= __builtin_popcount(cur);
        ++res;
      }
      cout << res << '\\n';
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}