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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int coun(ll n) {
  int res = 0;
  while (n > 0) {
    res = res + n % 2;
    n /= 2;
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  vector<ll> kai(n + 1);
  ll pc = 0;
  vector<ll> mop(n + 1), mom(n + 1);
  for (int i = 0; i < n; i++) {
    if (x.at(i) == '1') {
      pc++;
    }
  }
  if (pc == 0 && n != 1) {
    for (int i = 0; i < n; i++) {
      cout << 0 << endl;
    }
    return 0;
  } else if (pc == 0 && n == 1) {
    cout << 1 << endl;
    return 0;
  }
  mop[0] = 1;
  if (pc != 1)
    mom[0] = 1;
  else
    mom[0] = 0;
  for (int i = 1; i < n; i++) {
    mop.at(i) = (mop.at(i - 1) * 2) % (pc + 1);
    if (pc != 1)
      mom.at(i) = (mom.at(i - 1) * 2) % (pc - 1);
    else
      mom.at(i) = 0;
  }
  kai.at(1) = 1;
  for (int i = 1; i <= n; i++) {
    int tmp = coun(i);
    int tt = i % tmp;
    if (tt == 0)
      kai.at(i) = 1;
    else
      kai.at(i) = kai.at(tt) + 1;
  }
  ll xp = 0;
  ll xm = 0;
  for (int i = 0; i < n; i++) {
    if (x.at(n - i - 1) == '1') {
      xp += mop.at(i);
      xp %= (pc + 1);
      xm += mom.at(i);
      if (pc != 1)
        xm %= (pc - 1);
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (x.at(n - i - 1) == '1') {
      ll tmod = pc - 1;
      ll tmp = xm - mom.at(i);
      if (tmp < 0)
        tmp += pc - 1;
      if (pc != 1)
        tmp %= tmod;
      if (tmod != 0) {
        cout << (tmp == 0 || tmod == 0 ? 1 : kai.at(tmp) + 1) << endl;
      } else
        cout << 0 << endl;
    } else {
      ll tmod = pc + 1;
      ll tmp = xp + mop.at(i);
      tmp %= tmod;
      cout << (tmp == 0 ? 1 : kai.at(tmp) + 1) << endl;
    }
  }
  //    cout << fixed << setprecision(10);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int coun(ll n) {
  int res = 0;
  while (n > 0) {
    res = res + n % 2;
    n /= 2;
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  vector<ll> kai(n + 1);
  ll pc = 0;
  vector<ll> mop(n), mom(n);
  for (int i = 0; i < n; i++) {
    if (x.at(i) == '1') {
      pc++;
    }
  }
  if (pc == 0 || pc == n) {
    for (int i = 0; i < n; i++) {
      cout << 0 << endl;
    }
    return 0;
  }
  mop[0] = 1;
  if (pc != 1)
    mom[0] = 1;
  else
    mom[0] = 0;
  for (int i = 1; i < n; i++) {
    mop.at(i) = (mop.at(i - 1) * 2) % (pc + 1);
    if (pc != 1)
      mom.at(i) = (mom.at(i - 1) * 2) % (pc - 1);
    else
      mom.at(i) = 0;
  }
  kai.at(1) = 1;
  for (int i = 1; i <= n; i++) {
    int tmp = coun(i);
    int tt = i % tmp;
    if (tt == 0)
      kai.at(i) = 1;
    else
      kai.at(i) = kai.at(tt) + 1;
  }
  ll xp = 0;
  ll xm = 0;
  for (int i = 0; i < n; i++) {
    if (x.at(n - i - 1) == '1') {
      xp += mop.at(i);
      xp %= (pc + 1);
      xm += mom.at(i);
      xm %= (pc - 1);
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (x.at(n - i - 1) == '1') {
      ll tmod = pc - 1;
      ll tmp = xm - mom.at(i);
      if (tmp < 0)
        tmp += pc - 1;
      tmp %= tmod;
      cout << (tmp == 0 ? 1 : kai.at(tmp) + 1) << endl;
    } else {
      ll tmod = pc + 1;
      ll tmp = xp + mop.at(i);
      tmp %= tmod;
      cout << (tmp == 0 ? 1 : kai.at(tmp) + 1) << endl;
    }
  }
  //    cout << fixed << setprecision(10);
}