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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ocut cout
#define ouct cout
#define itn int

struct Union {
  vector<ll> par;
  Union(ll a) { par = vector<ll>(a, -1); }
  ll find(ll a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(ll a, ll b) { return (find(a) == find(b)); }
  ll size(ll a) { return -par[find(a)]; }
  void unite(ll a, ll b) {
    ll c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
ll euclidean_gcd(ll a, ll b) {
  if (a < b)
    return euclidean_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll euclidean_lcm(ll a, ll b) { return a / euclidean_gcd(a, b) * b; }

ll binary(ll bina) {
  ll ans = 0;
  for (ll i = 0; bina > 0; i++) {
    ans += bina % 2;
    bina = bina / 2;
  }
  return ans;
}
void revStr(char *str) {
  int size = strlen(str);
  int i, j;
  char tmp = {0};

  for (i = 0, j = size - 1; i < size / 2; i++, j--) {
    tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
  }
  return;
}
int main(void) {
  ll n, z = 0;
  cin >> n;
  char s[n + 1];
  cin >> s;
  revStr(s);
  rep(i, n) {
    if (s[i] == '1') {
      z++;
    }
  }
  ll g = z - 1, h = z + 1;
  ll dp[250001];
  ll w[250001], e[250001];
  ll t = 1, u = 1;
  rep(i, 250001) {
    w[i] = t;
    e[i] = u;
    if (g > 0)
      t = (t * 2) % g;
    if (h > 0)
      u = (u * 2) % h;
  }
  ll ww = 0, ee = 0;
  rep(i, n) {
    if (s[i] == '1') {
      ww += w[i];
      if (g > 0)
        ww %= g;
      ee += e[i];
      if (h > 0)
        ee %= h;
    }
  }
  dp[0] = 0;
  dp[1] = 1;
  rep(i, 250001) {
    if (i > 1) {
      dp[i] = dp[i % binary(i)] + 1;
    }
  }
  ll jj, kk;
  rep(i, n) {
    if (s[n - 1 - i] == '1') {
      if (g == 0)
        cout << 0 << endl;
      else {
        jj = (ww + g - w[n - 1 - i]) % g;
        cout << 1 + dp[jj] << endl;
      }
    } else {
      if (h == 0)
        cout << 0 << endl;
      else {
        kk = (ee + h + e[n - 1 - i]) % h;
        cout << 1 + dp[kk] << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ocut cout
#define ouct cout
#define itn int

struct Union {
  vector<ll> par;
  Union(ll a) { par = vector<ll>(a, -1); }
  ll find(ll a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(ll a, ll b) { return (find(a) == find(b)); }
  ll size(ll a) { return -par[find(a)]; }
  void unite(ll a, ll b) {
    ll c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
ll euclidean_gcd(ll a, ll b) {
  if (a < b)
    return euclidean_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll euclidean_lcm(ll a, ll b) { return a / euclidean_gcd(a, b) * b; }

ll binary(ll bina) {
  ll ans = 0;
  for (ll i = 0; bina > 0; i++) {
    ans += bina % 2;
    bina = bina / 2;
  }
  return ans;
}
void revStr(char *str) {
  int size = strlen(str);
  int i, j;
  char tmp = {0};

  for (i = 0, j = size - 1; i < size / 2; i++, j--) {
    tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
  }
  return;
}
int main(void) {
  ll n, z = 0;
  cin >> n;
  char s[n + 1];
  cin >> s;
  revStr(s);
  rep(i, n) {
    if (s[i] == '1') {
      z++;
    }
  }
  ll g = z - 1, h = z + 1;
  ll dp[250001];
  ll w[250001], e[250001];
  ll t = 1, u = 1;
  rep(i, 250001) {
    w[i] = t;
    e[i] = u;
    t = (t * 2) % g;
    u = (u * 2) % h;
  }
  ll ww = 0, ee = 0;
  rep(i, n) {
    if (s[i] == '1') {
      ww += w[i];
      ww %= g;
      ee += e[i];
      ee %= h;
    }
  }
  dp[0] = 0;
  dp[1] = 1;
  rep(i, 250001) {
    if (i > 1) {
      dp[i] = dp[i % binary(i)] + 1;
    }
  }
  ll jj, kk;
  rep(i, n) {
    if (s[n - 1 - i] == '1') {
      jj = (ww + g - w[n - 1 - i]) % g;
      cout << 1 + dp[jj] << endl;
    } else {
      kk = (ee + h + e[n - 1 - i]) % h;
      cout << 1 + dp[kk] << endl;
    }
  }
}