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

#include <bits/stdc++.h>

using namespace std;

using usize = ::std::size_t;
// using u64 = ::std::uint_least64_t;
typedef long long u64;
// static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;

u64 N;

u64 modPow(u64 n, u64 k, u64 mod) {
  if (k == 0)
    return 1LL % mod;
  u64 r = modPow(n, k >> 1, mod);
  r = r * r % mod;
  if (k & 1)
    r = r * n % mod;
  return r;
}

int main(int argc, char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  // vector<u64> dp(200001,0);
  // dp[0] = 0;
  // for (int n = 1; n <= 200000; n++) {
  //     u64 bits = __builtin_popcount(n);
  //     dp[n] = dp[n % bits] + 1;
  // }

  string X;
  cin >> X;

  u64 cnt = 0, mod1 = 0, mod2 = 0;
  for (int i = 0; i < N; i++) {
    cnt += X[i] - '0';
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      mod2 += modPow(2, (N - i - 1), cnt + 1);
      mod2 %= cnt + 1;
      if (cnt == 1) {
        continue;
      }
      mod1 += modPow(2, (N - i - 1), cnt - 1);
      mod1 %= cnt - 1;
    }
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      u64 modi = mod1 - modPow(2, (N - i - 1), cnt - 1);
      modi %= cnt - 1;
      if (modi < 0) {
        modi += cnt - 1;
      }
      u64 times = 1;
      while (modi > 0) {
        modi %= __builtin_popcount(modi);
        times++;
      }
      cout << times << endl;
    } else {
      u64 modi = mod2 + modPow(2, (N - i - 1), cnt + 1);
      modi %= cnt + 1;
      u64 times = 1;
      while (modi > 0) {
        modi %= __builtin_popcount(modi);
        times++;
      }
      cout << times << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using usize = ::std::size_t;
// using u64 = ::std::uint_least64_t;
typedef long long u64;
// static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;

u64 N;

u64 modPow(u64 n, u64 k, u64 mod) {
  if (k == 0)
    return 1LL % mod;
  u64 r = modPow(n, k >> 1, mod);
  r = r * r % mod;
  if (k & 1)
    r = r * n % mod;
  return r;
}

int main(int argc, char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  // vector<u64> dp(200001,0);
  // dp[0] = 0;
  // for (int n = 1; n <= 200000; n++) {
  //     u64 bits = __builtin_popcount(n);
  //     dp[n] = dp[n % bits] + 1;
  // }

  string X;
  cin >> X;

  u64 cnt = 0, mod1 = 0, mod2 = 0;
  for (int i = 0; i < N; i++) {
    cnt += X[i] - '0';
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      mod1 += modPow(2, (N - i - 1), cnt - 1);
      mod2 += modPow(2, (N - i - 1), cnt + 1);
      mod1 %= cnt - 1;
      mod2 %= cnt + 1;
    }
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      u64 modi = mod1 - modPow(2, (N - i - 1), cnt - 1);
      modi %= cnt - 1;
      if (modi < 0) {
        modi += cnt - 1;
      }
      u64 times = 1;
      while (modi > 0) {
        modi %= __builtin_popcount(modi);
        times++;
      }
      cout << times << endl;
    } else {
      u64 modi = mod2 + modPow(2, (N - i - 1), cnt + 1);
      modi %= cnt + 1;
      u64 times = 1;
      while (modi > 0) {
        modi %= __builtin_popcount(modi);
        times++;
      }
      cout << times << endl;
    }
  }

  return 0;
}