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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define inf 2e9
#define linf 9000000000000000000ll
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int n;
vector<bool> bit;
ll m_m, m_p;
ll mod_power(ll x, ll n, ll M) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    return mod_power((x * x) % M, n / 2, M);
  } else {
    return (mod_power((x * x) % M, n / 2, M) * x) % M;
  }
}

int f(ll x, int cnt = 0) {
  if (x == 0)
    return cnt;
  return f(x % __builtin_popcount(x), cnt + 1);
}

int main() {
  cin >> n;
  bit.resize(n);
  int cnt = 0;
  rep(i, n) {
    char c;
    cin >> c;
    if (c == '1')
      bit[i] = true, cnt++;
  }

  rrep(i, n) {
    if (bit[i]) {
      if (cnt - 1 != 0)
        m_m = (m_m + mod_power(2, n - i - 1, cnt - 1)) % (cnt - 1);
      m_p = (m_p + mod_power(2, n - i - 1, cnt + 1)) % (cnt + 1);
    }
  }
  rep(i, n) {
    if (bit[i]) {
      if (cnt - 1 == 0) {
        printf("%d\\n", 0);
        continue;
      }
      ll x = (m_m - mod_power(2, n - i - 1, cnt - 1) + (cnt - 1)) % (cnt - 1);
      int fx = f(x) + 1;
      printf("%d\\n", fx);
    } else {
      ll x = (mod_power(2, n - i - 1, cnt + 1) + m_p) % (cnt + 1);
      int fx = f(x) + 1;
      printf("%d\\n", fx);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define inf 2e9
#define linf 9000000000000000000ll
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int n;
vector<bool> bit;
ll m_m, m_p;
ll mod_power(ll x, ll n, ll M) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    return mod_power((x * x) % M, n / 2, M);
  } else {
    return (mod_power((x * x) % M, n / 2, M) * x) % M;
  }
}

int f(ll x, int cnt = 0) {
  if (x == 0)
    return cnt;
  return f(x % __builtin_popcount(x), cnt + 1);
}

int main() {
  cin >> n;
  bit.resize(n);
  int cnt = 0;
  rep(i, n) {
    char c;
    cin >> c;
    if (c == '1')
      bit[i] = true, cnt++;
  }

  rrep(i, n) {
    if (bit[i]) {
      m_m = (m_m + mod_power(2, n - i - 1, cnt - 1)) % (cnt - 1);
      m_p = (m_p + mod_power(2, n - i - 1, cnt + 1)) % (cnt + 1);
    }
  }
  rep(i, n) {
    if (bit[i]) {
      ll x = (m_m - mod_power(2, n - i - 1, cnt - 1) + (cnt - 1)) % (cnt - 1);
      int fx = f(x) + 1;
      printf("%d\\n", fx);
    } else {
      ll x = (mod_power(2, n - i - 1, cnt + 1) + m_p) % (cnt + 1);
      int fx = f(x) + 1;
      printf("%d\\n", fx);
    }
  }
  return 0;
}