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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

#ifdef __GNUC__
#define popcount __builtin_popcountll
#else
// 立っているビットを数える
constexpr int popcount(u64 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);
  return (int)((x & 0x00000000ffffffff) + (x >> 32 & 0x00000000ffffffff));
}
#endif

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  string X;
  cin >> N >> X;

  int pc = 0;
  F0R(i, N) {
    if (X[i] == '1')
      pc++;
  }
  int pcl = pc - 1, pcr = pc + 1;

  auto xmod = [&](int v) {
    int n = 0;
    F0R(i, N) {
      n <<= 1;
      if (X[i] == '1')
        n++;
      n %= v;
    }
    return n;
  };

  // X % (pc, pcl, pcr)
  // int xm = xmod(pc);
  int xml = pcl ? xmod(pcl) : 0;
  int xmr = pcr ? xmod(pcr) : 0;

  // pow(2, n) % pc
  int twol = 1, twor = 1;

  auto calc = [](int n) {
    int r = 0;
    while (n) {
      // debug(n);
      n %= popcount(n);
      r++;
    }
    return r;
  };

  vector<int> rs(N);
  RF0(i, N) {
    if (pcl)
      twol %= pcl;
    else
      twol = 0;
    if (pcr)
      twor %= pcr;
    else
      twor = 0;

    int n;
    if (X[i] == '0') {
      n = xmr + twor;
      if (n >= pcr)
        n -= pcr;
      rs[i] = 1 + calc(n);
    } else {
      if (pcl) {
        n = xml - twol;
        if (n < 0)
          n += pcl;
        rs[i] = 1 + calc(n);
      } else {
        rs[i] = 0;
      }
    }

    twol <<= 1;
    twor <<= 1;
  }

  F0R(i, N) { cout << rs[i] << newl; }
}
#endif


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

#ifdef __GNUC__
#define popcount __builtin_popcountll
#else
// 立っているビットを数える
constexpr int popcount(u64 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);
  return (int)((x & 0x00000000ffffffff) + (x >> 32 & 0x00000000ffffffff));
}
#endif

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  string X;
  cin >> N >> X;

  int pc = 0;
  F0R(i, N) {
    if (X[i] == '1')
      pc++;
  }
  int pcl = pc - 1, pcr = pc + 1;

  auto xmod = [&](int v) {
    int n = 0;
    F0R(i, N) {
      n <<= 1;
      if (X[i] == '1')
        n++;
      n %= v;
    }
    return n;
  };

  // X % (pc, pcl, pcr)
  // int xm = xmod(pc);
  int xml = xmod(pcl);
  int xmr = xmod(pcr);

  // pow(2, n) % pc
  int twol = 1, twor = 1;

  auto calc = [](int n) {
    int r = 0;
    while (n) {
      n %= popcount(n);
      r++;
    }
    return r;
  };

  vector<int> rs(N);
  RF0(i, N) {
    twol %= pcl;
    twor %= pcr;

    int n;
    if (X[i] == '0') {
      n = xmr + twor;
      if (n >= pcr)
        n -= pcr;
    } else {
      n = xml - twol;
      if (n < 0)
        n += pcl;
    }
    rs[i] = 1 + calc(n);

    twol <<= 1;
    twor <<= 1;
  }

  F0R(i, N) { cout << rs[i] << newl; }
}
#endif