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

#include <bits/stdc++.h>
#define int int64_t
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define REPP(i, n) for (int i = 1; (i) < (int)(n + 1); ++(i))
#define REPR(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = (m)-1; i >= (n); i--)
#define ALL(v) v.begin(), v.end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>());
#define BIT(n) (1LL << (n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x, y) __gcd(x, y)
#define LCM(x, y) x / __gcd(x, y) * y
#define PB push_back
#define MP make_pair
#define endl "\\n"
#define pa cout << ans << endl
#define p(x) cout << x << endl
#define pall(x)                                                                \\
  for (auto iter : x)                                                          \\
  p(iter)
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
using namespace std;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<pair<int, int>>>;
typedef long double LD;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const int INF = 2000000000;
const long double PI = acosl(-1);
void print() { cout << endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0)
    cout << " ";
  print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back())
      cout << " ";
  }
  cout << endl;
}
template <class T> void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}

int modpow(int N, int M, int MOD) {
  return M ? modpow(N * N % MOD, M / 2, MOD) * (M % 2 ? N : (int)1) % MOD
           : (int)1;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int ans = 0;

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

  int pcnt = 0;
  REP(i, N) if (X[i] == '1') pcnt++;
  int p = 0, m = 0;
  REP(i, N) if (X[i] == '1') {
    p = p + modpow((int)2, N - 1 - i, pcnt + 1);
    p %= pcnt + 1;
    if (pcnt > 1) {
      m = m + modpow((int)2, N - 1 - i, pcnt - 1);
      m %= pcnt - 1;
    } else {
      m = 0;
    }
  }
  REP(i, N) {
    if (X[i] == '0') {
      ans = p;
      ans += modpow((int)2, N - 1 - i, pcnt + 1);
      ans %= pcnt + 1;
    } else {
      if (pcnt > 1) {
        ans = m;
        ans -= modpow((int)2, N - 1 - i, pcnt - 1);
        ans += pcnt - 1;
        ans %= pcnt - 1;
      } else {
        cout << 0 << endl;
        continue;
      }
    }
    int cnt = 1;
    while (ans) {
      ans %= PCNT(ans);
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

/*
cd ~/Desktop/Programming/AtCoder/practice/aising2020/aising2020_d && sh
shellscripts/test_cpp.sh cd
~/Desktop/Programming/AtCoder/practice/aising2020/aising2020_d && sh
shellscripts/etest_cpp.sh cd
~/Desktop/Programming/AtCoder/practice/aising2020/aising2020_d && sh
shellscripts/submit_cpp.sh
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int int64_t
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define REPP(i, n) for (int i = 1; (i) < (int)(n + 1); ++(i))
#define REPR(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = (m)-1; i >= (n); i--)
#define ALL(v) v.begin(), v.end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>());
#define BIT(n) (1LL << (n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x, y) __gcd(x, y)
#define LCM(x, y) x / __gcd(x, y) * y
#define PB push_back
#define MP make_pair
#define endl "\\n"
#define pa cout << ans << endl
#define p(x) cout << x << endl
#define pall(x)                                                                \\
  for (auto iter : x)                                                          \\
  p(iter)
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
using namespace std;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<pair<int, int>>>;
typedef long double LD;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const int INF = 2000000000;
const long double PI = acosl(-1);
void print() { cout << endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0)
    cout << " ";
  print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back())
      cout << " ";
  }
  cout << endl;
}
template <class T> void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}

int modpow(int N, int M, int MOD) {
  return M ? modpow(N * N % MOD, M / 2, MOD) * (M % 2 ? N : 1) % MOD : 1;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int ans = 0;

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

  int pcnt = 0;
  REP(i, N) if (X[i] == '1') pcnt++;
  if (pcnt == 0) {
    cout << ans << endl;
    return 0;
  }
  int p = 0, m = 0;
  REP(i, N) if (X[i] == '1') {
    p = p + modpow((int)2, N - 1 - i, pcnt + 1);
    p %= pcnt + 1;
    m = m + modpow((int)2, N - 1 - i, pcnt - 1);
    m %= pcnt - 1;
  }
  REP(i, N) {
    if (X[i] == '0') {
      ans = p;
      ans += modpow((int)2, N - 1 - i, pcnt + 1);
      ans %= pcnt + 1;
    } else {
      ans = m;
      ans -= modpow((int)2, N - 1 - i, pcnt - 1);
      ans += pcnt - 1;
      ans %= pcnt - 1;
    }
    int cnt = 1;
    while (ans) {
      ans %= PCNT(ans);
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

/*
cd ~/Desktop/Programming/AtCoder/practice/aising2020/aising2020_d && sh
shellscripts/test_cpp.sh cd
~/Desktop/Programming/AtCoder/practice/aising2020/aising2020_d && sh
shellscripts/etest_cpp.sh cd
~/Desktop/Programming/AtCoder/practice/aising2020/aising2020_d && sh
shellscripts/submit_cpp.sh
*/