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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
// constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll rui(ll a, ll b, ll MOD) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return ans;
}

int main() {
  int n;
  string x;
  cin >> n >> x;

  int popcount = count(all(x), '1');
  int pp = popcount + 1, pm = popcount - 1;
  int mod_plus = 0, mod_minus = 0;
  rep(i, n) {
    if (x[n - 1 - i] == '1') {
      mod_plus = (mod_plus + rui(2, i, pp)) % pp;
      if (pm)
        mod_minus = (mod_minus + rui(2, i, pm)) % pm;
      else
        mod_minus = 0;
    }
  }
  // cout << mod_plus << " " << mod_minus << ln;
  vector<int> res;
  rep(i, n) { // 前からi番目のビットを反転
    int m;
    if (x[n - 1 - i] == '1' && popcount == 1) {
      res.push_back(0);
      continue;
    };
    if (x[n - 1 - i] == '1')
      m = (mod_minus + pm - rui(2, i, pm)) % pm;
    else
      m = (mod_plus + rui(2, i, pp)) % pp;
    if (m == 0) {
      res.push_back(1);
      continue;
    }
    int cnt = 1;
    // cout << "i:" << i << " m:" << m << ln;
    while (m != 0) {
      int tmp = m, pc = 0;
      while (tmp > 0) {
        if (tmp & 1)
          pc++;
        tmp /= 2;
      }
      if (pc == 0)
        break;
      m %= pc;
      cnt++;
    }
    res.push_back(cnt);
  }
  reverse(all(res));
  for (int a : res)
    cout << a << ln;
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
// constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll rui(ll a, ll b, ll MOD) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return ans;
}

int main() {
  int n;
  string x;
  cin >> n >> x;

  int popcount = count(all(x), '1');
  int pp = popcount + 1, pm = popcount - 1;
  int mod_plus = 0, mod_minus = 0;
  rep(i, n) {
    if (x[n - 1 - i] == '1') {
      mod_plus = (mod_plus + rui(2, i, pp)) % pp;
      mod_minus = (mod_minus + rui(2, i, pm)) % pm;
    }
  }
  // cout << mod_plus << " " << mod_minus << ln;
  vector<int> res;
  rep(i, n) { // 前からi番目のビットを反転
    int m;
    if (x[n - 1 - i] == '1' && popcount == 1) {
      res.push_back(0);
      continue;
    };
    if (x[n - 1 - i] == '1')
      m = (mod_minus + pm - rui(2, i, pm)) % pm;
    else
      m = (mod_plus + rui(2, i, pp)) % pp;
    if (m == 0) {
      res.push_back(1);
      continue;
    }
    int cnt = 1;
    // cout << "i:" << i << " m:" << m << ln;
    while (m != 0) {
      int tmp = m, pc = 0;
      while (tmp > 0) {
        if (tmp & 1)
          pc++;
        tmp /= 2;
      }
      if (pc == 0)
        break;
      m %= pc;
      cnt++;
    }
    res.push_back(cnt);
  }
  reverse(all(res));
  for (int a : res)
    cout << a << ln;
}