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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)

template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// vector出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}" << endl;
  return o;
}

// 逆元　a/b の modをとるとき -> a*modinv(b)%mod
long long modinv(long long a, long long m = 1e9 + 7) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll const mod = 1e9 + 7;

// combination
const int MAX = 510000;
const ll MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int N;
  ll n;
  cin >> N;
  vector<ll> m(2 * 1e5 + 1, 0);
  rep2(i, 1, 2 * 1e5 + 1) {
    n = __builtin_popcount(i);
    m[i] = m[i % n] + 1;
    // cout << m[i] << endl;
  }
  string X;
  cin >> X;
  n = 0;
  for (auto i : X) {
    if (i == '1')
      n++;
  }

  vector<ll> ans(N, 0), P(N), P1(N);
  ll x = 0, x1 = 0, p = 1, p1 = 1;
  reverse(X.begin(), X.end());
  rep(i, N) {
    if (i > 0) {
      p *= 2;
      p1 *= 2;
    }

    p %= n + 1;
    if (n - 1 > 0)
      p1 %= n - 1;

    P[i] = p;
    P1[i] = p1;
    if (X[i] == '1') {
      x += p;   // if (x>n+1) x%=n+1;
      x1 += p1; // if (x1>n-1&&n-1>0) x1%=n-1;
    }
  }

  rep(i, N) {
    ll t;
    if (X[i] == '0') {
      t = (x + P[i]);
      if (t > n + 1)
        t %= n + 1;
      ans[i] = m[t] + 1;
    } else {
      t = x1 - P1[i];
      if (n - 1 == 0)
        ans[i] = 0;
      else {
        if (t > n - 1)
          t %= n - 1;
        ans[i] = m[t] + 1;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, N) cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)

template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// vector出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}" << endl;
  return o;
}

// 逆元　a/b の modをとるとき -> a*modinv(b)%mod
long long modinv(long long a, long long m = 1e9 + 7) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll const mod = 1e9 + 7;

// combination
const int MAX = 510000;
const ll MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int N;
  ll n;
  cin >> N;
  vector<ll> m(2 * 1e5 + 1, 0);
  rep2(i, 1, 2 * 1e5 + 1) {
    n = __builtin_popcount(i);
    m[i] = m[i % n] + 1;
    // cout << m[i] << endl;
  }
  string X;
  cin >> X;
  n = 0;
  for (auto i : X) {
    if (i == '1')
      n++;
  }

  vector<ll> ans(N, 0), P(N), P1(N);
  ll x = 0, x1 = 0, p = 1, p1 = 1;
  reverse(X.begin(), X.end());
  rep(i, N) {
    if (i > 0) {
      p *= 2;
      p1 *= 2;
    }
    // cout << p << ' ' << p1 << endl;
    p %= n + 1;
    p1 %= n - 1;
    // cout << p << ' ' << p1 << endl;
    P[i] = p;
    P1[i] = p1;
    if (X[i] == '1') {
      x += p;
      x1 += p1;
    }
  }

  rep(i, N) {
    ll t;
    if (X[i] == '0') {
      t = (x + P[i]);
      if (t > n + 1)
        t %= n + 1;
      ans[i] = m[t] + 1;
    } else {
      t = x1 - P1[i];
      if (t > n - 1)
        t %= n - 1;
      ans[i] = m[t] + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, N) cout << ans[i] << endl;
}