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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int mod = 998244353;

struct mint {
  long long x;
  mint(long long 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(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  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, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
bool operator==(const mint &a, const mint &b) { return a.x == b.x; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  string s;
  cin >> n >> s;

  int cnt = 0;
  rep(i, n) if (s[i] == '1') cnt++;

  if (cnt == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  } else if (cnt == 1) {
    rep(i, n) {
      if (s[i] == '1') {
        cout << 0 << endl;
      } else {
        if (i != n - 1) {
          if (s[n - 1] == '1') {
            cout << 2 << endl;
          } else {
            cout << 1 << endl;
          }
        } else {
          cout << 2 << endl;
        }
      }
    }
    return 0;
  }

  mod = cnt + 1;
  mint plus = 0;
  rep(i, n) {
    if (s[i] == '1') {
      plus += mint(2).pow(n - 1 - i);
    }
  }

  mod = cnt - 1;
  mint minus = 0;
  rep(i, n) {
    if (s[i] == '1') {
      minus += mint(2).pow(n - 1 - i);
    }
  }

  vector<int> ans(n);
  rep(i, n) {
    int x = 0;
    if (s[i] == '0') {
      mod = cnt + 1;
      mint now = plus.x;
      now += mint(2).pow(n - 1 - i);
      x = now.x;
    } else {
      mod = cnt - 1;
      mint now = minus.x;
      now -= mint(2).pow(n - 1 - i);
      x = now.x;
    }
    int res = 1;
    while (x > 0) {
      int pc = __builtin_popcount(x);
      x = x % pc;
      res++;
    }
    ans[i] = res;
  }
  rep(i, n) cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int mod = 998244353;

struct mint {
  long long x;
  mint(long long 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(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  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, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
bool operator==(const mint &a, const mint &b) { return a.x == b.x; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  string s;
  cin >> n >> s;

  int cnt = 0;
  rep(i, n) if (s[i] == '1') cnt++;

  mod = cnt + 1;
  mint plus = 0;
  rep(i, n) {
    if (s[i] == '1') {
      plus += mint(2).pow(n - 1 - i);
    }
  }
  mint minus = 0;
  if (cnt != 0) {
    mod = cnt - 1;
    rep(i, n) {
      if (s[i] == '1') {
        minus += mint(2).pow(n - 1 - i);
      }
    }
  }

  vector<int> ans(n);
  rep(i, n) {
    int x = 0;
    if (s[i] == '0') {
      mod = cnt + 1;
      mint now = plus.x;
      now += mint(2).pow(n - 1 - i);
      x = now.x;
    } else {
      mod = cnt - 1;
      mint now = minus.x;
      now -= mint(2).pow(n - 1 - i);
      x = now.x;
    }
    int cnt = 1;
    while (x > 0) {
      int pc = __builtin_popcount(x);
      x = x % pc;
      cnt++;
    }
    ans[i] = cnt;
  }

  rep(i, n) cout << ans[i] << endl;
}