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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; // int:2*10**9
typedef long double ld;
typedef pair<ll, ll> P;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define vec2(name, i, j, k) vector<vector<ll>> name(i, vector<ll>(j, k))
#define vec3(name, i, j, k, l)                                                 \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k, l)))
#define pb push_back
#define MOD 1000000007 // 998244353
#define PI 3.141592653
#define INF 100000000000000 // 14
// cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
vector<ll> pl(210000, 0);
ll pcnt(ll x) {
  ll y = 0;
  while (x > 0) {
    y += x % 2;
    x /= 2;
  }
  return y;
}

void makepl() {
  FOR(i, 1, 209999) {
    ll x = i % (pcnt(i));
    pl[i] = pl[x] + 1;
  }
}

int main() {
  makepl();
  ll n;
  cin >> n;
  string s;
  cin >> s;
  if (s == "0") {
    cout << 1 << endl;
    return 0;
  }
  if (s == "1") {
    cout << 0 << endl;
    return 0;
  }
  ll cnter = 0;
  REP(i, n) if (s[i] == '1') cnter++;
  if (cnter == 0) {
    REP(i, n) cout << 1 << endl;
    return 0;
  }
  ll m1 = cnter - 1;
  ll m2 = cnter + 1;
  vector<ll> d1(n, 1);
  vector<ll> d2(n, 1);
  FOR(i, 1, n - 1) {
    if (m1 != 0)
      d1[i] = (d1[i - 1] * 2) % m1;
    d2[i] = (d2[i - 1] * 2) % m2;
  }
  ll t1, t2;
  if (s[n - 1] != '1')
    t1 = t2 = 0;
  else
    t1 = t2 = 1;
  FOR(i, 1, n - 1) {
    if (s[n - 1 - i] == '1') {
      if (m1 != 0)
        t1 = (t1 + d1[i]) % m1;
      t2 = (t2 + d2[i]) % m2;
    }
  }
  if (m1 == 0) {
    FOR(i, 0, n - 1) {
      ll x;
      if (s[i] == '1') {
        x = -1;
      } else {
        // cout << "t2 " << t2 << " " << d2[n-1-i] << " " << m2 << endl;
        x = (t2 + d2[n - 1 - i]) % m2;
      }
      cout << x + 1 << endl;
    }
    return 0;
  }
  FOR(i, 0, n - 1) {
    ll x;
    if (s[i] == '1') {
      // cout << "t1 " << t1 << " " << d1[n-1-i] << " " << m1 << endl;
      x = (t1 + m1 - d1[n - 1 - i]) % m1;
    } else {
      // cout << "t2 " << t2 << " " << d2[n-1-i] << " " << m2 << endl;
      x = (t2 + d2[n - 1 - i]) % m2;
    }

    // cout << x << endl;
    cout << pl[x] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; // int:2*10**9
typedef long double ld;
typedef pair<ll, ll> P;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define vec2(name, i, j, k) vector<vector<ll>> name(i, vector<ll>(j, k))
#define vec3(name, i, j, k, l)                                                 \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k, l)))
#define pb push_back
#define MOD 1000000007 // 998244353
#define PI 3.141592653
#define INF 100000000000000 // 14
// cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
vector<ll> pl(210000, 0);
ll pcnt(ll x) {
  ll y = 0;
  while (x > 0) {
    y += x % 2;
    x /= 2;
  }
  return y;
}

void makepl() {
  FOR(i, 1, 209999) {
    ll x = i % (pcnt(i));
    pl[i] = pl[x] + 1;
  }
}

int main() {
  makepl();
  ll n;
  string s;
  cin >> n >> s;
  ll cnter = 0;
  REP(i, n) if (s[i] == '1') cnter++;
  ll m1 = cnter - 1;
  ll m2 = cnter + 1;
  vector<ll> d1(n, 1);
  vector<ll> d2(n, 1);
  FOR(i, 1, n - 1) {
    d1[i] = (d1[i - 1] * 2) % m1;
    d2[i] = (d2[i - 1] * 2) % m2;
  }
  ll t1, t2;
  if (s[n - 1] != '1')
    t1 = t2 = 0;
  else
    t1 = t2 = 1;
  FOR(i, 1, n - 1) {
    if (s[n - 1 - i] == '1') {
      t1 = (t1 + d1[i]) % m1;
      t2 = (t2 + d2[i]) % m2;
    }
  }
  FOR(i, 0, n - 1) {
    ll x;
    if (s[i] == '1') {
      // cout << "t1 " << t1 << " " << d1[n-1-i] << " " << m1 << endl;
      x = (t1 + m1 - d1[n - 1 - i]) % m1;
    } else {
      // cout << "t2 " << t2 << " " << d2[n-1-i] << " " << m2 << endl;
      x = (t2 + d2[n - 1 - i]) % m2;
    }

    // cout << x << endl;
    cout << pl[x] + 1 << endl;
  }
}