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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct Intmod {
  int mod;
  ll a;
  Intmod(int mod, ll a = 0) : a(((a % mod) + mod) % mod), mod(mod) {}

  Intmod operator-() const { return Intmod(mod, -a); }

  Intmod &operator=(const ll b) {
    a = ((b % mod) + mod) % mod;
    return *this;
  }
  Intmod &operator+=(const Intmod b) {
    if ((a += b.a) >= mod)
      a -= mod;
    return *this;
  }
  Intmod &operator+=(const ll b) {
    if ((a += b) >= Intmod(mod, b).a)
      a -= mod;
    return *this;
  }
  Intmod &operator-=(const Intmod b) {
    if ((a -= b.a) < 0)
      a += mod;
    return *this;
  }
  Intmod &operator-=(const ll b) {
    if ((a -= Intmod(mod, b).a) < 0)
      a += mod;
    return *this;
  }
  Intmod &operator*=(const Intmod b) {
    (a *= b.a) %= mod;
    return *this;
  }
  Intmod &operator*=(const ll b) {
    (a *= Intmod(mod, b).a) %= mod;
    return *this;
  }

  template <typename T> Intmod operator+(const T b) const {
    return Intmod(*this) += b;
  }
  template <typename T> Intmod operator-(const T b) const {
    return Intmod(*this) -= b;
  }
  template <typename T> Intmod operator*(const T b) const {
    return Intmod(*this) *= b;
  }

  // Valid for prime mod. pown() needs to be included.
  // int i = 0;
  // Intmod inv() {
  //     if(a == 0) throw "Inverse of zero does not exist.";
  //     if(!i) i = pown(*this, mod - 2).a;
  //     return Intmod(mod, i);
  // }
  // Intmod& operator/=(const Intmod b) {return *this *= b.inv();}
  // Intmod& operator/=(const ll b) {return *this *= Intmod(mod, b).inv();}
  // template<typename T> Intmod operator/(const T b) const {return
  // Intmod(*this) /= b;}
};
Intmod operator+(const ll b, Intmod a) { return a += b; }
Intmod operator-(const ll b, Intmod a) { return a -= b; }
Intmod operator*(const ll b, Intmod a) { return a *= b; }
// Intmod operator/(const ll b, Intmod a) {return a /= b;} //Valid for prime
// mod.

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  string x;
  cin >> n >> x;
  vector<bool> xb(n);
  int popcnt = 0;
  for (int i = 0; i < n; ++i)
    if (xb[n - 1 - i] = x[i] - '0')
      popcnt++;

  int ans, div;

  vector<Intmod> bimodp(n, Intmod(popcnt + 1)), bimodm;
  bimodp[0] = 1;
  for (int i = 0; i < n - 1; ++i)
    bimodp[i + 1] = bimodp[i] * 2;
  Intmod xp(popcnt + 1), xm(1);
  for (int i = 0; i < n; ++i)
    xp += bimodp[i] * xb[i];

  if (popcnt >= 2) {
    bimodm = vector<Intmod>(n, Intmod(popcnt - 1));
    bimodm[0] = 1;
    for (int i = 0; i < n - 1; ++i)
      bimodm[i + 1] = bimodm[i] * 2;
    xm = Intmod(popcnt - 1);
    for (int i = 0; i < n; ++i)
      xm += bimodm[i] * xb[i];
  }

  for (int i = n - 1; i >= 0; --i) {
    if (popcnt == 1 && xb[i]) {
      printf("0\\n");
      continue;
    }
    ans = 1;
    if (xb[i])
      div = (xm - bimodm[i]).a;
    else
      div = (xp + bimodp[i]).a;

    while (div) {
      ans++;
      div %= __builtin_popcount(div);
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct Intmod {
  int mod;
  ll a;
  Intmod(int mod, ll a = 0) : a(((a % mod) + mod) % mod), mod(mod) {}

  Intmod operator-() const { return Intmod(mod, -a); }

  Intmod &operator=(const ll b) {
    a = ((b % mod) + mod) % mod;
    return *this;
  }
  Intmod &operator+=(const Intmod b) {
    if ((a += b.a) >= mod)
      a -= mod;
    return *this;
  }
  Intmod &operator+=(const ll b) {
    if ((a += b) >= Intmod(mod, b).a)
      a -= mod;
    return *this;
  }
  Intmod &operator-=(const Intmod b) {
    if ((a -= b.a) < 0)
      a += mod;
    return *this;
  }
  Intmod &operator-=(const ll b) {
    if ((a -= Intmod(mod, b).a) < 0)
      a += mod;
    return *this;
  }
  Intmod &operator*=(const Intmod b) {
    (a *= b.a) %= mod;
    return *this;
  }
  Intmod &operator*=(const ll b) {
    (a *= Intmod(mod, b).a) %= mod;
    return *this;
  }

  template <typename T> Intmod operator+(const T b) const {
    return Intmod(*this) += b;
  }
  template <typename T> Intmod operator-(const T b) const {
    return Intmod(*this) -= b;
  }
  template <typename T> Intmod operator*(const T b) const {
    return Intmod(*this) *= b;
  }

  // Valid for prime mod. pown() needs to be included.
  // int i = 0;
  // Intmod inv() {
  //     if(a == 0) throw "Inverse of zero does not exist.";
  //     if(!i) i = pown(*this, mod - 2).a;
  //     return Intmod(mod, i);
  // }
  // Intmod& operator/=(const Intmod b) {return *this *= b.inv();}
  // Intmod& operator/=(const ll b) {return *this *= Intmod(mod, b).inv();}
  // template<typename T> Intmod operator/(const T b) const {return
  // Intmod(*this) /= b;}
};
Intmod operator+(const ll b, Intmod a) { return a += b; }
Intmod operator-(const ll b, Intmod a) { return a -= b; }
Intmod operator*(const ll b, Intmod a) { return a *= b; }
// Intmod operator/(const ll b, Intmod a) {return a /= b;} //Valid for prime
// mod.

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  string x;
  cin >> n >> x;
  bitset<100002> xb(x);

  int one = xb.count();
  int ans, div;
  bitset<100002> xt;
  vector<Intmod> bimodp(n, Intmod(one + 1)), bimodm(n, Intmod(one - 1));
  bimodp[0] = 1;
  bimodm[0] = 1;
  for (int i = 0; i < n - 1; ++i) {
    bimodp[i + 1] = bimodp[i] * 2;
    bimodm[i + 1] = bimodm[i] * 2;
  }
  Intmod xp(one + 1), xm(one - 1);
  for (int i = 0; i < n; ++i) {
    xp += bimodp[i] * xb[i];
    xm += bimodm[i] * xb[i];
  }
  bitset<100002> tmp;
  for (int i = n - 1; i >= 0; --i) {
    if (one == 1 && xb[i]) {
      printf("0\\n");
      continue;
    }
    ans = 1;
    if (xb[i])
      div = (xm - bimodm[i]).a;
    else
      div = (xp + bimodp[i]).a;

    while (div) {
      ans++;
      tmp = div;
      div %= tmp.count();
    }
    printf("%d\\n", ans);
  }

  return 0;
}