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

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

int f(int x) { return x % __builtin_popcount(x); }

int dp[200009];

ll modpow(ll n, ll k, ll m) {
  ll res = 1;
  while (k) {
    if (k & 1)
      res = res * n % m;
    n = n * n % m;
    k >>= 1;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  FOR(i, 1, 200009) dp[i] = dp[f(i)] + 1;

  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = count(ALL(s), '1');

  int a = 0, b = 0;
  rep(i, n) if (s[i] == '1') {
    a = (a + modpow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
    if (cnt > 1)
      b = (b + modpow(2, n - 1 - i, cnt - 1)) % (cnt - 1);
  }

  rep(i, n) {
    if (s[i] == '0') {
      cout << dp[(a + modpow(2, n - 1 - i, cnt + 1)) % (cnt + 1)] + 1 << endl;
    } else {
      if (cnt <= 1) {
        cout << 0 << endl;
      } else {
        cout << dp[(b + cnt - 1 - modpow(2, n - 1 - i, cnt - 1)) % (cnt - 1)] +
                    1
             << endl;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

int f(int x) { return x % __builtin_popcount(x); }

int dp[200009];

ll modpow(int n, int k, int m) {
  ll res = 1;
  while (k) {
    if (k & 1)
      res = res * n % m;
    n = n * n % m;
    k >>= 1;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  FOR(i, 1, 200009) dp[i] = dp[f(i)] + 1;

  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = count(ALL(s), '1');

  int a = 0, b = 0;
  rep(i, n) if (s[i] == '1') {
    a = (a + modpow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
    if (cnt > 1)
      b = (b + modpow(2, n - 1 - i, cnt - 1)) % (cnt - 1);
  }

  rep(i, n) {
    if (s[i] == '0') {
      cout << dp[(a + modpow(2, n - 1 - i, cnt + 1)) % (cnt + 1)] + 1 << endl;
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
      } else {
        cout << dp[(b + cnt - 1 - modpow(2, n - 1 - i, cnt - 1)) % (cnt - 1)] +
                    1
             << endl;
      }
    }
  }

  return 0;
}