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

#include <bits/stdc++.h>
#define ll long long int
#define pragi(a, b) for (ll i = a; i < b; i++)
#define pragj(a, b) for (ll j = a; j < b; j++)
#define pragk(a, b) for (ll k = a; k >= b; k--)
#define all(v) (v.begin(), v.end())
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define MT make_tuple
#define F first
#define S second
#define KAKA                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef long double ld;
typedef pair<int, int> pii;
// typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ll, ll> pll;
ll negmd(ll a, ll n) { return (n + (a % n)) % n; }
ll modexp(ll x, ll n, ll m) {
  if (n == 0)
    return 1;
  else if (n & 1) {
    return ((x % m) * modexp(x, n - 1, m)) % m;
  } else {
    ll y = modexp(x, n / 2, m);
    return (y * y) % m;
  }
}
void Compute(ll n) {
  KAKA string s;
  cin >> s;
  ll popc = 0;
  ll a = 0, b = 0, c = 0;
  popc = count(s.begin(), s.end(), '1');
  ll rem1 = popc, rem2 = popc - 1, rem3 = popc + 1;
  pragi(0, n) {
    if (s[i] == '1') {
      if (rem1)
        a = ((a % rem1) + (modexp(2, n - i - 1, rem1))) % rem1;
      if (rem2)
        b = ((b % rem2) + (modexp(2, n - i - 1, rem2))) % rem2;
      if (rem3)
        c = ((c % rem3) + (modexp(2, n - i - 1, rem3))) % rem3;
    }
  }
  // cout<<a<<" "<<b<<" "<<c<<"\\n";
  pragi(0, n) {
    // ith bit is inverted

    ll num = 0;
    ll steps = 0;
    if (s[i] == '0') {
      num = (c + modexp(2, n - i - 1, rem3)) % rem3;
      steps++;
    } else if ((s[i] == '1') and (popc > 1)) {
      num = (b - modexp(2, n - i - 1, rem2));
      if (num >= 0)
        num %= rem2;
      else
        num = negmd(num, rem2);
      steps++;
    } else if ((s[i] == '1') and (popc == 1)) {
      cout << 0 << "\\n";
      continue;
    }
    // ll steps = 1;
    // cout<<num<<"\\n";
    while (num > 0) {
      ll pc = __builtin_popcountll(num);
      num %= pc;
      steps++;
    }
    cout << steps << "\\n";
  }
}
int main() {
  KAKA ll n;
  cin >> n;
  Compute(n);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define pragi(a, b) for (ll i = a; i < b; i++)
#define pragj(a, b) for (ll j = a; j < b; j++)
#define pragk(a, b) for (ll k = a; k >= b; k--)
#define all(v) (v.begin(), v.end())
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define MT make_tuple
#define F first
#define S second
#define KAKA                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef long double ld;
typedef pair<int, int> pii;
// typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ll, ll> pll;
ll negmd(ll a, ll n) { return (n + (a % n)) % n; }
ll modexp(ll x, ll n, ll m) {
  if (n == 0)
    return 1;
  else if (n & 1) {
    return ((x % m) * modexp(x, n - 1, m)) % m;
  } else {
    ll y = modexp(x, n / 2, m);
    return (y * y) % mod;
  }
}
void Compute(ll n) {
  KAKA string s;
  cin >> s;
  ll popc = 0;
  ll a = 0, b = 0, c = 0;
  popc = count(s.begin(), s.end(), '1');
  ll rem1 = popc, rem2 = popc - 1, rem3 = popc + 1;
  pragi(0, n) {
    if (s[i] == '1') {
      a = ((a % rem1) + (modexp(2, n - i - 1, rem1))) % rem1;
      b = ((b % rem2) + (modexp(2, n - i - 1, rem2))) % rem2;
      c = ((c % rem3) + (modexp(2, n - i - 1, rem3))) % rem3;
    }
  }

  pragi(0, n) {
    // ith bit is inverted
    ll num;
    if (s[i] == '0') {
      num = (c + modexp(2, n - i - 1, rem3)) % rem3;
    } else if (s[i] == '1') {
      num = (b - modexp(2, n - i - 1, rem2));
      if (num >= 0)
        num %= rem2;
      else
        num = negmd(num, rem2);
    }
    ll steps = 1;
    while (num > 0) {
      ll pc = __builtin_popcountll(num);
      num %= pc;
      steps++;
    }
    cout << steps << "\\n";
  }
}
int main() {
  KAKA ll n;
  cin >> n;
  Compute(n);
}