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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
#define INF int(1e9)

template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &p) {
  ost << "(" << p.first << ", " << p.second << ")";
  return ost;
}
#define ostream_container                                                      \\
  {                                                                            \\
    ost << "{";                                                                \\
    for (const auto &t : v) {                                                  \\
      if (&t != &*v.begin())                                                   \\
        ost << ", ";                                                           \\
      ost << t;                                                                \\
    }                                                                          \\
    ost << "}";                                                                \\
    return ost;                                                                \\
  }
template <class T>
ostream &operator<<(ostream &ost, const vector<T> &v) ostream_container
    template <class T>
    ostream &operator<<(ostream &ost, const set<T> &v) ostream_container
    template <class T, class U>
    ostream &operator<<(ostream &ost, const map<T, U> &v) ostream_container

    template <class H>
    void recursive_debug(string s, H h) {
  cerr << "\\033[33m" << s << " = " << h << endl << "\\033[m";
}
template <class H, class... T> void recursive_debug(string s, H h, T... t) {
  int comma = s.find(',');
  cerr << "\\033[33m" << s.substr(0, comma) << " = " << h << ", ";
  recursive_debug(s.substr(comma + 1), t...);
}
#define debug(...) recursive_debug(#__VA_ARGS__, __VA_ARGS__)

ll mul(ll a, ll b, ll mod) { return (a * b) % mod; }
ll mpow(ll a, ll r, ll mod) {
  if (r == 0)
    return 1;
  return mul(mpow(mul(a, a, mod), (r / 2), mod), (r & 1 ? a : 1), mod);
}

void f(ll x, ll &cnt) {
  if (x == 0)
    return;
  cnt++;
  f(x % __builtin_popcountll(x), cnt);
}

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll M = count(all(s), '1');
  ll fx1 = 0, fx2 = 0;
  reverse(all(s));
  rep(i, n) {
    if (s[i] == '1') {
      if (M == 1) {
        fx1 = 0;
      } else {
        fx1 += mpow(2, i, M - 1);
        fx2 += mpow(2, i, M + 1);
      }
    }
  }
  vector<ll> res;
  rep(i, n) {
    ll fx;
    if (s[i] == '1' && M == 1) {
      res.push_back(0);
      continue;
    } else if (s[i] == '1') {
      fx = (fx1 - mpow(2, i, M - 1) + (M - 1)) % (M - 1);
    } else {
      fx = (fx2 + mpow(2, i, M + 1) + (M + 1)) % (M + 1);
    }
    ll cnt = 1;
    f(fx, cnt);
    res.push_back(cnt);
  }
  reverse(all(res));
  rep(i, n) { cout << res[i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
#define INF int(1e9)

template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &p) {
  ost << "(" << p.first << ", " << p.second << ")";
  return ost;
}
#define ostream_container                                                      \\
  {                                                                            \\
    ost << "{";                                                                \\
    for (const auto &t : v) {                                                  \\
      if (&t != &*v.begin())                                                   \\
        ost << ", ";                                                           \\
      ost << t;                                                                \\
    }                                                                          \\
    ost << "}";                                                                \\
    return ost;                                                                \\
  }
template <class T>
ostream &operator<<(ostream &ost, const vector<T> &v) ostream_container
    template <class T>
    ostream &operator<<(ostream &ost, const set<T> &v) ostream_container
    template <class T, class U>
    ostream &operator<<(ostream &ost, const map<T, U> &v) ostream_container

    template <class H>
    void recursive_debug(string s, H h) {
  cerr << "\\033[33m" << s << " = " << h << endl << "\\033[m";
}
template <class H, class... T> void recursive_debug(string s, H h, T... t) {
  int comma = s.find(',');
  cerr << "\\033[33m" << s.substr(0, comma) << " = " << h << ", ";
  recursive_debug(s.substr(comma + 1), t...);
}
#define debug(...) recursive_debug(#__VA_ARGS__, __VA_ARGS__)

ll mul(ll a, ll b, ll mod) { return (a * b) % mod; }
ll mpow(ll a, ll r, ll mod) {
  if (r == 0)
    return 1;
  return mul(mpow(mul(a, a, mod), (r / 2), mod), (r & 1 ? a : 1), mod);
}

void f(ll x, ll &cnt) {
  if (x == 0)
    return;
  cnt++;
  f(x % __builtin_popcountll(x), cnt);
}

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll M = count(all(s), '1');
  ll fx1 = 0, fx2 = 0;
  reverse(all(s));
  rep(i, n) {
    if (s[i] == '1') {
      fx1 += mpow(2, i, M - 1);
      fx2 += mpow(2, i, M + 1);
    }
  }
  vector<ll> res;
  rep(i, n) {
    ll fx;
    if (s[i] == '1') {
      fx = (fx1 - mpow(2, i, M - 1) + (M - 1)) % (M - 1);
    } else {
      fx = (fx2 + mpow(2, i, M + 1) + (M + 1)) % (M + 1);
    }
    ll cnt = 1;
    f(fx, cnt);
    res.push_back(cnt);
  }
  reverse(all(res));
  rep(i, n) { cout << res[i] << endl; }

  return 0;
}