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

/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define bitcount(x) (int)__builtin_popcount(x)
#define vi vector<int>
#define co(x) cout << x << '\\n'
#define dbg(x) cerr << #x << " = " << x << '\\n'
#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

int bigmod(int a, int p, int m) {
  if (p == 0)
    return (1 % m);
  if (p & 1) {
    int p1 = a % m;
    int p2 = bigmod(a, p - 1, m);
    return (p1 * p2) % m;
  }
  int p1 = bigmod(a, p / 2, m);
  return (p1 * p1) % m;
}

inline void solve() {
  int n, bit = 0, l = 0, r = 0, in = -1;
  string x;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    bit += (x[i] == '1');
    if (x[i] == '1')
      in = n - i - 1;
  }
  for (int i = n - 1, j = 0; i >= 0 && bit > 1; i--, j++) {
    if (x[i] == '1') {
      l += bigmod(2, j, bit - 1);
      r += bigmod(2, j, bit + 1);
      l %= bit - 1;
      r %= bit + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    int num = 0, cnt = 1;
    if (bit == 1) {
      if (x[i] == '1')
        num = 0, cnt--;
      else
        num = bigmod(2, n - i - 1, 2) + bigmod(2, in, 2);
    } else if (bit == 0) {
      num = bigmod(2, n - i - 1, 1);
    } else if (x[i] == '1') {
      num = l;
      num -= bigmod(2, n - i - 1, bit - 1);
      if (bit - 1 == 0)
        cnt = 0;
      if (num < 0)
        num += bit - 1;
      num %= bit - 1;
    } else {
      num = r;
      num += bigmod(2, n - i - 1, bit + 1);
      num %= bit + 1;
    }
    while (num) {
      num = num % bitcount(num);
      cnt++;
    }
    co(cnt);
  }
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define bitcount(x) (int)__builtin_popcount(x)
#define vi vector<int>
#define co(x) cout << x << '\\n'
#define dbg(x) cerr << #x << " = " << x << '\\n'
#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

int bigmod(int a, int p, int m) {
  if (p == 0)
    return 1;
  if (p & 1) {
    int p1 = a % m;
    int p2 = bigmod(a, p - 1, m);
    return (p1 * p2) % m;
  }
  int p1 = bigmod(a, p / 2, m);
  return (p1 * p1) % m;
}

inline void solve() {
  int n, bit = 0, l = 0, r = 0;
  string x;
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    bit += (x[i] == '1');
  for (int i = n - 1, j = 0; i >= 0; i--, j++) {
    if (x[i] == '1') {
      l += bigmod(2, j, bit - 1);
      r += bigmod(2, j, bit + 1);
      l %= bit - 1;
      r %= bit + 1;
    }
  }
  // co(p);
  for (int i = 0; i < n; i++) {
    int num = 0, cur, cnt = 1;
    if (x[i] == '1') {
      num = l;
      num -= bigmod(2, n - i - 1, bit - 1);
      if (bit - 1 == 0)
        cnt = 0;
      if (num < 0)
        num += bit - 1;
      num %= bit - 1;
    } else {
      num = r;
      num += bigmod(2, n - i - 1, bit + 1);
      num %= bit + 1;
    }
    while (num) {
      num = num % bitcount(num);
      cnt++;
    }
    co(cnt);
  }
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}