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

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

#define INF ((i64)(1e18))
#define MOD ((i64)(1e9 + 7))
#define REP(i, n) for (i64 i = 0; i < (n); i++)
#define RREP(i, n) for (i64 i = (n)-1; i >= 0; i--)
#define RANGE(i, a, b) for (i64 i = (a); i < (b); i++)
#define RRANGE(i, a, b) for (i64 i = (b)-1; i >= (a); i--)
#define ALL(v) (v).begin(), (v).end()
#define SIZE(v) ((i64)(v).size())
template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

int main() {
  i64 n;
  cin >> n;
  string x;
  cin >> x;
  reverse(ALL(x));
  i64 c = 0;
  REP(i, n) if (x[i] == '1') c++;
  vector<i64> mod0(n, 0), mod1(n, 0);
  i64 c0 = c + 1, c1 = c - 1;
  i64 tmp0 = 1, tmp1 = 1;
  REP(i, n) {
    if (c0 > 0) {
      mod0[i] = tmp0 % c0;
      tmp0 = (tmp0 * 2) % c0;
    }
    if (c1 > 0) {
      mod1[i] = tmp1 % c1;
      tmp1 = (tmp1 * 2) % c1;
    }
  }

  i64 origin0 = 0, origin1 = 0;
  REP(i, n) {
    if (x[i] == '1') {
      if (c0 > 0)
        origin0 = (origin0 + mod0[i]) % c0;
      if (c1 > 0)
        origin1 = (origin1 + mod1[i]) % c1;
    }
  }

  vector<i64> ppc(n);
  ppc[0] = 0;
  RANGE(i, 1, n) {
    i64 tmp = i, keta = 0;
    while (tmp > 0) {
      if (tmp & 1)
        keta++;
      tmp /= 2;
    }
    ppc[i] = ppc[i % keta] + 1;
  }

  vector<i64> ans(n);
  RREP(i, n) {
    if (x[i] == '1' && c1 == 0) {
      cout << 0 << endl;
      continue;
    }
    i64 value = 0;
    if (x[i] == '0' && c0 > 0) {
      value = (origin0 + mod0[i]) % c0;
    } else if (x[i] == '1' && c1 > 0) {
      value = (origin1 + c1 - mod1[i]) % c1;
    }
    cout << ppc[value] + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define INF ((i64)(1e18))
#define MOD ((i64)(1e9 + 7))
#define REP(i, n) for (i64 i = 0; i < (n); i++)
#define RREP(i, n) for (i64 i = (n)-1; i >= 0; i--)
#define RANGE(i, a, b) for (i64 i = (a); i < (b); i++)
#define RRANGE(i, a, b) for (i64 i = (b)-1; i >= (a); i--)
#define ALL(v) (v).begin(), (v).end()
#define SIZE(v) ((i64)(v).size())
template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

int main() {
  i64 n;
  cin >> n;
  string x;
  cin >> x;
  reverse(ALL(x));
  i64 c = 0;
  REP(i, n) if (x[i] == '1') c++;
  vector<i64> mod0(n, 0), mod1(n, 0);
  i64 c0 = c + 1, c1 = c - 1;
  i64 tmp0 = 1, tmp1 = 1;
  REP(i, n) {
    if (c0 > 0) {
      mod0[i] = tmp0 % c0;
      tmp0 = (tmp0 * 2) % c0;
    }
    if (c1 > 0) {
      mod1[i] = tmp1 % c1;
      tmp1 = (tmp1 * 2) % c1;
    }
  }

  i64 origin0 = 0, origin1 = 0;
  REP(i, n) {
    if (x[i] == '1') {
      origin0 = (origin0 + mod0[i]) % c0;
      origin1 = (origin1 + mod1[i]) % c1;
    }
  }

  vector<i64> ppc(n);
  ppc[0] = 0;
  RANGE(i, 1, n) {
    i64 tmp = i, keta = 0;
    while (tmp > 0) {
      if (tmp & 1)
        keta++;
      tmp /= 2;
    }
    ppc[i] = ppc[i % keta] + 1;
  }

  vector<i64> ans(n);
  RREP(i, n) {
    i64 value = 0;
    if (x[i] == '0' && c0 > 0) {
      value = (origin0 + mod0[i]) % c0;
    } else if (x[i] == '1' && c1 > 0) {
      value = (origin1 + c1 - mod1[i]) % c1;
    }
    cout << ppc[value] + 1 << endl;
  }

  return 0;
}