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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

vector<ll> ans(2 * 1e5 + 10, 0);

ll bin_pow(ll a, ll n, ll mod) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ans % mod;
}

ll f(ll i) {
  ll cnt = 0;
  while (i != 0) {
    i %= __builtin_popcount(i);
    cnt++;
  }
  return cnt;
}

int main() {
  for (int i = 0; i < ans.size() - 5; ++i) {
    ans[i] = f(i);
  }

  int n;
  cin >> n;
  string x;
  cin >> x;
  if (n == 1 && x == "1") {
    cout << 0 << endl;
    return 0;
  }

  ll pop = 0;
  for (int i = 0; i < x.size(); ++i) {
    if (x[i] == '1')
      pop++;
  }
  ll pop_plus = pop + 1;
  ll pop_minus = pop - 1;
  bool judge = false;
  if (pop_minus == 0) {
    pop_minus = 1;
    judge = true;
  }
  string rev_x = x;
  reverse(rev_x.begin(), rev_x.end());
  ll plus = 0, minus = 0;
  for (ll i = 0; i < rev_x.size(); ++i) {
    if (rev_x[i] == '1') {
      plus += bin_pow(2, i, pop_plus);
      minus += bin_pow(2, i, pop_minus);
      plus %= pop_plus;
      minus %= pop_minus;
    }
  }

  for (int i = 0; i < x.size(); ++i) {
    if (x[i] == '1') {
      // cout<<"i: "<<i<<endl;
      // cout<<minus<<" "
      // <<pop_minus<<" "
      // <<bin_pow(2,n-1-i,pop_minus)<<" "<<endl;
      int idx = (minus + 2 * pop_minus - bin_pow(2, n - 1 - i, pop_minus)) %
                pop_minus;
      if (judge)
        cout << 0 << endl;
      else
        cout << 1 + ans[idx] << endl;
    } else {
      // cout<<"i: "<<i<<endl;
      // cout<<plus<<" "
      // <<pop_plus<<" "
      // <<bin_pow(2,n-1-i,pop_plus)<<" "<<endl;
      int idx =
          (plus + 2 * pop_plus + bin_pow(2, n - 1 - i, pop_plus)) % pop_plus;
      cout << 1 + ans[idx] << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

vector<ll> ans(2 * 1e5 + 10, 0);

ll bin_pow(ll a, ll n, ll mod) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ans % mod;
}

ll f(ll i) {
  ll cnt = 0;
  while (i != 0) {
    i %= __builtin_popcount(i);
    cnt++;
  }
  return cnt;
}

int main() {
  for (int i = 0; i < ans.size() - 5; ++i) {
    ans[i] = f(i);
  }

  int n;
  cin >> n;
  string x;
  cin >> x;
  if (n == 1 && x == "1") {
    cout << 0 << endl;
    return 0;
  }

  ll pop = 0;
  for (int i = 0; i < x.size(); ++i) {
    if (x[i] == '1')
      pop++;
  }
  ll pop_plus = pop + 1;
  ll pop_minus = pop - 1;
  string rev_x = x;
  reverse(rev_x.begin(), rev_x.end());
  ll plus = 0, minus = 0;
  for (ll i = 0; i < rev_x.size(); ++i) {
    if (rev_x[i] == '1') {
      plus += bin_pow(2, i, pop_plus);
      minus += bin_pow(2, i, pop_minus);
      plus %= pop_plus;
      minus %= pop_minus;
    }
  }

  for (int i = 0; i < x.size(); ++i) {
    if (x[i] == '1') {
      // cout<<"i: "<<i<<endl;
      // cout<<minus<<" "
      // <<pop_minus<<" "
      // <<bin_pow(2,n-1-i,pop_minus)<<" "<<endl;
      cout << 1 + ans.at((minus + 2 * pop_minus -
                          bin_pow(2, n - 1 - i, pop_minus)) %
                         pop_minus)
           << endl;
    } else {
      // cout<<"i: "<<i<<endl;
      // cout<<plus<<" "
      // <<pop_plus<<" "
      // <<bin_pow(2,n-1-i,pop_plus)<<" "<<endl;
      cout << 1 + ans.at(
                      (plus + 2 * pop_plus + bin_pow(2, n - 1 - i, pop_plus)) %
                      pop_plus)
           << endl;
    }
  }
}