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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

ll pow_mod(ll n, ll p, ll mod) {
  if (p == 0) {
    return 1;
  } else if (p % 2 == 1) {
    return pow_mod(n, p - 1, mod) * n % mod;
  } else {
    ll s = pow_mod(n, p / 2, mod);
    return s * s % mod;
  }
}

template <typename T> T calc_mod(string s, T d) {
  T r = 0;
  REP(i, sz(s)) r = (r * 2 + (s[i] - '0')) % d;
  return r;
}

void solve() {
  int n;
  string x;
  cin >> n >> x;
  int q = 0;
  REP(i, n) if (x[i] == '1') q++;
  int u = 0;
  if (q - 1 > 0)
    u = calc_mod(x, q - 1);
  int v = calc_mod(x, q + 1);
  REP(i, n) {
    string y = x;
    bool b = x[i] == '0';
    if (b)
      y[i] = '1';
    else
      y[i] = '0';
    int p = q;
    p += (b ? 1 : -1);
    if (p <= 0) {
      cout << 0 << endl;
      continue;
    }
    int ans = 1;
    int z = pow_mod(2, n - i - 1, p);
    // cout << "? " << z << " : " << y << "%" << p << endl;
    if (b) {
      z = (v + z) % p;
    } else {
      z = (u - z + p) % p;
    }
    while (z > 0) {
      ans++;
      int pc = __builtin_popcount(z);
      z %= pc;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

template <typename T> T calc_mod(string s, T d) {
  T r = 0;
  REP(i, sz(s)) r = (r * 2 + (s[i] - '0')) % d;
  return r;
}

void solve() {
  int n;
  string x;
  cin >> n >> x;
  int q = 0;
  REP(i, n) if (x[i] == '1') q++;
  int u = calc_mod(x, q - 1);
  int v = calc_mod(x, q + 1);
  REP(i, n) {
    string y = x;
    bool b = x[i] == '0';
    if (b)
      y[i] = '1';
    else
      y[i] = '0';
    int p = q;
    p += (b ? 1 : -1);
    if (p <= 0) {
      cout << 0 << endl;
      continue;
    }
    int ans = 1;
    int z = 1;
    REP(j, n - i - 1) z = (z * 2) % p;
    // cout << "? " << z << " : " << y << "%" << p << endl;
    if (b) {
      z = (v + z) % p;
    } else {
      z = (u - z + p) % p;
    }
    while (z > 0) {
      ans++;
      int pc = __builtin_popcount(z);
      z %= pc;
    }
    cout << ans << endl;
  }
}