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

#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
ll mod = 1000000007;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;

  reverse(s.begin(), s.end());

  ll count = 0, now_count;

  rep(i, n) {
    if (s[i] == '1')
      count++;
  }

  vector<ll> aaa(n + 1, 0);

  ll now = 1;
  now_count = count + 1;
  rep(i, n) {
    if (s[i] == '0')
      aaa[i] = now;
    now = now * 2 % now_count;
  }

  now = 1;
  now_count = count - 1;
  if (now_count != 0) {
    rep(i, n) {
      if (s[i] == '1')
        aaa[i] = now;
      now = now * 2 % now_count;
    }
  }

  ll purasu = 0, mainasu = 0;

  now = 1;
  now_count = count + 1;
  rep(i, n) {
    if (s[i] == '1')
      purasu += now;
    purasu = purasu % now_count;
    now = now * 2 % now_count;
  }

  now = 1;
  now_count = count - 1;
  if (count - 1 != 0) {
    rep(i, n) {
      if (s[i] == '1')
        mainasu += now;
      mainasu = mainasu % now_count;
      now = now * 2 % now_count;
    }
  } else {
    mainasu = 0;
  }

  for (ll i = n - 1; i >= 0; i--) {
    ll nownow;
    if (s[i] == '1') {
      if (count - 1 == 0) {
        cout << '0' << endl;
        continue;
      }
      nownow = (mainasu - aaa[i] + count - 1) % (count - 1);
    } else {
      nownow = (purasu + aaa[i] + count + 1) % (count + 1);
    }

    ll ans = 1;
    while (nownow) {
      ll abc = nownow;
      ll c = 0;
      while (abc) {
        if (abc % 2 == 1)
          c++;
        abc /= 2;
      }
      nownow = nownow % c;
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
ll mod = 1000000007;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

struct mint {
  ll x;
  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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

// 組み合わせ計算
mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i, a) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

// 繰り返し二乗法
mint f(int n) {
  if (n == 0)
    return 1;
  mint x = f(n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= 2;
  return x;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;

  reverse(s.begin(), s.end());

  ll count = 0, now_count;

  rep(i, n) {
    if (s[i] == '1')
      count++;
  }

  vector<ll> aaa(n + 1, 0);

  ll now = 1;
  now_count = count + 1;
  rep(i, n) {
    if (s[i] == '0')
      aaa[i] = now;
    now = now * 2 % now_count;
  }

  now = 1;
  now_count = count - 1;
  if (now_count != 0) {
    rep(i, n) {
      if (s[i] == '1')
        aaa[i] = now;
      now = now * 2 % now_count;
    }
  }

  ll purasu = 0, mainasu = 0;

  now = 1;
  now_count = count + 1;
  rep(i, n) {
    if (s[i] == '1')
      purasu += now;
    purasu = purasu % now_count;
    now = now * 2 % now_count;
  }

  now = 1;
  now_count = count - 1;
  rep(i, n) {
    if (s[i] == '1')
      mainasu += now;
    mainasu = mainasu % now_count;
    now = now * 2 % now_count;
  }

  for (ll i = n - 1; i >= 0; i--) {
    ll nownow;
    if (s[i] == '1') {
      if (count - 1 == 0) {
        cout << '0' << endl;
        continue;
      }
      nownow = (mainasu - aaa[i] + count - 1) % (count - 1);
    } else {
      nownow = (purasu + aaa[i] + count + 1) % (count + 1);
    }

    ll ans = 1;
    while (nownow) {
      ll abc = nownow;
      ll c = 0;
      while (abc) {
        if (abc % 2 == 1)
          c++;
        abc /= 2;
      }
      nownow = nownow % c;
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}