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

#include <bits/stdc++.h>
using namespace std;
constexpr int Nbit = 200'000;
constexpr int sNbit = 21;

int nextX(const bitset<sNbit> &bsX, int popX) {
  bitset<sNbit> X = bsX;

  int mod = 0;
  int base_mod = 1;
  while (X.count() > 0) {
    if (X.test(0)) {
      mod = (mod + base_mod) % popX;
    }

    base_mod = (2 * base_mod) % popX;
    X >>= 1;
  }

  return mod;
}

int init(const bitset<Nbit> &bsX, vector<int> *base_mod, int div) {
  if (div == 0) {
    return 0;
  } else {
    const size_t N = (int)base_mod->size();

    bitset<Nbit> X = bsX;
    int mod = 0;
    base_mod->at(0) = 1 % div;
    for (size_t i = 0; i < N; i++) {
      if (X.test(0)) {
        mod = (mod + base_mod->at(i)) % div;
      }

      if (i + 1 < N) {
        base_mod->at(i + 1) = (2 * base_mod->at(i)) % div;
      }
      X >>= 1;
    }

    return mod;
  }
}

int main() {
  int N;
  string X;
  cin >> N >> X;

  const bitset<Nbit> bsX(X);
  const int popX = (int)bsX.count();

  vector<int> offset_plus1(N);
  vector<int> offset_minus1(N);

  const int mod_popXplus1 = init(bsX, &offset_plus1, popX + 1);
  const int mod_popXminus1 = init(bsX, &offset_minus1, popX - 1);

  for (int i = 1; i <= N; i++) {
    int bit_i = (bsX.test(N - i)) ? 0 : 1;
    int popXi = (bit_i == 1) ? popX + 1 : popX - 1;

    bitset<sNbit> bsXi;
    int ans_i = 0;
    while (popXi > 0) {
      ans_i++;
      if (ans_i == 1) {
        int next = (bit_i == 1)
                       ? (mod_popXplus1 + offset_plus1.at(N - i)) % popXi
                       : (((mod_popXminus1 - offset_minus1.at(N - i)) % popXi) +
                          popXi) %
                             popXi;
        bsXi = bitset<sNbit>(next);
        popXi = (int)bsXi.count();
      } else {
        int next = nextX(bsXi, popXi);

        bsXi = bitset<sNbit>(next);
        popXi = (int)bsXi.count();
      }
    }

    cout << ans_i << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
constexpr int Nbit = 200'000;

int nextX(const bitset<Nbit> &bsX, int popX) {
  bitset<Nbit> X = bsX;

  int mod = 0;
  int base_mod = 1;
  while (X.count() > 0) {
    if (X.test(0)) {
      mod = (mod + base_mod) % popX;
    }

    base_mod = (2 * base_mod) % popX;
    X >>= 1;
  }

  return mod;
}

void init(vector<int> *buf, int popX) {
  int base_mod = 1;
  buf->at(0) = base_mod % popX;
  for (size_t i = 1; i < buf->size(); i++) {
    buf->at(i) = (2 * buf->at(i - 1)) % popX;
  }
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  const bitset<Nbit> bsX(X);
  const int popX = bsX.count();

  const int mod_popXplus1 = nextX(bsX, popX + 1);
  const int mod_popXminus1 = nextX(bsX, popX - 1);

  vector<int> offset_plus1(N);
  vector<int> offset_minus1(N);

  init(&offset_plus1, popX + 1);
  init(&offset_minus1, popX - 1);

  for (int i = 0; i < N; i++) {
    string Xi = X;
    Xi.at(i) = (X.at(i) == '0') ? '1' : '0';

    bitset<Nbit> bsXi(Xi);
    int popXi = (bsXi.test(N - 1 - i)) ? popX + 1 : popX - 1;

    int ans_i = 0;
    while (popXi > 0) {
      ans_i++;
      if (ans_i == 1) {
        int next =
            (bsXi.test(N - 1 - i))
                ? (mod_popXplus1 + offset_plus1.at(N - 1 - i)) % popXi
                : (((mod_popXminus1 - offset_minus1.at(N - 1 - i)) % popXi) +
                   popXi) %
                      popXi;
        bsXi = bitset<Nbit>(next);
        popXi = bsXi.count();
      } else {
        int next = nextX(bsXi, popXi);

        bsXi = bitset<Nbit>(next);
        popXi = bsXi.count();
      }
    }

    cout << ans_i << endl;
  }
}