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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

ll f(ll n) {
  ll ret = 0;
  while (n > 0) {
    n %= __builtin_popcountll(n);
    ret++;
  }
  return ret;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  ll d = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      d++;
  }

  ll a = d - 1;
  ll b = d;
  ll c = d + 1;
  vector<ll> x(n + 1, 1);
  vector<ll> y(n + 1, 1);
  vector<ll> z(n + 1, 1);
  rep(i, n) {
    if (a > 0)
      x[i + 1] = x[i] * 2 % a;
    if (b > 0)
      y[i + 1] = y[i] * 2 % b;
    if (c > 0)
      z[i + 1] = z[i] * 2 % c;
  }

  ll xx = 0;
  ll yy = 0;
  ll zz = 0;
  rep(i, n) {
    if (s[i] == '1') {
      if (a > 0)
        (xx += x[i]) %= a;
      if (b > 0)
        (yy += y[i]) %= b;
      if (c > 0)
        (zz += z[i]) %= c;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (a <= 0) {
        cout << 0 << endl;
        continue;
      } else {
        ll ans = f((xx + a - x[i]) % a);
        cout << ans + 1 << endl;
      }
    } else {
      ll ans = f((zz + z[i]) % c);
      cout << ans + 1 << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

ll f(ll n) {
  ll ret = 0;
  while (n > 0) {
    n %= __builtin_popcountll(n);
    ret++;
  }
  return ret;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  ll d = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      d++;
  }

  ll a = d - 1;
  ll b = d;
  ll c = d + 1;
  vector<ll> x(n + 1, 1);
  vector<ll> y(n + 1, 1);
  vector<ll> z(n + 1, 1);
  rep(i, n) {
    if (a > 0)
      x[i + 1] = x[i] * 2 % a;
    if (b > 0)
      y[i + 1] = y[i] * 2 % b;
    if (c > 0)
      z[i + 1] = z[i] * 2 % c;
  }

  ll xx = 0;
  ll yy = 0;
  ll zz = 0;
  rep(i, n) {
    if (s[i] == '1') {
      (xx += x[i]) %= a;
      (yy += y[i]) %= b;
      (zz += z[i]) %= c;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (a <= 0) {
        cout << 0 << endl;
        continue;
      } else {
        ll ans = f((xx + a - x[i]) % a);
        cout << ans + 1 << endl;
      }
    } else {
      ll ans = f((zz + z[i]) % c);
      cout << ans + 1 << endl;
    }
  }

  return 0;
}