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

#include <bits/stdc++.h>
#include <random>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using Pt = pair<int, int>;

namespace {
int fn(int N, int ct, vi &C) {
  if (N == 0) {
    return ct;
  }
  if (C[N] > 0)
    return ct + C[N];
  ++ct;
  int M = N;
  int c = 0;
  while (M > 0) {
    c += (M & 1);
    M >>= 1;
  }
  int d = N % c;
  return fn(d, ct, C);
}
} // namespace
int main() {
  int N;
  cin >> N;
  vi C(N + 2);
  for (int i = 0; i <= N + 1; ++i) {
    int c = fn(i, 0, C);
    C[i] = c;
  }

  string X;
  cin >> X;
  vi XX(N);
  for (int i = 0; i < N; ++i)
    XX[i] = X[i] == '1' ? 1 : 0;

  int M = 0;
  for (int i = 0; i < N; ++i) {
    if (XX[i] == 1)
      ++M;
  }
  vi ans(N);

  if (M <= 1) {
    int r2 = 0;
    for (int i = 0; i < N; ++i) {
      r2 *= 2;
      r2 += XX[i];
      r2 %= (M + 1);
    }

    int rr2 = 1;
    for (int i = N - 1; i > -1; --i) {
      if (XX[i] == 1) {
        ans[i] = 0;
      } else {
        int q = r2 + rr2;
        q %= M + 1;
        while (q < 0)
          q += XX[i] == 1 ? M - 1 : M + 1;
        q %= XX[i] == 1 ? M - 1 : M + 1;

        ans[i] = C[q] + 1;
      }

      rr2 *= 2;
      rr2 %= M + 1;
    }
    for (int i = 0; i < N; ++i)
      cout << ans[i] << endl;
    return 0;
  }

  int r1 = 0;
  int r2 = 0;
  for (int i = 0; i < N; ++i) {
    r1 *= 2;
    r1 += XX[i];
    r1 %= (M - 1);

    r2 *= 2;
    r2 += XX[i];
    r2 %= (M + 1);
  }

  int rr1 = 1;
  int rr2 = 1;
  for (int i = N - 1; i > -1; --i) {
    int q = XX[i] == 1 ? r1 - rr1 : r2 + rr2;
    q %= XX[i] == 1 ? M - 1 : M + 1;
    while (q < 0)
      q += XX[i] == 1 ? M - 1 : M + 1;
    q %= XX[i] == 1 ? M - 1 : M + 1;

    ans[i] = C[q] + 1;

    rr1 *= 2;
    rr1 %= M - 1;
    rr2 *= 2;
    rr2 %= M + 1;
  }
  for (int i = 0; i < N; ++i)
    cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <random>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using Pt = pair<int, int>;

namespace {
int fn(int N, int ct, vi &C) {
  if (N == 0) {
    return ct;
  }
  if (C[N] > 0)
    return ct + C[N];
  ++ct;
  int M = N;
  int c = 0;
  while (M > 0) {
    c += (M & 1);
    M >>= 1;
  }
  int d = N % c;
  return fn(d, ct, C);
}
} // namespace
int main() {
  int N;
  cin >> N;
  vi C(N + 2);
  for (int i = 0; i <= N + 1; ++i) {
    int c = fn(i, 0, C);
    C[i] = c;
  }

  string X;
  cin >> X;
  vi XX(N);
  for (int i = 0; i < N; ++i)
    XX[i] = X[i] == '1' ? 1 : 0;

  int M = 0;
  for (int i = 0; i < N; ++i) {
    if (XX[i] == 1)
      ++M;
  }

  int r1 = 0;
  int r2 = 0;
  for (int i = 0; i < N; ++i) {
    r1 *= 2;
    r1 += XX[i];
    r1 %= (M - 1);

    r2 *= 2;
    r2 += XX[i];
    r2 %= (M + 1);
  }

  vi ans(N);
  int rr1 = 1;
  int rr2 = 1;
  for (int i = N - 1; i > -1; --i) {
    int q = XX[i] == 1 ? r1 - rr1 : r2 + rr2;
    q %= XX[i] == 1 ? M - 1 : M + 1;
    while (q < 0)
      q += XX[i] == 1 ? M - 1 : M + 1;
    q %= XX[i] == 1 ? M - 1 : M + 1;

    if (q <= N)
      ans[i] = C[q] + 1;

    rr1 *= 2;
    rr1 %= M - 1;
    rr2 *= 2;
    rr2 %= M + 1;
  }
  for (int i = 0; i < N; ++i)
    cout << ans[i] << endl;
  return 0;
}