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

#include <bits/stdc++.h>
using namespace std;
//
template <class T> struct mow {
  T pow(T x, T n, T mod) {
    T ret = 1;
    if (n > 0) {
      ret = pow(x, n / 2, mod);
      if (n % 2 == 0) {
        (ret = (ret * ret)) %= mod;
      } else {
        (ret = ((ret * ret) % mod) * x) %= mod;
      }
    }
    return ret;
  }
};
long long mem[222222][2] = {};
void solve(long long n, string x, long long cnt, long long hoge) {
  long long ans = 1;
  if (cnt == 0) {
    cout << 0 << endl;
    return;
  }
  if (cnt == 1) {
    cout << 1 << endl;
    return;
  }
  while (hoge > 0) {
    ans++;
    long long ji = 0;
    for (int i = 0; i < 20; i++) {
      if ((1LL << i) & hoge) {
        ji++;
      }
    }
    hoge %= ji;
  }
  cout << ans << endl;
  return;
}
int main() {
  mow<long long> nii;
  long long n;
  string x;
  cin >> n >> x;
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cnt++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (cnt > 1) {
      mem[i][0] = nii.pow(2, i, cnt - 1);
    }
    mem[i][1] = nii.pow(2, i, cnt + 1);
  }
  long long mema = 0;
  long long memb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (cnt > 1) {
        (mema += mem[n - 1 - i][0]) %= (cnt - 1);
      }
      (memb += mem[n - 1 - i][1]) %= (cnt + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      x[i] = '1';
      solve(n, x, cnt + 1, (memb + mem[n - 1 - i][1]) % (cnt + 1));
      x[i] = '0';
    } else {
      x[i] = '0';
      if (cnt > 1) {
        solve(n, x, cnt - 1,
              (mema - mem[n - 1 - i][0] + (cnt - 1) * 222222) % (cnt - 1));
      } else {
        if (cnt == 1) {
          cout << 0 << endl;
        }
      }
      x[i] = '1';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
//
template <class T> struct mow {
  T pow(T x, T n, T mod) {
    T ret = 1;
    if (n > 0) {
      ret = pow(x, n / 2, mod);
      if (n % 2 == 0) {
        (ret = (ret * ret)) %= mod;
      } else {
        (ret = ((ret * ret) % mod) * x) %= mod;
      }
    }
    return ret;
  }
};
long long mem[222222][2] = {};
void solve(long long n, string x, long long cnt, long long hoge) {
  long long ans = 1;
  if (cnt == 0) {
    cout << 0 << endl;
    return;
  }
  if (cnt == 1) {
    cout << 1 << endl;
    return;
  }
  while (hoge > 0) {
    ans++;
    long long ji = 0;
    for (int i = 0; i < 20; i++) {
      if ((1LL << i) & hoge) {
        ji++;
      }
    }
    hoge %= ji;
  }
  cout << ans << endl;
  return;
}
int main() {
  mow<long long> nii;
  long long n;
  string x;
  cin >> n >> x;
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cnt++;
    }
  }
  for (int i = 0; i < n; i++) {
    mem[i][0] = nii.pow(2, i, cnt - 1);
    mem[i][1] = nii.pow(2, i, cnt + 1);
  }
  long long mema = 0;
  long long memb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      (mema += mem[n - 1 - i][0]) %= (cnt - 1);
      (memb += mem[n - 1 - i][1]) %= (cnt + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      x[i] = '1';
      solve(n, x, cnt + 1, (memb + mem[n - 1 - i][1]) % (cnt + 1));
      x[i] = '0';
    } else {
      x[i] = '0';
      solve(n, x, cnt - 1,
            (mema - mem[n - 1 - i][0] + (cnt - 1) * 222222) % (cnt - 1));
      x[i] = '1';
    }
  }
  return 0;
}