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

/*** akshaykumar99 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long N = 1e5 + 1;

typedef long long ll;
typedef long double ld;

ll binpow(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
void test_case() {
  ll n;
  string s;
  cin >> n >> s;
  ll res[n + 1];
  for (int i = 0; i <= n; i++) {
    res[i] = -1;
  }
  res[0] = 0;
  for (int i = 1; i <= n; i++) {
    ll x = __builtin_popcount(i);
    ll z = i % x;
    res[i] = res[z] + 1;
  }
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      tot++;
  }
  ll x1 = 0, x2 = 0, x3 = 0;
  ll pow1 = 1, pow2 = 1, pow3 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (tot > 0)
        x1 = (x1 + pow1) % tot;
      if (tot + 1 > 0)
        x2 = (x2 + pow2) % (tot + 1);
      if (tot - 1 > 0)
        x3 = (x3 + pow3) % (tot - 1);
    }
    if (tot > 0)
      pow1 = (pow1 * 2) % (tot);
    if (tot + 1 > 0)
      pow2 = (pow2 * 2) % (tot + 1);
    if (tot - 1 > 0)
      pow3 = (pow3 * 2) % (tot - 1);
  }
  for (int i = 0; i < n; i++) {
    ll curr = tot;
    if (s[i] == '1')
      curr--;
    else
      curr++;
    if (curr == tot) {
      cout << res[x1] + 1 << '\\n';
    } else if (curr == tot + 1) {
      ll xx = 0;
      if (tot + 1 > 0)
        xx = binpow(2, n - 1 - i, (tot + 1));
      // cout << xx << ' ';
      // cout << res[(x2 + xx) % (tot+1)] + 1 << '\\n';
      ll ans = 0;
      if (tot + 1 > 0)
        ans = res[(x2 + xx) % (tot + 1)] + 1;
      cout << ans << '\\n';
    } else if (curr == tot - 1) {
      ll xx = 0;
      if (tot - 1 > 0)
        xx = binpow(2, n - 1 - i, (tot - 1));
      // cout << xx << ' ';
      ll ans = 0;
      if (tot - 1 > 0)
        ans = res[(x3 - xx + tot - 1) % (tot - 1)] + 1;
      cout << ans << '\\n';
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}


Error: 0

Fixed submission:

/*** akshaykumar99 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long N = 1e5 + 1;

typedef long long ll;
typedef long double ld;

ll binpow(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
void test_case() {
  ll n;
  string s;
  cin >> n >> s;
  ll res[n + 1];
  for (int i = 0; i <= n; i++) {
    res[i] = -1;
  }
  res[0] = 0;
  for (int i = 1; i <= n; i++) {
    ll x = __builtin_popcount(i);
    ll z = i % x;
    res[i] = res[z] + 1;
  }
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      tot++;
  }
  ll x1 = 0, x2 = 0, x3 = 0;
  ll pow1 = 1, pow2 = 1, pow3 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (tot > 0)
        x1 = (x1 + pow1) % tot;
      if (tot + 1 > 0)
        x2 = (x2 + pow2) % (tot + 1);
      if (tot - 1 > 0)
        x3 = (x3 + pow3) % (tot - 1);
    }
    if (tot > 0)
      pow1 = (pow1 * 2) % (tot);
    if (tot + 1 > 0)
      pow2 = (pow2 * 2) % (tot + 1);
    if (tot - 1 > 0)
      pow3 = (pow3 * 2) % (tot - 1);
  }
  for (int i = 0; i < n; i++) {
    ll curr = tot;
    if (s[i] == '1')
      curr--;
    else
      curr++;
    if (curr == tot) {
      cout << res[x1] + 1 << '\\n';
    } else if (curr == tot + 1) {
      ll xx = 0;
      if (tot + 1 > 0)
        xx = binpow(2, n - 1 - i, (tot + 1));
      // cout << xx << ' ';
      cout << res[(x2 + xx) % (tot + 1)] + 1 << '\\n';
    } else if (curr == tot - 1) {
      ll xx = 0;
      if (tot - 1 > 0)
        xx = binpow(2, n - 1 - i, (tot - 1));
      // cout << xx << ' ';
      cout << res[(x3 - xx + tot - 1) % (tot - 1)] + 1 << '\\n';
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}