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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define fs first
#define sc second

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int mod;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int popcount(int x) {
  int res = 0;
  while (x) {
    res += (x & 1);
    x >>= 1;
  }
  return res;
}

int f(int x, int c) {
  if (x == 0)
    return c;
  return f(x % popcount(x), c + 1);
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int k = count(x.cbegin(), x.cend(), '1');
  vector<int> s(n), l(n);
  int sums = 0;
  int suml = 0;
  if (k > 1) {
    int t = 1 % (k - 1);
    for (int i = 0; i < n; i++) {
      s[i] = t;
      t = (t * 2) % (k - 1);
      if (x[n - 1 - i] == '1') {
        sums = (sums + s[i]) % (k - 1);
      }
    }
  }
  int t = 1 % (k + 1);
  for (int i = 0; i < n; i++) {
    l[i] = t;
    t = (t * 2) % (k + 1);
    if (x[n - 1 - i] == '1') {
      suml = (suml + l[i]) % (k + 1);
    }
  }
  if (k > 1) {
    rep(i, n) {
      if (x[i] == '1') {
        int d = (sums + (k - 1) - s[n - 1 - i]) % (k - 1);
        cout << f(d, 1) << endl;
      } else {
        int d = (suml + l[n - 1 - i]) % (k + 1);
        cout << f(d, 1) << endl;
      }
    }
  } else {
    rep(i, n) {
      if (x[i] == '1') {
        cout << "0\\n";
      } else {
        int d = (suml + l[n - 1 - i]) % (k + 1);
        cout << f(d, 1) << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define fs first
#define sc second

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int mod;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int popcount(int x) {
  int res = 0;
  while (x) {
    res += (x & 1);
    x >>= 1;
  }
  return res;
}

int f(int x, int c) {
  if (x == 0)
    return c;
  return f(x % popcount(x), c + 1);
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int k = count(x.cbegin(), x.cend(), '1');
  vector<int> s(n), l(n);
  int sums = 0;
  int suml = 0;
  if (k > 1) {
    int t = 1 % (k - 1);
    for (int i = 0; i < n; i++) {
      s[i] = t;
      t = (t * 2) % (k - 1);
      if (x[n - 1 - i] == '1') {
        sums = (sums + s[i]) % (k - 1);
      }
    }
  }
  if (k < n) {
    int t = 1 % (k + 1);
    for (int i = 0; i < n; i++) {
      l[i] = t;
      t = (t * 2) % (k + 1);
      if (x[n - 1 - i] == '1') {
        suml = (suml + l[i]) % (k + 1);
      }
    }
  }
  rep(i, n) {
    if (x[i] == '1') {
      int d = (sums + (k - 1) - s[n - 1 - i]) % (k - 1);
      cout << f(d, 1) << endl;
    } else {
      int d = (suml + l[n - 1 - i]) % (k + 1);
      cout << f(d, 1) << endl;
    }
  }
}