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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \\
  cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll) x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll, ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri {
  template <class T> bool operator()(T const &a, T const &b) const {
    return a > b;
  }
};
void setIO(string s) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
ll pwrTwo[800001], pwrTwo1[800001];
ll getMod(string str, ll n, ll k) {
  pwrTwo[0] = 1 % k;
  for (ll i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }
  ll res = 0;
  ll i = 0, j = n - 1;
  while (i < n) {
    if (str[j] == '1') {
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
ll getModi(string str, ll n, ll k) {
  pwrTwo1[0] = 1 % k;
  for (ll i = 1; i < n; i++) {
    pwrTwo1[i] = pwrTwo1[i - 1] * (2 % k);
    pwrTwo1[i] %= k;
  }
  ll res = 0;
  ll i = 0, j = n - 1;
  while (i < n) {
    if (str[j] == '1') {
      res += (pwrTwo1[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int main() {
  flash;
  ll n;
  string x;
  cin >> n;
  cin >> x;
  ll y = 0;
  for (ll i = 0; i < sz(x); ++i) {
    if (x[i] == '1')
      y++;
  }
  ll f = y + 1, z = y - 1;
  ll yo = 1, ko = 1;
  ll rem = 0, rem1 = 0;
  if (f > 0)
    rem = getMod(x, sz(x), f);
  if (z > 0)
    rem1 = getModi(x, sz(x), z);
  reverse(all(x));
  ve ans;
  for (ll i = 0; i < sz(x); ++i) {
    ll yo = 0;
    if (z == 0 && x[i] == '1') {
      ans.pb(0);
    } else {
      if (x[i] == '1') {
        if (z > 0)
          yo = (rem1 - pwrTwo1[i] + z) % z;
      } else {
        if (f > 0)
          yo = (rem + pwrTwo[i]) % f;
      }
      ll cur = yo;
      ll ans1 = 0;
      while (cur) {
        ll nb = 0;
        for (ll j = 0; j <= log2(cur) + 3; ++j) {
          if ((cur & (ll)(pow(2, j))))
            nb++;
        }
        if (nb != 0)
          cur = cur % nb;
        ans1++;
      }
      ans.pb(ans1 + 1);
    }
  }
  reverse(all(ans));
  for (auto it : ans)
    cout << it << '\\n';
  return 0;
}
// code the AC sol !
//  BS/queue/map

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \\
  cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll) x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll, ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri {
  template <class T> bool operator()(T const &a, T const &b) const {
    return a > b;
  }
};
void setIO(string s) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
ll pwrTwo[800001], pwrTwo1[800001];
ll getMod(string str, ll n, ll k) {
  pwrTwo[0] = 1 % k;
  for (ll i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }
  ll res = 0;
  ll i = 0, j = n - 1;
  while (i < n) {
    if (str[j] == '1') {
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
ll getModi(string str, ll n, ll k) {
  pwrTwo1[0] = 1 % k;
  for (ll i = 1; i < n; i++) {
    pwrTwo1[i] = pwrTwo1[i - 1] * (2 % k);
    pwrTwo1[i] %= k;
  }
  ll res = 0;
  ll i = 0, j = n - 1;
  while (i < n) {
    if (str[j] == '1') {
      res += (pwrTwo1[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int main() {
  flash;
  ll n;
  string x;
  cin >> n;
  cin >> x;
  ll y = 0;
  for (ll i = 0; i < sz(x); ++i) {
    if (x[i] == '1')
      y++;
  }
  ll f = y + 1, z = y - 1;
  ll yo = 1, ko = 1;
  ll rem = getMod(x, sz(x), f);
  ll rem1 = getModi(x, sz(x), z);
  reverse(all(x));
  ve ans;
  for (ll i = 0; i < sz(x); ++i) {
    ll yo = 0;
    if (x[i] == '1') {
      yo = (rem1 - pwrTwo1[i] + z) % z;
    } else {
      yo = (rem + pwrTwo[i]) % f;
    }
    ll cur = yo;
    ll ans1 = 0;
    while (cur) {
      ll nb = 0;
      for (ll j = 0; j <= log2(cur) + 3; ++j) {
        if ((cur & (ll)(pow(2, j))))
          nb++;
      }
      if (nb != 0)
        cur = cur % nb;
      ans1++;
    }
    ans.pb(ans1 + 1);
  }
  reverse(all(ans));
  for (auto it : ans)
    cout << it << '\\n';
  return 0;
}
// code the AC sol !
//  BS/queue/map