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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int mod = 1;

struct mint {
  ll x;

  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

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

  int n;
  cin >> n;

  string s;
  cin >> s;

  reverse(s.begin(), s.end());

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }

  mod = cnt - 1;
  mint less = 0;

  if (mod > 0) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        less = less + mint(2).pow(i);
    }
  }

  mod = cnt + 1;
  mint more = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      more = more + mint(2).pow(i);
  }

  for (int i = n - 1; i >= 0; i--) {

    if (s[i] == '0') {
      int tmp_cnt = 1;
      mod = cnt + 1;
      mint two = mint(2).pow(i);
      mint mod_ans = more + two;

      int ans = mod_ans.x;
      int int_mod = __builtin_popcount(ans);

      while (true) {
        if (ans == 0)
          break;
        if (int_mod == 0)
          break;
        else
          ans = ans % int_mod;

        int_mod = __builtin_popcount(ans);
        tmp_cnt++;
      }
      cout << tmp_cnt << '\\n';

    } else {
      int tmp_cnt = 1;
      mod = cnt - 1;

      if (mod == 0) {
        cout << 0 << '\\n';
      } else {

        mint two = mint(2).pow(i);
        mint mod_ans = less - two;

        int ans = mod_ans.x;
        int int_mod = __builtin_popcount(ans);

        while (true) {
          if (ans == 0)
            break;
          if (int_mod == 0)
            break;
          else
            ans = ans % int_mod;

          int_mod = __builtin_popcount(ans);
          tmp_cnt++;
        }
        cout << tmp_cnt << '\\n';
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

ll mod = 1;

struct mint {
  ll x;

  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

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

  int n;
  cin >> n;

  string s;
  cin >> s;

  reverse(s.begin(), s.end());

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }

  mod = cnt - 1;

  mint less = 0;
  if (mod > 0) {
    for (ll i = 0; i < n; i++) {
      if (s[i] == '0')
        continue;
      less = less + mint(2).pow(i);
    }
  }

  mod = cnt + 1;
  mint more = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    more = more + mint(2).pow(i);
  }

  for (int i = n - 1; i >= 0; i--) {

    if (s[i] == '0') {
      int tmp_cnt = 1;
      mod = cnt + 1;
      mint two = mint(2).pow(i);
      mint mod_ans = more + two;

      int ans = mod_ans.x;
      int int_mod = __builtin_popcount(ans);

      while (true) {
        if (ans == 0)
          break;
        if (int_mod == 0)
          break;
        else
          ans = ans % int_mod;

        int_mod = __builtin_popcount(ans);
        tmp_cnt++;
      }
      cout << tmp_cnt << '\\n';

    } else {
      int tmp_cnt = 1;
      mod = cnt - 1;
      mint two = mint(2).pow(i);
      mint mod_ans = less - two;

      int ans = mod_ans.x;
      int int_mod = __builtin_popcount(ans);

      while (true) {
        if (ans == 0)
          break;
        if (int_mod == 0)
          break;
        else
          ans = ans % int_mod;

        int_mod = __builtin_popcount(ans);
        tmp_cnt++;
      }
      cout << tmp_cnt << '\\n';
    }
  }

  return 0;
}