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

#include <bits/stdc++.h>
using namespace std;
// T+N
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod | x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const ll MAX_N = 1e6 + 10;
ll ans[MAX_N];
ll pw[MAX_N][3], hsh[3];

ll popcount(ll x) {
  ll ret = 0;
  while (x) {
    ret += x & 1;
    x /= 2;
  }
  return ret;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ans[0] = 0;
  for (ll i = 1; i < MAX_N; i++) {
    ll to = i % popcount(i);
    ans[i] = ans[to] + 1;
  }
  ll n;
  string in;
  cin >> n;
  cin >> in;
  reverse(in.begin(), in.end());
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (in[i] == '1') {
      cnt++;
    }
  }
  pw[0][0] = pw[0][1] = pw[0][2] = 1;
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < 3; j++) {
      ll nowMod = cnt - 1 + j;
      if (nowMod == 0) {
        continue;
      }
      pw[i][j] = (pw[i - 1][j] * 2) % nowMod;
    }
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++) {
      ll nowMod = cnt - 1 + j;
      if (nowMod == 0) {
        continue;
      }
      if (in[i] == '1') {
        hsh[j] = (hsh[j] + pw[i][j]) % nowMod;
      }
    }
  }
  for (ll i = n - 1; i >= 0; i--) {
    if (in[i] == '0') {
      ll nowMod = cnt + 1;
      ll now = hsh[2];
      now = (now + pw[i][2]) % nowMod;
      cout << ans[now] + 1 << endl;
    } else {
      ll nowMod = cnt - 1;
      if (nowMod == 0) {
        cout << 0 << endl;
        continue;
      }
      ll now = hsh[0];
      now = (now - pw[i][0] + nowMod) % nowMod;
      cout << ans[now] + 1 << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// T+N
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod | x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const ll MAX_N = 1e6 + 10;
ll ans[MAX_N];
ll pw[MAX_N][3], hsh[3];

ll popcount(ll x) {
  ll ret = 0;
  while (x) {
    ret += x & 1;
    x /= 2;
  }
  return ret;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ans[0] = 0;
  for (ll i = 1; i < MAX_N; i++) {
    ll to = i % popcount(i);
    ans[i] = ans[to] + 1;
  }
  ll n;
  string in;
  cin >> n;
  cin >> in;
  reverse(in.begin(), in.end());
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (in[i] == '1') {
      cnt++;
    }
  }
  pw[0][0] = pw[0][1] = pw[0][2] = 1;
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < 3; j++)
      if (j != 1) {
        ll nowMod = cnt - 1 + j;
        pw[i][j] = (pw[i - 1][j] * 2) % nowMod;
      }
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++)
      if (j != 1) {
        ll nowMod = cnt - 1 + j;
        if (in[i] == '1') {
          hsh[j] = (hsh[j] + pw[i][j]) % nowMod;
        }
      }
  }
  for (ll i = n - 1; i >= 0; i--) {
    if (in[i] == '0') {
      ll nowMod = cnt + 1;
      ll now = hsh[2];
      now = (now + pw[i][2]) % nowMod;
      cout << ans[now] + 1 << endl;
    } else {
      ll nowMod = cnt - 1;
      ll now = hsh[0];
      now = (now - pw[i][0] + nowMod) % nowMod;
      cout << ans[now] + 1 << endl;
    }
  }
  return 0;
}