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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
int dp[200010];

int dfs(int n) {
  if (dp[n] >= 0)
    return dp[n];
  if (n == 0)
    return 0;
  int popcnt = __builtin_popcount(n);
  if (n % popcnt == 0) {
    dp[n] = 1;
    return 1;
  }
  dp[n] = dfs(n % popcnt) + 1;
  return dp[n];
}

long long modpow(long long N, long long K, long long mod = MOD) {
  long long ret = 1;
  while (K > 0) {
    if (K & 1) {
      ret *= N;
      ret %= mod;
    }
    K >>= 1;
    N *= N;
    N %= mod;
  }
  return ret;
}
int main() {
  int N;
  cin >> N;
  rep(i, N + 1) { dp[i] = -1; }
  string X;
  cin >> X;
  int cnt = 0;
  rep(i, N) {
    if (X[i] == '1')
      ++cnt;
  }
  if (cnt == 0) {
    rep(i, N) cout << 1 << '\\n';
    return 0;
  }

  vector<ll> modul1(N, 0);
  vector<ll> modul2(N, 0);
  rep(i, N) {
    if (cnt > 1)
      modul1[i] = modpow(2, i, cnt - 1);
    modul2[i] = modpow(2, i, cnt + 1);
  }
  int ret1 = 0, ret2 = 0;
  rep(i, N) {
    if (X[N - i - 1] == '1') {
      if (cnt > 1) {
        ret1 += modul1[i];
        ret1 %= (cnt - 1);
      }
      ret2 += modul2[i];
      ret2 %= (cnt + 1);
    }
  }

  vector<ll> ans(N, 0);
  rep(i, N) {
    if (X[i] == '1') {
      if (cnt == 1)
        continue;
      ll ret = ret1;
      ret -= modul1[N - i - 1];
      ret += cnt - 1;
      ret %= (cnt - 1);
      ans[i] = dfs(ret) + 1;
    } else {
      ll ret = ret2;
      ret += modul2[N - i - 1];
      ret %= (cnt + 1);
      ans[i] = dfs(ret) + 1;
    }
  }
  rep(i, N) cout << ans[i] << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr ll HINF = 1000000000000000;
constexpr int MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

int dp[200010];

int dfs(int n) {
  if (dp[n] >= 0)
    return dp[n];
  if (n == 0) {
    dp[n] = 0;
    return 0;
  }
  int x = __builtin_popcount(n);
  // printf("x: %d n: %d\\n",x,n);
  if (x == 0) {
    dp[n] = 0;
    return 0;
  }
  if (n % x == 0) {
    dp[n] = 1;
    return 1;
  }
  dp[n] = dfs(n % x) + 1;
  return dp[n];
}

int modpow(int N, int K, int mod) {
  int ret = 1;
  while (K > 0) {
    if (K & 1) {
      ret *= N;
      ret %= mod;
    }
    K >>= 1;
    N *= N;
    N %= mod;
  }
  return ret;
}
int main() {
  int N;
  cin >> N;
  rep(i, N + 1) dp[i] = -1;
  string X;
  cin >> X;
  vector<int> ans(N, 0);
  int cnt = 0;
  rep(i, N) {
    if (X[i] == '1')
      ++cnt;
  }
  if (cnt == 0) {
    rep(i, N) cout << 1 << '\\n';
    return 0;
  }
  // cout << cnt << '\\n';

  vector<int> powmod1(N, 0);
  vector<int> powmod2(N, 0);
  vector<int> powmod3(N, 0);
  rep(i, N) {
    powmod1[i] = modpow(2, i, cnt);
    if (cnt > 1) {
      powmod2[i] = modpow(2, i, cnt - 1);
    }
    powmod3[i] = modpow(2, i, cnt + 1);
  }
  int mod1 = 0, mod2 = 0, mod3 = 0;
  rep(i, N) {
    if (X[N - i - 1] == '1') {
      mod1 += powmod1[i];
      mod1 %= cnt;
      if (cnt > 1) {
        mod2 += powmod2[i];
        mod2 %= (cnt - 1);
      }
      mod3 += powmod3[i];
      mod3 %= (cnt + 1);
    }
  }
  // rep(i,N) printf("modpow : %d\\n",powmod2[i]);
  // cout << mod1 << ' ' << mod2 << ' ' << mod3 << '\\n';
  rep(i, N) {
    if (X[i] == '1') {
      if (cnt == 1) {
        ans[i] = 0;
        continue;
      }
      if (cnt > 1) {
        int now = mod2;
        now -= powmod2[N - i - 1];
        now += cnt - 1;
        now %= (cnt - 1);
        // printf("now: %d\\n",now);
        ans[i] = dfs(now) + 1;
      }
    } else {
      int now = mod3;
      now += powmod3[N - i - 1];
      now %= (cnt + 1);
      // printf("now: %d\\n",now);
      ans[i] = dfs(now) + 1;
    }
  }
  rep(i, N) cout << ans[i] << '\\n';
  // rep(i,N) cout << dp[i] << '\\n';
  return 0;
}