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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i <= (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

ll modpow(ll a, ll n, ll p) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % p;
    a = a * a % p;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  reverse(all(x));

  vector<ll> ans(n, 0);

  ll count = 0;
  rep(i, n) if (x[i] == '1') count++;
  ll ans1 = 0;
  rep(i, n) if (x[i] == '1') ans1 += modpow(2, i, count + 1), ans1 %= count + 1;
  ll ans3 = 0;
  if (count > 1)
    rep(i, n) if (x[i] == '1') ans3 += modpow(2, i, count - 1),
        ans3 %= count - 1;

  rep(i, n) {
    if (count > 1 && x[i] == '1')
      ans[i] = (ans3 - modpow(2, i, count - 1) + (count - 1)) % (count - 1);
    else if (count == 1 && x[i] == '1')
      ans[i] = n;
    else if (x[i] == '0')
      ans[i] = (ans1 + modpow(2, i, count + 1)) % (count + 1);
  }
  vector<ll> ans12(n + 1, 0);
  rep(i, n) {
    int d = i;
    int num = 0;
    while (d) {
      d %= __builtin_popcount(d);
      num++;
    }
    ans12[i] = num;
  }
  ans12[n] = -1;

  rrep(i, n) cout << ans12[ans[i]] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i <= (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

ll modpow(ll a, ll n, ll p) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % p;
    a = a * a % p;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  reverse(all(x));

  vector<ll> ans(n, 0);

  ll count = 0;
  rep(i, n) if (x[i] == '1') count++;
  ll ans1 = 0;
  rep(i, n) if (x[i] == '1') ans1 += modpow(2, i, count + 1), ans1 %= count + 1;
  ll ans2 = 0;
  rep(i, n) if (x[i] == '1') ans2 += modpow(2, i, count), ans2 %= count;
  ll ans3 = 0;
  rep(i, n) if (x[i] == '1') ans3 += modpow(2, i, count - 1), ans3 %= count - 1;

  rep(i, n) {
    if (x[i] == '1')
      ans[i] = (ans3 - modpow(2, i, count - 1) + (count - 1)) % (count - 1);
    else
      ans[i] = (ans1 + modpow(2, i, count + 1) + (count + 1)) % (count + 1);
  }
  vector<ll> ans12(n, 0);
  rep(i, n) {
    int d = i;
    int num = 0;
    while (d) {
      d %= __builtin_popcount(d);
      num++;
    }
    ans12[i] = num;
  }

  rrep(i, n) cout << ans12[ans[i]] + 1 << endl;
  return 0;
}