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

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

ll bin_pow(ll x, ll n, ll mod) {
  ll res = 1LL;
  for (; n > 0; n >>= 1, x = x * x % mod)
    if (n & 1)
      res = res * x % mod;
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  string x;
  cin >> n >> x;
  const int mx = 2002000;
  VI san(mx + 5);
  rep2(i, 1, mx + 1) {
    int p = 0;
    for (int w = i; w > 0;) {
      w = w % __builtin_popcount(w);
      p++;
    }
    san[i] = p;
  }
  int pcct = 0;
  rep(i, n) pcct += x[i] == '1';
  int pctp = pcct + 1, pctm = pcct - 1;
  ll smp = 0, smm = 0;
  rep(i, n) {
    if (pctm != 0)
      smm = (smm * 2 + (x[i] == '1')) % pctm;
    smp = (smp * 2 + (x[i] == '1')) % pctp;
  }
  // dbg("sm",smp, smm,"ct",pcct,pctp, pctm);
  rep(i, n) {
    ll cnd = 0;
    if (x[i] == '1') {
      if (pctm == 0) {
        cout << 0 << "\\n";
      } else {
        cnd = (smm + pctm - bin_pow(2, n - 1 - i, pctm)) % pctm;
        cout << san[cnd] + 1 << "\\n";
      }
    } else {
      cnd = (smp + pctp + bin_pow(2, n - 1 - i, pctp)) % pctp;
      cout << san[cnd] + 1 << "\\n";
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

ll bin_pow(ll x, ll n, ll mod) {
  ll res = 1LL;
  for (; n > 0; n >>= 1, x = x * x % mod)
    if (n & 1)
      res = res * x % mod;
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  string x;
  cin >> n >> x;
  const int mx = 2000000;
  VI san(mx + 1);
  rep2(i, 1, mx + 1) {
    int p = 0;
    for (int w = i; w > 0;) {
      w = w % __builtin_popcount(w);
      p++;
    }
    san[i] = p;
  }
  int pcct = 0;
  rep(i, n) pcct += x[i] == '1';
  int pctp = pcct + 1, pctm = pcct - 1;
  ll smp = 0, smm = 0;
  rep(i, n) {
    (smp *= 2) %= pctp;
    (smm *= 2) %= pctm;
    if (x[i] == '1') {
      (smp += 1) %= pctp;
      if (pctm >= 0)
        (smm += 1) %= pctm;
    }
  }
  // dbg("sm",smp, smm,"ct",pcct,pctp, pctm);
  rep(i, n) {
    ll cnd = 0;
    int ans = 1;
    if (x[i] == '1') {
      if (pctm == 0)
        cnd = 0;
      else {
        cnd = (smm + pctm - bin_pow(2, n - 1 - i, pctm)) % pctm;
        // dbg("m", i,smm,ws,n-1-i,pctm);
        // ans = ws;
      }
    } else {
      cnd = (smp + pctp + bin_pow(2, n - 1 - i, pctp)) % pctp;
      // dbg("p", i,smp,ws,n-1-i,pctp);
    }
    while (cnd > mx) {
      cnd = cnd % __builtin_popcountll(cnd);
      ans++;
    }

    cout << ans + san[cnd] << "\\n";
  }

  return 0;
}