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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

ll extGCD(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll g = extGCD(b, a % b, y, x);
  y -= a / b * x;
  return g;
}

ll MOD = 1000000007;

vector<ll> mfact(1, 1);

ll minv(ll a) {
  ll x;
  ll y;
  extGCD(a, MOD, x, y);
  if (x < 0) {
    x += ((-x) / MOD + 1) * MOD;
    x %= MOD;
  }
  return x;
}

ll mpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % MOD;
    b /= 2;
    a = (a * a) % MOD;
  }
  return res;
}

ll mfactorial(ll a) {
  if (mfact.size() - 1 >= a)
    return mfact[a];
  for (ll i = mfact.size(); i <= a; i++) {
    mfact.push_back((i * mfact[i - 1]) % MOD);
  }
  return mfact[a];
}

ll mcomb(ll n, ll r) {
  return (((mfactorial(n) * minv(mfactorial(r))) % MOD) *
          minv(mfactorial(n - r))) %
         MOD;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll X_1 = 0;
  ll X = 0;
  ll X_2 = 0;
  int pX = 0;
  // if(N == 1 && S[0] == '1'){
  //     cout<<0<<endl;
  //     return 0;
  // }
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      pX++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (S[i] == '0')
      continue;
    MOD = pX;
    X += mpow(2, N - i - 1);
    X %= pX;
    MOD = pX + 1;
    X_2 += mpow(2, N - i - 1);
    X_2 %= pX + 1;
    if (pX <= 1)
      continue;
    MOD = pX - 1;
    X_1 += mpow(2, N - i - 1);
    X_1 %= pX - 1;
  }
  for (int i = 0; i < N; i++) {
    ll x = 0;
    if (S[i] == '0') {
      MOD = pX + 1;
      ll n = mpow(2, N - i - 1);
      // cout<<X_2<<endl;
      x = (X_2 + n) % MOD;
    } else {
      if (pX == 1) {
        cout << 0 << endl;
        continue;
      }
      MOD = pX - 1;
      ll n = mpow(2, N - i - 1);
      // cout<<n<<endl;cout<<"!"<<endl;
      // cout<<X_1<<endl;
      x = (X_1 - n + MOD) % MOD;
    }
    // cout<<x<<endl;
    // cout<<"A"<<endl;
    int res = 1;
    while (x != 0) {
      ll xx = x;
      int d = 0;
      for (d = 0; xx > 1; xx /= 2) {
        d++;
      }
      d += 1;
      int pop = 0;
      for (int i = 0; i < d; i++) {
        // cout<<i<<endl;
        if (((x >> i) & 1) == 1)
          pop++;
      }
      x %= pop;
      // cout<<x<<endl;
      res++;
    }
    cout << res << endl;
    // cout<<"---"<<endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

ll extGCD(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll g = extGCD(b, a % b, y, x);
  y -= a / b * x;
  return g;
}

ll MOD = 1000000007;

vector<ll> mfact(1, 1);

ll minv(ll a) {
  ll x;
  ll y;
  extGCD(a, MOD, x, y);
  if (x < 0) {
    x += ((-x) / MOD + 1) * MOD;
    x %= MOD;
  }
  return x;
}

ll mpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % MOD;
    b /= 2;
    a = (a * a) % MOD;
  }
  return res;
}

ll mfactorial(ll a) {
  if (mfact.size() - 1 >= a)
    return mfact[a];
  for (ll i = mfact.size(); i <= a; i++) {
    mfact.push_back((i * mfact[i - 1]) % MOD);
  }
  return mfact[a];
}

ll mcomb(ll n, ll r) {
  return (((mfactorial(n) * minv(mfactorial(r))) % MOD) *
          minv(mfactorial(n - r))) %
         MOD;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll X_1 = 0;
  ll X = 0;
  ll X_2 = 0;
  int pX = 0;
  if (N == 1 && S[0] == '1') {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      pX++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (S[i] == '0')
      continue;
    MOD = pX;
    X += mpow(2, N - i - 1);
    X %= pX;
    MOD = pX + 1;
    X_2 += mpow(2, N - i - 1);
    X_2 %= pX + 1;
    if (pX == 0)
      continue;
    MOD = pX - 1;
    X_1 += mpow(2, N - i - 1);
    X_1 %= pX - 1;
  }
  for (int i = 0; i < N; i++) {
    ll x = 0;
    if (S[i] == '0') {
      MOD = pX + 1;
      ll n = mpow(2, N - i - 1);
      // cout<<X_2<<endl;
      x = (X_2 + n) % MOD;
    } else {

      MOD = pX - 1;
      ll n = mpow(2, N - i - 1);
      // cout<<n<<endl;cout<<"!"<<endl;
      // cout<<X_1<<endl;
      x = (X_1 - n + MOD) % MOD;
    }
    // cout<<x<<endl;
    // cout<<"A"<<endl;
    int res = 1;
    while (x != 0) {
      ll xx = x;
      int d = 0;
      for (d = 0; xx > 1; xx /= 2) {
        d++;
      }
      d += 1;
      int pop = 0;
      for (int i = 0; i < d; i++) {
        // cout<<i<<endl;
        if (((x >> i) & 1) == 1)
          pop++;
      }
      x %= pop;
      // cout<<x<<endl;
      res++;
    }
    cout << res << endl;
    // cout<<"---"<<endl;
  }
}