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;

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

  ll a = d - 1;
  ll b = d + 1;

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

  ll xx = 0;
  ll yy = 0;
  rep(i, n) {
    if (s[i] == '1') {
      xx += x[i];
      yy += y[i];
    }
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (a <= 0) {
        cout << 0 << endl;
        continue;
      }
      ll ans = f((xx + a - x[i]) % a) + 1;
      cout << ans << endl;
    } else {
      ll ans = f((yy + y[i]) % b) + 1;
      cout << ans << 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;

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

  vector<vector<ll>> a(n, vector<ll>(2));
  {
    for (int j = 0; j < 2; j++) {
      ll e = d;
      if (j == 0)
        e++;
      else
        e--;
      if (e < 0)
        continue;
      ll t = 1;
      for (int i = 0; i < n; i++) {
        if (i > 0)
          t *= 2;
        a[n - i - 1][j] = t % e;
      }
    }
  }
  vector<ll> sum(2);
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        sum[j] += a[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    ll e = d;
    ll idx = s[i] - '0';
    ll t = sum[idx];
    if (s[i] == '1') {
      e--;
      if (e <= 0) {
        cout << 0 << endl;
        continue;
      }
      t = (t + e - a[i][idx]) % e;
    } else {
      e++;
      t = (t + a[i][idx]) % e;
    }
    cout << f(t) + 1 << endl;
  }

  return 0;
}