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

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
#define repl(i, n) for (long long i = 0; i < (n); ++i)
#define reppl(i, n, m) for (long long i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT> void printvec(vector<AT> &ar) {
  rep(i, ar.size() - 1) cout << ar[i] << " ";
  cout << ar[ar.size() - 1] << endl;
}
template <typename Q> void printvvec(vector<vector<Q>> &ar) {
  rep(i, ar.size()) {
    rep(j, ar[0].size() - 1) cout << ar[i][j] << " ";
    cout << ar[i][ar[0].size() - 1] << endl;
  }
}
template <typename S> bool range(S a, S b, S x) { return (a <= x && x < b); }

int ppc(int s) {
  int ans = 0;
  rep(i, 30) if (s >> i & 1) ans++;
  return s % ans;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  vector<int> ar(n + 3, 0);
  repp(i, n + 3, 1) { ar[i] = ar[ppc(i)] + 1; }
  int c = 0;
  rep(i, n) if (x[i] == '1') c++;
  vector<int> cp(n + 1, 0);
  vector<int> cm(n + 1, 0);
  cp[0] = 1 % (c + 1);
  if (c - 1 > 0)
    cm[0] = 1 % (c - 1);
  rep(i, n) { cp[i + 1] = cp[i] * 2 % (c + 1); }
  if (c - 1 > 0) {
    rep(i, n) { cm[i + 1] = cm[i] * 2 % (c - 1); }
  }
  int ap = 0;
  int am = 0;
  rep(i, n) {
    if (x[n - 1 - i] == '1') {
      ap += cp[i];
      ap %= c + 1;
    }
  }
  if (c - 1 > 0) {
    rep(i, n) {
      if (x[n - 1 - i] == '1') {
        am += cm[i];
        am %= c - 1;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    // cout << i << " ";
    int ans = 0;
    if (x[i] == '1' && c - 1 > 0) {
      ans = am + c - 1;
      ans -= cm[n - 1 - i];
      ans %= c - 1; // cout << "! ";
      cout << ar[ans] + 1 << endl;
    } else if (x[i] == '1' && c - 1 == 0) {
      cout << 0 << endl;
    } else {
      ans = ap;
      ans += cp[n - 1 - i];
      ans %= c + 1; // cout << "!! ";
      cout << ar[ans] + 1 << endl;
    }
  }
  // printvec(ar);
  // printvec(cp); printvec(cm); cout << c << endl;
  // cout << ap << " " << am << endl;
  // string y = "abc"; cout << y[0] << y[1] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
#define repl(i, n) for (long long i = 0; i < (n); ++i)
#define reppl(i, n, m) for (long long i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT> void printvec(vector<AT> &ar) {
  rep(i, ar.size() - 1) cout << ar[i] << " ";
  cout << ar[ar.size() - 1] << endl;
}
template <typename Q> void printvvec(vector<vector<Q>> &ar) {
  rep(i, ar.size()) {
    rep(j, ar[0].size() - 1) cout << ar[i][j] << " ";
    cout << ar[i][ar[0].size() - 1] << endl;
  }
}
template <typename S> bool range(S a, S b, S x) { return (a <= x && x < b); }

int ppc(int s) {
  int ans = 0;
  rep(i, 10) if (s >> i & 1) ans++;
  return s % ans;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  vector<int> ar(n + 3, 0);
  repp(i, n + 3, 1) { ar[i] = ar[ppc(i)] + 1; }
  int c = 0;
  rep(i, n) if (x[i] == '1') c++;
  vector<int> cp(n + 1, 0);
  vector<int> cm(n + 1, 0);
  cp[0] = 1 % (c + 1);
  if (c - 1 > 0)
    cm[0] = 1 % (c - 1);
  rep(i, n) { cp[i + 1] = cp[i] * 2 % (c + 1); }
  if (c - 1 > 0) {
    rep(i, n) { cm[i + 1] = cm[i] * 2 % (c - 1); }
  }
  int ap = 0;
  int am = 0;
  rep(i, n) {
    if (x[n - 1 - i] == '1') {
      ap += cp[i];
      ap %= c + 1;
    }
  }
  if (c - 1 > 0) {
    rep(i, n) {
      if (x[n - 1 - i] == '1') {
        am += cm[i];
        am %= c - 1;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    // cout << i << " ";
    int ans = 0;
    if (x[i] == '1' && c - 1 > 0) {
      ans = am + c - 1;
      ans -= cm[n - 1 - i];
      ans %= c - 1; // cout << "! ";
      cout << ar[ans] + 1 << endl;
    } else if (x[i] == '1' && c - 1 == 0) {
      cout << 1 << endl;
    } else {
      ans = ap;
      ans += cp[n - 1 - i];
      ans %= c + 1; // cout << "!! ";
      cout << ar[ans] + 1 << endl;
    }
  }
  // printvec(ar);
  // printvec(cp); printvec(cm); cout << c << endl;
  // cout << ap << " " << am << endl;
  // string y = "abc"; cout << y[0] << y[1] << endl;
}