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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
const double EPS = 1e-9;
const int INF = 1e9 + 10;
const ll mod = 998244353;
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;
}
#define print(x) cerr << #x << " is " << x << endl;
// #define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << "
// " << __FILE__ << endl;

ll f(ll x, ll cnt) {
  // fprintf(stderr, "x = %d, cnt = %d\\n", x, cnt);
  ll x0 = x;
  if (x == 0)
    return cnt;
  ll ans = 0;
  while (x) {
    if (x % 2 == 1)
      ans++;
    x /= 2;
  }
  // fprintf(stderr, "ans = %d\\n", ans);
  return f(x0 % ans, cnt + 1);
}
int main() {

#if 0
    ll N = 20000;
    ll cnt = 0;
        vector<ll> A(N), base0(N + 1),base1(N + 1);

    rep(i, N){
        A[i] = 1;
        cnt += A[i];
    }
#else
  ll N;
  ll cnt = 0;
  cin >> N;

  string S;
  cin >> S;
  vector<ll> A(N), base0(N + 1), base1(N + 1);
  rep(i, N) {
    if (S[i] == '0')
      A[i] = 0;
    else
      A[i] = 1;
    cnt += A[i];
  }
#endif
  ll r = 0, mod = cnt + 1;
  ll base = 1;
  rep(j, N) {
    r += A[N - j - 1] * base;
    r %= mod;
    base0[j] = base;
    (base *= 2) %= mod;
  }
  ll rbase0 = r;

  r = 0, mod = cnt - 1;
  if (mod) {

    base = 1;
    rep(j, N) {
      r += A[N - j - 1] * base;
      r %= mod;
      base1[j] = base;
      (base *= 2) %= mod;
    }
  }
  ll rbase1 = r;
  rep(i, N) {
    ll mod;

    ll r = 0;
    if (A[i] == 0)
      mod = cnt + 1;
    if (A[i] == 1)
      mod = cnt - 1;
    if (mod == 0) {
      cout << 0 << endl;
      continue;
    }
    if (A[i] == 0) {
      ll tmp = (base0[N - i - 1]) % mod;
      r = rbase0 + tmp;
    } else {
      ll tmp = (base1[N - i - 1]) % mod;
      r = rbase1 + mod - tmp;
    }
    r %= mod;
#if 0        
        rep(j, N){
            if (N - j - 1 == i){
                r += (A[N - j - 1]  ^ 1)* base;
            } else {
                r += A[N - j - 1] * base;
            }
            r %= mod;
            //fprintf(stderr, "j = %d, A[x] = %d, r = %d, base = %d\\n", j, A[N - j - 1], r, base);
            (base *= 2) %= mod;
            
        }
#endif
    cout << f(r, 1) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
const double EPS = 1e-9;
const int INF = 1e9 + 10;
const ll mod = 998244353;
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;
}
#define print(x) cerr << #x << " is " << x << endl;
// #define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << "
// " << __FILE__ << endl;

ll f(ll x, ll cnt) {
  // fprintf(stderr, "x = %d, cnt = %d\\n", x, cnt);
  ll x0 = x;
  if (x == 0)
    return cnt;
  ll ans = 0;
  while (x) {
    if (x % 2 == 1)
      ans++;
    x /= 2;
  }
  // fprintf(stderr, "ans = %d\\n", ans);
  return f(x0 % ans, cnt + 1);
}
int main() {
  ll N;
  ll cnt = 0;
  cin >> N;
  string S;
  cin >> S;
  vector<ll> A(N), base0(N + 1), base1(N + 1);
  rep(i, N) {
    if (S[i] == '0')
      A[i] = 0;
    else
      A[i] = 1;
    cnt += A[i];
  }
  ll r = 0, mod = cnt + 1;
  ll base = 1;
  rep(j, N) {
    r += A[N - j - 1] * base;
    r %= mod;
    base0[j] = base;
    (base *= 2) %= mod;
  }
  ll rbase0 = r;

  r = 0, mod = cnt - 1;
  base = 1;
  rep(j, N) {
    r += A[N - j - 1] * base;
    r %= mod;
    base1[j] = base;
    (base *= 2) %= mod;
  }
  ll rbase1 = r;
  rep(i, N) {
    ll mod;

    ll r = 0;
    if (A[i] == 0)
      mod = cnt + 1;
    if (A[i] == 1)
      mod = cnt - 1;
    if (mod == 0) {
      cout << 1 << endl;
      continue;
    }
    if (A[i] == 0) {
      ll tmp = (base0[N - i - 1]) % mod;
      r = rbase0 + tmp;
    } else {
      ll tmp = (base1[N - i - 1]) % mod;
      r = rbase1 + mod - tmp;
    }
    r %= mod;
#if 0        
        rep(j, N){
            if (N - j - 1 == i){
                r += (A[N - j - 1]  ^ 1)* base;
            } else {
                r += A[N - j - 1] * base;
            }
            r %= mod;
            //fprintf(stderr, "j = %d, A[x] = %d, r = %d, base = %d\\n", j, A[N - j - 1], r, base);
            (base *= 2) %= mod;
            
        }
#endif
    cout << f(r, 1) << endl;
  }
  return 0;
}