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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\\n"
#define ENDL cout << "\\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\\n"
#define init()                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\\n";
#define debugV(v)                                                              \\
  cerr << "[debugV] " << #v << ":";                                            \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << "\\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

int modA = 0;
struct mintA {
  ll x;
  mintA(ll x = 0) : x((x % modA + modA) % modA) {}
  mintA operator-() const { return mintA(-x); }
  mintA &operator+=(const mintA a) {
    if ((x += a.x) >= modA)
      x -= modA;
    return *this;
  }
  mintA &operator-=(const mintA a) {
    if ((x += modA - a.x) >= modA)
      x -= modA;
    return *this;
  }
  mintA &operator*=(const mintA a) {
    (x *= a.x) %= modA;
    return *this;
  }
  mintA operator+(const mintA a) const { return mintA(*this) += a; }
  mintA operator-(const mintA a) const { return mintA(*this) -= a; }
  mintA operator*(const mintA a) const { return mintA(*this) *= a; }
  mintA pow(ll t) const {
    if (!t)
      return 1;
    mintA a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime modA
  mintA inv() const { return pow(modA - 2); }
  mintA &operator/=(const mintA a) { return *this *= a.inv(); }
  mintA operator/(const mintA a) const { return mintA(*this) /= a; }
};
istream &operator>>(istream &is, const mintA &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mintA &a) { return os << a.x; }

int modB = 0;
struct mintB {
  ll x;
  mintB(ll x = 0) : x((x % modB + modB) % modB) {}
  mintB operator-() const { return mintB(-x); }
  mintB &operator+=(const mintB a) {
    if ((x += a.x) >= modB)
      x -= modB;
    return *this;
  }
  mintB &operator-=(const mintB a) {
    if ((x += modB - a.x) >= modB)
      x -= modB;
    return *this;
  }
  mintB &operator*=(const mintB a) {
    (x *= a.x) %= modB;
    return *this;
  }
  mintB operator+(const mintB a) const { return mintB(*this) += a; }
  mintB operator-(const mintB a) const { return mintB(*this) -= a; }
  mintB operator*(const mintB a) const { return mintB(*this) *= a; }
  mintB pow(ll t) const {
    if (!t)
      return 1;
    mintB a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime modB
  mintB inv() const { return pow(modB - 2); }
  mintB &operator/=(const mintB a) { return *this *= a.inv(); }
  mintB operator/(const mintB a) const { return mintB(*this) /= a; }
};
// istream& operator>>(istream& is, const mintB& a) {
//   return is >> a.x;
// }
// ostream& operator<<(ostream& os, const mintB& a) {
//   return os << a.x;
// }

signed main() {
  init();
  ll N;
  string S;
  cin >> N >> S;

  vll b(1, 0);
  vll pop(1, 0);
  ll index = 0;
  while (index < 201000) {
    vll next = b;
    rep(i, b.size()) {
      pop.pb(b[i] + 1);
      next.pb(b[i] + 1);
      index++;
    }
    b = next;
  }

  ll count = 0;
  rep(i, N) if (S[i] == '1') count++;

  REVERSE(S);
  string rS = S;

  modA = count + 1;
  modB = max(1ll, count - 1);
  mintA mod_a = 0;
  mintB mod_b = 0;
  rep(i, N) {
    if (rS[i] == '1') {
      mod_a += mintA(2).pow(i);
      mod_b += mintB(2).pow(i);
    }
  }

  vll ans;
  rep(i, N) {
    ll m = 0;
    if (rS[i] == '1') {
      mintB mb = mod_b;
      mb -= mintB(2).pow(i);
      m = mb.x;
    } else {
      mintA ma = mod_a;
      ma += mintA(2).pow(i);
      m = ma.x;
    }

    if (count == 1 and rS[i] == '1') {
      ans.pb(0);
      continue;
    }

    ll count = 1;
    while (m != 0) {
      m %= pop[m];
      count++;
    }
    ans.pb(count);
  }

  repr(i, N - 1) COUT(ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\\n"
#define ENDL cout << "\\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\\n"
#define init()                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\\n";
#define debugV(v)                                                              \\
  cerr << "[debugV] " << #v << ":";                                            \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << "\\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

int modA = 0;
struct mintA {
  ll x;
  mintA(ll x = 0) : x((x % modA + modA) % modA) {}
  mintA operator-() const { return mintA(-x); }
  mintA &operator+=(const mintA a) {
    if ((x += a.x) >= modA)
      x -= modA;
    return *this;
  }
  mintA &operator-=(const mintA a) {
    if ((x += modA - a.x) >= modA)
      x -= modA;
    return *this;
  }
  mintA &operator*=(const mintA a) {
    (x *= a.x) %= modA;
    return *this;
  }
  mintA operator+(const mintA a) const { return mintA(*this) += a; }
  mintA operator-(const mintA a) const { return mintA(*this) -= a; }
  mintA operator*(const mintA a) const { return mintA(*this) *= a; }
  mintA pow(ll t) const {
    if (!t)
      return 1;
    mintA a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime modA
  mintA inv() const { return pow(modA - 2); }
  mintA &operator/=(const mintA a) { return *this *= a.inv(); }
  mintA operator/(const mintA a) const { return mintA(*this) /= a; }
};
istream &operator>>(istream &is, const mintA &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mintA &a) { return os << a.x; }

int modB = 0;
struct mintB {
  ll x;
  mintB(ll x = 0) : x((x % modB + modB) % modB) {}
  mintB operator-() const { return mintB(-x); }
  mintB &operator+=(const mintB a) {
    if ((x += a.x) >= modB)
      x -= modB;
    return *this;
  }
  mintB &operator-=(const mintB a) {
    if ((x += modB - a.x) >= modB)
      x -= modB;
    return *this;
  }
  mintB &operator*=(const mintB a) {
    (x *= a.x) %= modB;
    return *this;
  }
  mintB operator+(const mintB a) const { return mintB(*this) += a; }
  mintB operator-(const mintB a) const { return mintB(*this) -= a; }
  mintB operator*(const mintB a) const { return mintB(*this) *= a; }
  mintB pow(ll t) const {
    if (!t)
      return 1;
    mintB a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime modB
  mintB inv() const { return pow(modB - 2); }
  mintB &operator/=(const mintB a) { return *this *= a.inv(); }
  mintB operator/(const mintB a) const { return mintB(*this) /= a; }
};
// istream& operator>>(istream& is, const mintB& a) {
//   return is >> a.x;
// }
// ostream& operator<<(ostream& os, const mintB& a) {
//   return os << a.x;
// }

signed main() {
  init();
  ll N;
  cin >> N;
  string S;
  cin >> S;

  vll b(1, 0);
  vll pop(1, 0);
  ll index = 0;
  // while (index < 100) {
  while (index < 200005) {
    vll next = b;
    rep(i, b.size()) {
      pop.pb(b[i] + 1);
      next.pb(b[i] + 1);
      index++;
    }
    b = next;
  }

  ll count = 0;
  rep(i, N) if (S[i] == '1') count++;

  REVERSE(S);
  string rS = S;

  modA = count + 1;
  modB = count - 1;
  mintA mod_a = 0;
  mintB mod_b = 0;
  rep(i, N) {
    if (rS[i] == '1') {
      mod_a += mintA(2).pow(i);
      mod_b += mintB(2).pow(i);
    }
  }

  // debug(mod_a.x);
  // debug(mod_b.x);
  // ENDL;

  vll ans;
  rep(i, N) {
    ll m = 0;
    if (rS[i] == '1') {
      mintB mb = mod_b;
      mb -= mintB(2).pow(i);
      m = mb.x;
    } else {
      mintA ma = mod_a;
      ma += mintA(2).pow(i);
      m = ma.x;
    }

    ll count = 1;
    // debug(m);
    while (m != 0) {
      m %= pop[m];
      // debug(m);
      count++;
    }
    // debug(count);
    // ENDL;
    ans.pb(count);
  }

  repr(i, N - 1) COUT(ans[i]);
  return 0;
}