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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

ll mpow(ll a, ll b, int mod) {
  a %= mod;
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  vi memo(300000, 0);
  srep(k, 1, 300000) {
    int x = k;
    int y = __builtin_popcountll(x);
    x %= y;
    memo[k] = memo[x] + 1;
  }
  int cnt = 0;
  rep(i, N) { cnt += s[i] == '1'; }
  ll Nmc = 0;
  rep(i, N) {
    Nmc <<= 1;
    Nmc += s[i] == '1';
    Nmc %= cnt;
  }
  ll Nmcd = 0;
  if (cnt > 1) {
    rep(i, N) {
      Nmcd <<= 1;
      Nmcd += s[i] == '1';
      Nmcd %= cnt - 1;
    }
  }
  ll Nmci = 0;
  rep(i, N) {
    Nmci <<= 1;
    Nmci += s[i] == '1';
    Nmci %= cnt + 1;
  }
  rep(i, N) {
    if (s[i] == '0') {
      ll k = Nmci + mpow(2, N - 1 - i, cnt + 1);
      k %= cnt + 1;
      cout << memo[k] + 1 << endl;
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      ll k = Nmcd - mpow(2, N - 1 - i, cnt - 1);
      k += cnt - 1;
      k %= cnt - 1;
      cout << memo[k] + 1 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

ll mpow(ll a, ll b, int mod) {
  a %= mod;
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  vi memo(300000, 0);
  srep(k, 1, 300000) {
    int x = k;
    int y = __builtin_popcountll(x);
    x %= y;
    memo[k] = memo[x] + 1;
  }
  int cnt = 0;
  rep(i, N) { cnt += s[i] == '1'; }
  ll Nmc = 0;
  rep(i, N) {
    Nmc <<= 1;
    Nmc += s[i] == '1';
    Nmc %= cnt;
  }
  ll Nmcd = 0;
  rep(i, N) {
    Nmcd <<= 1;
    Nmcd += s[i] == '1';
    Nmcd %= cnt - 1;
  }
  ll Nmci = 0;
  rep(i, N) {
    Nmci <<= 1;
    Nmci += s[i] == '1';
    Nmci %= cnt + 1;
  }
  rep(i, N) {
    if (s[i] == '0') {
      ll k = Nmci + mpow(2, N - 1 - i, cnt + 1);
      k %= cnt + 1;
      cout << memo[k] + 1 << endl;
    } else {
      ll k = Nmcd - mpow(2, N - 1 - i, cnt - 1);
      k += cnt - 1;
      k %= cnt - 1;
      cout << memo[k] + 1 << endl;
    }
  }
  return 0;
}