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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef ll Weight;
typedef vector<vector<Weight>> Graph;
void add_edge(vector<vector<Weight>> &g, Weight from, Weight to,
              bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

ll popcount(ll a) {
  ll ret = 0;
  while (a > 0) {
    if (a & 1)
      ret++;
    a >>= 1;
  }
  return ret;
}

ll f(ll a) {
  ll cnt = 1;
  while (a > 0) {
    a = a % popcount(a);
    cnt++;
  }
  return cnt;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popcnt = 0;
  ll x = 0;
  rep(i, n) {
    if (s[i] == '1') {
      popcnt++;
    }
  }

  if (n == 1 && s == "1") {
    cout << 0 << endl;
    return 0;
  }

  ll xmodp = 0, xmodm = 0;
  vector<ll> pow2p(n + 1), pow2m(n + 1);
  pow2p[0] = 1;
  pow2m[0] = 1;
  rrep(i, n) {
    if (s[i] == '1') {
      xmodp += pow2p[n - i - 1];
      xmodm += pow2m[n - i - 1];
      xmodp %= popcnt + 1;
      if (popcnt - 1 > 0)
        xmodm %= popcnt - 1;
    }
    pow2p[n - i] = pow2p[n - i - 1] * 2 % (popcnt + 1);
    if (popcnt - 1 > 0)
      pow2m[n - i] = pow2m[n - i - 1] * 2 % (popcnt - 1);
  }

  if (popcnt == 1) {
    rep(i, n) {
      if (s[i] == '1') {
        cout << 0 << endl;
      } else {
        cout << f(xmodp + pow2p[n - i - 1] >= popcnt + 1
                      ? xmodp + pow2p[n - i - 1] - (popcnt + 1)
                      : xmodp + pow2p[n - i - 1])
             << endl;
      }
    }
    return 0;
  }

  rep(i, n) {
    if (s[i] == '1') {
      cout << f(xmodm - pow2m[n - i - 1] < 0
                    ? xmodm - pow2m[n - i - 1] + popcnt - 1
                    : xmodm - pow2m[n - i - 1])
           << endl;
    } else {
      cout << f(xmodp + pow2p[n - i - 1] >= popcnt + 1
                    ? xmodp + pow2p[n - i - 1] - (popcnt + 1)
                    : xmodp + pow2p[n - i - 1])
           << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef ll Weight;
typedef vector<vector<Weight>> Graph;
void add_edge(vector<vector<Weight>> &g, Weight from, Weight to,
              bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

ll popcount(ll a) {
  ll ret = 0;
  while (a > 0) {
    if (a & 1)
      ret++;
    a >>= 1;
  }
  return ret;
}

ll f(ll a) {
  ll cnt = 1;
  while (a > 0) {
    a = a % popcount(a);
    cnt++;
  }
  return cnt;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popcnt = 0;
  ll x = 0;
  rep(i, n) {
    if (s[i] == '1') {
      popcnt++;
    }
  }

  if (n == 1 && s == "1") {
    cout << 0 << endl;
    return 0;
  }

  ll xmodp = 0, xmodm = 0;
  vector<ll> pow2p(n + 1), pow2m(n + 1);
  pow2p[0] = 1;
  pow2m[0] = 1;
  rrep(i, n) {
    if (s[i] == '1') {
      xmodp += pow2p[n - i - 1];
      xmodm += pow2m[n - i - 1];
      xmodp %= popcnt + 1;
      xmodm %= popcnt - 1;
    }
    pow2p[n - i] = pow2p[n - i - 1] * 2 % (popcnt + 1);
    pow2m[n - i] = pow2m[n - i - 1] * 2 % (popcnt - 1);
  }

  rep(i, n) {
    if (s[i] == '1') {
      cout << f(xmodm - pow2m[n - i - 1] < 0
                    ? xmodm - pow2m[n - i - 1] + popcnt - 1
                    : xmodm - pow2m[n - i - 1])
           << endl;
    } else {
      cout << f(xmodp + pow2p[n - i - 1] >= popcnt
                    ? xmodp + pow2p[n - i - 1] - (popcnt + 1)
                    : xmodp + pow2p[n - i - 1])
           << endl;
    }
  }
}