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

/*
    Ujjawal Pabreja [cuber_coder]
*/
#include <bits/stdc++.h>
#include <functional>
using namespace std;
#define speed_cubing                                                           \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
#define ld long double
#define rep(i, a) for (ll i = 0; i < a; i++)
#define repe(i, a, b) for (ll i = a; i < b; i++)
#define bac(i, a) for (ll i = a; i >= 0; i--)
#define bace(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define in insert
#define ff first
#define ss second
#define setbit(x) __builtin_popcountll(x)
#define init(c, a) memset(c, a, sizeof(c))
#define all(c) c.begin(), c.end()
#define sz(c) (ll) c.size()
#define lb lower_bound
#define ub upper_bound
#define maxe *max_element
#define mine *min_element
#define rev reverse
#define Y "YES"
#define N "NO"
#define endl "\\n"
#define debug(x) cout << #x << " : " << x << nl;
const ll inf = 1e18;
ll mod;
const ll MOD = 998244353;
const ll MAX = 2e5 + 1;

// ====== BigInt begins ======
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// ====== BigInt ends ======

// ====== PBDS begins ======
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag,
//         tree_order_statistics_node_update>
//         pbds;
// ====== PBDS ends ======

// ====== Useful functions begin ======
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll pwr(ll x, ll n) {
  if (n == 0) {
    return 1;
  }
  if (n & 1) {
    return mul(x, pwr(mul(x, x), (n - 1) / 2));
  } else {
    return pwr(mul(x, x), n / 2);
  }
}
ll modInv(ll n) { return pwr(n, mod - 2); }
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
// ====== Useful functions end ======

// ====== Variables begin ======

// ====== Variables end ======
void speed_solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll one = 0;
  rep(i, n) one += (s[i] == '1');
  rev(all(s));

  mod = one + 1;
  ll remOn = 0;
  rep(i, n) { remOn = add(remOn, pwr(2, i) * (s[i] - '0')); }
  mod = one - 1;
  ll remOff = 0;
  if (mod) {
    rep(i, n) { remOff = add(remOff, pwr(2, i) * (s[i] - '0')); }
  }

  vector<ll> ans;
  rep(i, n) {
    if (s[i] == '0') {
      mod = one + 1;
      ll temp = add(remOn, pwr(2, i));
      ll steps = 1;
      while (temp) {
        ll x = setbit(temp);
        temp %= x;
        steps++;
      }
      ans.pb(steps);
    } else {
      mod = one - 1;
      if (!mod) {
        ans.pb(0);
        continue;
      }
      ll temp = sub(remOff, pwr(2, i));
      ll steps = 1;
      while (temp) {
        ll x = setbit(temp);
        temp %= x;
        steps++;
      }
      ans.pb(steps);
    }
  }
  rev(all(ans));
  rep(i, n) cout << ans[i] << endl;
}

int main() {
  speed_cubing;
  ll test_cases = 1;
  // cin >> test_cases;
  rep(i, test_cases) {
    // cout << "Case #" << i + 1 << ": ";
    speed_solve();
  }
  return 0;
}

Error: 0

Fixed submission:

/*
    Ujjawal Pabreja [cuber_coder]
*/
#include <bits/stdc++.h>
#include <functional>
using namespace std;
#define speed_cubing                                                           \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
#define ld long double
#define rep(i, a) for (ll i = 0; i < a; i++)
#define repe(i, a, b) for (ll i = a; i < b; i++)
#define bac(i, a) for (ll i = a; i >= 0; i--)
#define bace(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define in insert
#define ff first
#define ss second
#define setbit(x) __builtin_popcountll(x)
#define init(c, a) memset(c, a, sizeof(c))
#define all(c) c.begin(), c.end()
#define sz(c) (ll) c.size()
#define lb lower_bound
#define ub upper_bound
#define maxe *max_element
#define mine *min_element
#define rev reverse
#define Y "YES"
#define N "NO"
#define endl "\\n"
#define debug(x) cout << #x << " : " << x << nl;
const ll inf = 1e18;
ll mod;
const ll MOD = 998244353;
const ll MAX = 2e5 + 1;

// ====== BigInt begins ======
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// ====== BigInt ends ======

// ====== PBDS begins ======
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag,
//         tree_order_statistics_node_update>
//         pbds;
// ====== PBDS ends ======

// ====== Useful functions begin ======
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll pwr(ll x, ll n) {
  if (n == 0) {
    return 1;
  }
  if (n & 1) {
    return mul(x, pwr(mul(x, x), (n - 1) / 2));
  } else {
    return pwr(mul(x, x), n / 2);
  }
}
ll modInv(ll n) { return pwr(n, mod - 2); }
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
// ====== Useful functions end ======

// ====== Variables begin ======

// ====== Variables end ======
void speed_solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll one = 0;
  rep(i, n) one += (s[i] == '1');
  mod = one + 1;
  ll remOn = 0, stepsOn = 1;
  rev(all(s));
  rep(i, n) { remOn = add(remOn, pwr(2, i) * (s[i] - '0')); }
  ll temp = remOn;
  while (temp) {
    ll x = setbit(temp);
    temp %= x;
    stepsOn++;
  }
  ll remOff = 0, stepsOff = 1;
  mod = one - 1;
  rep(i, n) { remOff = add(remOff, pwr(2, i) * (s[i] - '0')); }
  temp = remOff;
  while (temp) {
    ll x = setbit(temp);
    temp %= x;
    stepsOff++;
  }
  vector<ll> ans;
  rep(i, n) {
    if (s[i] == '0') {
      mod = one + 1;
      ll temp = add(remOn, pwr(2, i));
      ll steps = 1;
      while (temp) {
        ll x = setbit(temp);
        temp %= x;
        steps++;
      }
      ans.pb(steps);
    } else {
      mod = one - 1;
      ll temp = sub(remOff, pwr(2, i));
      ll steps = 1;
      while (temp) {
        ll x = setbit(temp);
        temp %= x;
        steps++;
      }
      ans.pb(steps);
    }
  }
  rev(all(ans));
  rep(i, n) cout << ans[i] << endl;
}

int main() {
  speed_cubing;
  ll test_cases = 1;
  // cin >> test_cases;
  rep(i, test_cases) {
    // cout << "Case #" << i + 1 << ": ";
    speed_solve();
  }
  return 0;
}