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

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

ll N;
string X;
void input() {
  fast_io();
  cin >> N;
  cin >> X;
}

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

vector<ll> memo;
ll f(ll n) {
  if (n == 0)
    return 0LL;
  if (memo[n])
    return memo[n];
  return memo[n] = (1LL + f(n % __builtin_popcountll(n)));
}

int solve() {
  ll px = 0LL;
  for (int i = 0; i < N; i++)
    if (X[i] == '1')
      px++;
  ll XmodP = 0LL, XmodM = 0LL;
  if (px > 2) {
    for (int i = 0; i < N; i++) {
      XmodP *= 2LL;
      XmodM *= 2LL;
      if (X[i] == '1') {
        XmodP++;
        XmodM++;
      }
      XmodP %= (px + 1);
      XmodM %= (px - 1);
    }
  }
  if (px == 1 || px == 2) {
    for (int i = 0; i < N; i++) {
      XmodP *= 2LL;
      if (X[i] == '1') {
        XmodP++;
      }
      XmodP %= (px + 1);
    }
  }
  vector<ll> one_step(N, 0);
  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      one_step[i] = XmodP + mod_pow(2LL, N - 1 - i, px + 1);
      one_step[i] %= (px + 1);
    } else {
      if (px > 1) {
        one_step[i] = XmodM - mod_pow(2LL, N - 1 - i, px - 1);
        one_step[i] += (px - 1);
        one_step[i] %= (px - 1);
      } else {
        one_step[i] = -1;
      }
    }
  }
  memo.assign(N + 10, 0LL);
  for (int i = 0; i < N; i++) {
    if (one_step[i] == -1)
      cout << 0 << endl;
    else
      cout << (f(one_step[i]) + 1LL) << "\\n";
  }
  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int N;
string X;
void input() {
  fast_io();
  cin >> N;
  cin >> X;
}

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

vector<int> memo;
int f(int n) {
  if (n == 0)
    return 0;
  if (memo[n])
    return memo[n];
  return memo[n] = 1 + f(n % __builtin_popcount(n));
}

int solve() {
  int px = 0;
  for (int i = 0; i < X.length(); i++) {
    if (X[i] == '1')
      px++;
  }
  int XmodP = 0, XmodM = 0;
  if (px >= 2) {
    for (int i = 0; i < X.length(); i++) {
      XmodP <<= 1;
      XmodM <<= 1;
      if (X[i] == '1') {
        XmodP++;
        XmodM++;
      }
      XmodP %= (px + 1);
      XmodM %= (px - 1);
    }
  }
  if (px == 1) {
    for (int i = 0; i < X.length(); i++) {
      XmodP <<= 1;
      if (X[i] == '1') {
        XmodP++;
      }
      XmodP %= (px + 1);
    }
  }
  vector<int> one_step(N, 0);
  for (int i = 0; i < N; i++) {
    if (X[i] == '0') {
      one_step[i] = XmodP + mod_pow(2, N - 1 - i, px + 1);
      one_step[i] %= (px + 1);
    } else {
      one_step[i] = XmodM - mod_pow(2, N - 1 - i, px - 1);
      one_step[i] += (px - 1);
      one_step[i] %= (px - 1);
    }
  }
  memo.assign(N + 10, 0);
  for (int i = 0; i < N; i++) {
    cout << (f(one_step[i]) + 1) << "\\n";
  }
  return 0;
}

int test(int n) {
  cout << bitset<10>(n) << " ";
  while (n > 0) {
    cout << n << " ";
    n %= __builtin_popcount(n);
  }
  cout << 0 << endl;
  return 0;
}

int main() {
  input();
  solve();
  return 0;
}