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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

ll cnt[200005];

int main() {
  INIT;

  ll N;
  string str;
  cin >> N >> str;

  reps(i, 1, 200005) {
    int pp = __builtin_popcount(i);
    cnt[i] = cnt[i % pp] + 1;
  }

  reverse(ALL(str));

  ll ps = 0;
  rep(i, N) {
    if (str[i] == '1')
      ps++;
  }

  ll tot1 = 0, p1 = 1;
  ll tot2 = 0, p2 = 1;

  rep(i, N) {
    if (str[i] == '1') {
      tot1 = (tot1 + p1) % (ps + 1);
      if (ps > 1) {
        tot2 = (tot2 + p2) % (ps - 1);
      }
    }

    p1 = (p1 * 2) % (ps + 1);
    if (ps > 1) {
      p2 = (p2 * 2) % (ps - 1);
    }
  }

  vector<ll> ans;
  p1 = 1;
  p2 = 1;

  rep(i, N) {
    if (str[i] == '1') {
      if (ps == 1) {
        ans.pb(0);
      } else {
        ll x = (tot2 - p2 + ps - 1) % (ps - 1);
        ans.pb(cnt[x] + 1);
      }
    } else {
      ll x = (tot1 + p1 + ps + 1) % (ps + 1);
      ans.pb(cnt[x] + 1);
    }

    p1 = (p1 * 2) % (ps + 1);
    if (ps > 1) {
      p2 = (p2 * 2) % (ps - 1);
    }
  }

  reverse(ALL(ans));
  rep(i, N) { cout << ans[i] << endl; }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }

  mint operator+(const mint a) const { return mint(*this) += a; }

  mint operator-(const mint a) const { return mint(*this) -= a; }

  mint operator*(const mint a) const { return mint(*this) *= a; }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

ll f(mint s, ll cnt, ll ans, ll N) {
  // cout << "(" << s << ", " << cnt << ")" << endl;
  mint res = s - floor(s.x / cnt) * cnt;
  // cout << "res.x:" << res.x << endl;
  // ll new_cnt = 0;
  ans++;

  if (res.x == 0) {
    return ans;
  } else {
    ll new_cnt = __builtin_popcount(res.x);
    return f(res, new_cnt, ans, N);
  }
}

int main() {
  INIT;

  ll N;
  string str;
  cin >> N >> str;

  ll cnt = 0;

  mint S = 0;
  repr(i, N - 1) {
    if (str[i] == '1') {
      cnt++;
      S += mint(2).pow(N - 1 - i);
    }
  }

  rep(i, N) {
    ll now_cnt = cnt;
    mint now_S = S;
    if (str[i] == '0') {
      now_cnt++;
      now_S += mint(2).pow(N - 1 - i);
    } else {
      now_cnt--;
      now_S -= mint(2).pow(N - 1 - i);
    }

    cout << f(now_S, now_cnt, 0, N) << endl;
  }

  return 0;
}