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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
template <class T = ll> inline T in(istream &is = cin) {
  T ret;
  is >> ret;
  return ret;
}
template <class T>
inline auto vec2(typename vector<T>::size_type h,
                 typename vector<T>::size_type w, T v = T()) {
  return vector<vector<T>>(h, vector<T>(w, v));
}
template <class RandomAccessIterator>
inline void psortb(RandomAccessIterator first, RandomAccessIterator last) {
  using P = pair<decltype(first->first), decltype(first->second)>;
  sort(first, last, [](const P &p1, const P &p2) {
    return p1.second < p2.second ||
           (p1.second == p2.second && p1.first < p2.first);
  });
}

vector<ll> f(300001, -1);
ll calc_f(ll n) {
  if (n == 0) {
    return 0;
  } else {
    if (auto foo = f[n % __builtin_popcountll(n)]; foo != -1) {
      f[n] = foo + 1;
    } else {
      f[n] = calc_f(n % __builtin_popcountll(n)) + 1;
    }
    return f[n];
  }
}
int main() {
  for (ll i = 300000; i >= 0; --i) {
    if (f[i] == -1) {
      f[i] = calc_f(i);
    }
  }

  ll n;
  string s;
  cin >> n >> s;
  ll popcount = count(s.begin(), s.end(), '1');

  vector<ll> mod_2_i_pm1(300001, 1), mod_2_i_pp1(300001, 1);
  if (popcount > 1)
    for (ll i = 1; i <= 300000; ++i)
      mod_2_i_pm1[i] = 2 * mod_2_i_pm1[i - 1] % (popcount - 1);
  for (ll i = 1; i <= 300000; ++i)
    mod_2_i_pp1[i] = 2 * mod_2_i_pp1[i - 1] % (popcount + 1);

  ll fxm = 0, fxp = 0;
  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (popcount != 1) {
        fxm += mod_2_i_pm1[n - 1 - i];
        fxm %= popcount - 1;
      }
      fxp += mod_2_i_pp1[n - 1 - i];
      fxp %= popcount + 1;
    }
  }

  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (popcount == 1)
        cout << 0 << endl;
      else
        cout << f[(popcount - 1 + fxm - mod_2_i_pm1[n - 1 - i]) %
                  (popcount - 1)] +
                    1
             << endl;
    } else {
      cout << f[(fxp + mod_2_i_pp1[n - 1 - i]) % (popcount + 1)] + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
template <class T = ll> inline T in(istream &is = cin) {
  T ret;
  is >> ret;
  return ret;
}
template <class T>
inline auto vec2(typename vector<T>::size_type h,
                 typename vector<T>::size_type w, T v = T()) {
  return vector<vector<T>>(h, vector<T>(w, v));
}
template <class RandomAccessIterator>
inline void psortb(RandomAccessIterator first, RandomAccessIterator last) {
  using P = pair<decltype(first->first), decltype(first->second)>;
  sort(first, last, [](const P &p1, const P &p2) {
    return p1.second < p2.second ||
           (p1.second == p2.second && p1.first < p2.first);
  });
}

vector<ll> f(300001, -1);
ll calc_f(ll n) {
  if (n == 0) {
    return 0;
  } else {
    if (auto foo = f[n % __builtin_popcountll(n)]; foo != -1) {
      f[n] = foo + 1;
    } else {
      f[n] = calc_f(n % __builtin_popcountll(n)) + 1;
    }
    return f[n];
  }
}
int main() {
  for (ll i = 300000; i >= 0; --i) {
    if (f[i] == -1) {
      f[i] = calc_f(i);
    }
  }

  ll n;
  string s;
  cin >> n >> s;
  ll popcount = count(s.begin(), s.end(), '1');

  vector<ll> mod_2_i_pm1(300001, 1), mod_2_i_pp1(300001, 1);
  if (popcount > 1)
    for (ll i = 1; i <= 300000; ++i)
      mod_2_i_pm1[i] = 2 * mod_2_i_pm1[i - 1] % (popcount - 1);
  for (ll i = 1; i <= 300000; ++i)
    mod_2_i_pp1[i] = 2 * mod_2_i_pp1[i - 1] % (popcount + 1);

  ll fxm = 0, fxp = 0;
  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1') {
      fxm += mod_2_i_pm1[n - 1 - i];
      if (popcount != 1)
        fxm %= popcount - 1;
      fxp += mod_2_i_pp1[n - 1 - i];
      fxp %= popcount + 1;
    }
  }

  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1') {
      cout
          << f[(popcount - 1 + fxm - mod_2_i_pm1[n - 1 - i]) % (popcount - 1)] +
                 1
          << endl;
    } else {
      cout << f[(fxp + mod_2_i_pp1[n - 1 - i]) % (popcount + 1)] + 1 << endl;
    }
  }
}