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

#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template <class F> auto recursive(F f) {
  return [f](auto... a) { return f(f, a...); };
}
template <class T>
using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using vector2d = vector<vector<T>>;
struct fixprec {
  int p;
  fixprec(int p) : p(p) {}
};
ostream &operator<<(ostream &o, fixprec f) {
  return o << fixed << setprecision(f.p);
}

/*!* [iterutil] *!*/
/*+* イテレータの操作 *+*/
template <class I, class V = typename I::value_type>
V sum_up(const I &l, const I &r) {
  V v;
  for (I i = l; i != r; i++)
    v = i == l ? *i : v + *i;
  return v;
}
template <class I, class T = iterator_traits<I>> I operator+(I it, int n) {
  for (int i = 0; i < n; i++)
    it++;
  return it;
}
template <class I, class T = iterator_traits<I>> I operator-(I it, int n) {
  for (int i = 0; i < n; i++)
    it--;
  return it;
}

/*!* [bitutil] *!*/
/*+* ビット操作関連 *+*/
int PopCount(i64 x) {
  x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
  x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
  x = (x & 0x0f0f0f0f0f0f0f0f) + (x >> 4 & 0x0f0f0f0f0f0f0f0f);
  x = (x & 0x00ff00ff00ff00ff) + (x >> 8 & 0x00ff00ff00ff00ff);
  x = (x & 0x0000ffff0000ffff) + (x >> 16 & 0x0000ffff0000ffff);
  x = (x & 0x00000000ffffffff) + (x >> 32 & 0x00000000ffffffff);
  return int(x);
}
// 一番後ろに立っているビットの位置を取得
int LastBit(i64 x) {
  if (x == 0)
    return 0;
  for (int i = 0; i < 64; i++)
    if (x >> i == 1)
      return i + 1;
  return -1;
}

int main() {
  auto f = [](i64 n) -> int {
    int m = 0;
    while (n) {
      n = n % PopCount(n);
      m++;
    }
    return m;
  };

  int N;
  cin >> N;
  vector<int> X(N);
  for (int &x : X) {
    char c;
    cin >> c;
    x = c - '0';
  }

  reverse(WHOLE(X));
  int pop_all = sum_up(WHOLE(X));
  int R[2] = {0, 0};   // 全体を割ったあまり
  vector<int> Y(N, 0); // １ビットだけを割ったあまり

  // ビットを b ? 降ろす : 立てる
  for (int b = 0; b < 2; b++) {
    int M = pop_all + (b ? -1 : +1);
    int p = 1;
    if (M == 0)
      continue;
    for (int i = 0; i < N; i++) {
      int x = X[i];
      if (x == b)
        Y[i] = p * (b ? -1 : +1);
      R[b] = (R[b] + x * p) % M;
      p = (p * 2) % M;
    }
  }

  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int x = X[i];
    int M = pop_all + (x ? -1 : +1);
    int n = M ? (R[x] + Y[i] + M) % M : 0;
    ans[i] = f(n) + (M ? 1 : 0);
  }

  reverse(WHOLE(ans));
  for (int a : ans)
    cout << a << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template <class F> auto recursive(F f) {
  return [f](auto... a) { return f(f, a...); };
}
template <class T>
using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using vector2d = vector<vector<T>>;
struct fixprec {
  int p;
  fixprec(int p) : p(p) {}
};
ostream &operator<<(ostream &o, fixprec f) {
  return o << fixed << setprecision(f.p);
}

/*!* [iterutil] *!*/
/*+* イテレータの操作 *+*/
template <class I, class V = typename I::value_type>
V sum_up(const I &l, const I &r) {
  V v;
  for (I i = l; i != r; i++)
    v = i == l ? *i : v + *i;
  return v;
}
template <class I, class T = iterator_traits<I>> I operator+(I it, int n) {
  for (int i = 0; i < n; i++)
    it++;
  return it;
}
template <class I, class T = iterator_traits<I>> I operator-(I it, int n) {
  for (int i = 0; i < n; i++)
    it--;
  return it;
}

/*!* [bitutil] *!*/
/*+* ビット操作関連 *+*/
int PopCount(i64 x) {
  x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
  x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
  x = (x & 0x0f0f0f0f0f0f0f0f) + (x >> 4 & 0x0f0f0f0f0f0f0f0f);
  x = (x & 0x00ff00ff00ff00ff) + (x >> 8 & 0x00ff00ff00ff00ff);
  x = (x & 0x0000ffff0000ffff) + (x >> 16 & 0x0000ffff0000ffff);
  x = (x & 0x00000000ffffffff) + (x >> 32 & 0x00000000ffffffff);
  return int(x);
}
// 一番後ろに立っているビットの位置を取得
int LastBit(i64 x) {
  if (x == 0)
    return 0;
  for (int i = 0; i < 64; i++)
    if (x >> i == 1)
      return i + 1;
  return -1;
}

int main() {
  auto f = [](i64 n) -> int {
    int m = 0;
    while (n) {
      n = n % PopCount(n);
      m++;
    }
    return m;
  };

  int N;
  cin >> N;
  vector<int> X(N);
  for (int &x : X) {
    char c;
    cin >> c;
    x = c - '0';
  }

  reverse(WHOLE(X));
  int pop_all = sum_up(WHOLE(X));
  int R[2] = {0, 0}; // 全体を割ったあまり
  vector<int> Y(N);  // １ビットだけを割ったあまり

  // ビットを b ? 降ろす : 立てる
  for (int b = 0; b < 2; b++) {
    int M = pop_all + (b ? -1 : +1);
    int p = 1;
    for (int i = 0; i < N; i++) {
      int x = X[i];
      if (x == b)
        Y[i] = p * (b ? -1 : +1);
      R[b] = (R[b] + x * p) % M;
      p = (p * 2) % M;
    }
  }

  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int x = X[i];
    int M = pop_all + (x ? -1 : +1);
    int n = (R[x] + Y[i] + M) % M;
    ans[i] = f(n) + 1;
  }

  reverse(WHOLE(ans));
  for (int a : ans)
    cout << a << endl;
  return 0;
}