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

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;
const int MOD = 1000000007;

const int max_v = 2 * 100000;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<int> x(N);
  rep(i, N) x[i] = X[i] - '0';
  int pop_count = 0;
  rep(i, N) pop_count += x[i];

  vector<int> ans(N);
  rep(b, 2) {
    int now_pop_count = pop_count;
    if (b == 0)
      now_pop_count++;
    else
      now_pop_count--;
    if (now_pop_count == 0)
      continue;
    int total = 0;
    rep(i, N) {
      total = (total * 2) % now_pop_count;
      total += x[i];
    }
    int now_val = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (x[i] == b) {
        int count = 0;
        int t = total;
        if (b == 0) {
          t = (t + now_val) % now_pop_count;
        } else {
          t = (t - now_val + now_pop_count) % now_pop_count;
        }
        count++;
        while (t > 0) {
          t = t % __builtin_popcount(t);
          count++;
        }
        ans[i] = count;
      }
      now_val = (now_val * 2) % now_pop_count;
    }
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;
const int MOD = 1000000007;

const int max_v = 3 * 100000;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<int> one_counter(max_v + 1, 0);
  for (int i = 1; i <= max_v; i++) {
    int t = i;
    while (t > 0) {
      if (t % 2 == 1) {
        one_counter[i]++;
      }
      t /= 2;
    }
  }

  ll x = 0;
  ll p_count = 0;
  rep(i, X.size()) {
    if (X[i] == '1') {
      x += (1 << (X.size() - i - 1));
      p_count++;
    }
  }

  rep(i, X.size()) {
    ll t = 0;
    ll t_count = p_count;
    if (X[i] == '1') {
      t = x - (1 << (X.size() - 1 - i));
      t_count--;
    } else {
      t = x + (1 << (X.size() - 1 - i));
      t_count++;
    }
    // cout << t << endl;
    // cout << "aa" << endl;
    // cout << one_counter[t] << endl;

    ll md = t % t_count;
    ll ans = 1;
    // cout << endl;
    // cout << md << endl;
    while (md > 0) {
      md = md % one_counter[md];
      // cout << md << endl;
      ans++;
    }
    cout << ans << endl;
  }
}