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

#ifdef _MSC_VER
#include "stdc++.h"
#endif
#ifdef __GNUC__
#include <bits/stdc++.h>
#endif
using namespace std;

using ll = long long;
ll MOD = 1e7;

// vector
using vi = vector<int>;
using vii = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;

// constant
const double EPS = 1e-10;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, s, e) for (ll i = s; i < e; i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

// vector
// template<typename T> vector<vector<T>> vv(ll H, ll W, T v) { return
// vector<vector<T>> vec(H, vector<T>(W, v)); }

// view vector
template <typename T> void view(T e) { cout << e << endl; }
template <typename T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <typename T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

// DP
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;
}

struct Mint {
  ll v;
  ll _mod;
  Mint() : v(0), _mod(MOD) {}
  Mint(signed v, ll _mod = MOD) : v(v), _mod(_mod) {}
  Mint(ll t, ll _mod = MOD) : _mod(_mod) {
    v = t % _mod;
    if (v < 0)
      v += _mod;
  }

  Mint pow(ll k) {
    Mint res(1, _mod), tmp(v, _mod);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }
  static Mint add_identity(ll mod = MOD) { return Mint(0, mod); }
  static Mint mul_identity(ll mod = MOD) { return Mint(1, mod); }
  Mint inv() { return pow(_mod - 2); }

  Mint &operator+=(Mint a) {
    v += a.v;
    if (v >= _mod)
      v -= _mod;
    return *this;
  }
  Mint &operator-=(Mint a) {
    v += _mod - a.v;
    if (v >= _mod)
      v -= _mod;
    return *this;
  }
  Mint &operator*=(Mint a) {
    v = v * a.v % _mod;
    return *this;
  }
  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(v, _mod) += a; };
  Mint operator-(Mint a) const { return Mint(v, _mod) -= a; };
  Mint operator*(Mint a) const { return Mint(v, _mod) *= a; };
  Mint operator/(Mint a) const { return Mint(v, _mod) /= a; };

  Mint operator-() const { return v ? Mint(_mod - v, _mod) : Mint(v, _mod); }

  bool operator==(const Mint a) const { return v == a.v; }
  bool operator!=(const Mint a) const { return v != a.v; }
  bool operator<(const Mint a) const { return v < a.v; }
};
ostream &operator<<(ostream &os, Mint m) { return os << m.v; }

// ***************************************

ll pc(ll x) {
  ll cnt = 0;
  while (x > 0) {
    if (x & 1)
      cnt++;
    x = x >> 1;
  }

  return cnt;
}

ll f(Mint X) {
  if (X.v == 0)
    return 1;

  return f(Mint(X.v, pc(X.v))) + 1;
}

int main() {
  ll N;
  cin >> N;

  string S;
  cin >> S;

  ll n2 = 0;
  for (char c : S) {
    if (c == '1')
      n2++;
  }

  Mint Xp(0, n2 + 1), Xm(0, max<ll>(1, n2 - 1));
  REP(i, N) {
    if (S[i] == '1') {
      Xp += Mint(2, n2 + 1).pow(N - i - 1);
      Xm += Mint(2, max<ll>(1, n2 - 1)).pow(N - i - 1);
    }
  }

  REP(i, N) {
    Mint X;
    if (S[i] == '1') {
      if (n2 == 1) {
        cout << 0 << endl;
        continue;
      }
      X = Xm - Mint(2, n2 - 1).pow(N - i - 1);
    } else {
      X = Xp + Mint(2, n2 + 1).pow(N - i - 1);
    }

    ll ans = f(X);
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#ifdef _MSC_VER
#include "stdc++.h"
#endif
#ifdef __GNUC__
#include <bits/stdc++.h>
#endif
using namespace std;

using ll = long long;
ll MOD = 1e7;

// vector
using vi = vector<int>;
using vii = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;

// constant
const double EPS = 1e-10;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, s, e) for (ll i = s; i < e; i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

// vector
// template<typename T> vector<vector<T>> vv(ll H, ll W, T v) { return
// vector<vector<T>> vec(H, vector<T>(W, v)); }

// view vector
template <typename T> void view(T e) { cout << e << endl; }
template <typename T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <typename T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

// DP
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;
}

struct Mint {
  ll v;
  ll _mod;
  Mint() : v(0), _mod(MOD) {}
  Mint(signed v, ll _mod = MOD) : v(v), _mod(_mod) {}
  Mint(ll t, ll _mod = MOD) : _mod(_mod) {
    v = t % _mod;
    if (v < 0)
      v += _mod;
  }

  Mint pow(ll k) {
    Mint res(1, _mod), tmp(v, _mod);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }
  static Mint add_identity(ll mod = MOD) { return Mint(0, mod); }
  static Mint mul_identity(ll mod = MOD) { return Mint(1, mod); }
  Mint inv() { return pow(_mod - 2); }

  Mint &operator+=(Mint a) {
    v += a.v;
    if (v >= _mod)
      v -= _mod;
    return *this;
  }
  Mint &operator-=(Mint a) {
    v += _mod - a.v;
    if (v >= _mod)
      v -= _mod;
    return *this;
  }
  Mint &operator*=(Mint a) {
    v = v * a.v % _mod;
    return *this;
  }
  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(v, _mod) += a; };
  Mint operator-(Mint a) const { return Mint(v, _mod) -= a; };
  Mint operator*(Mint a) const { return Mint(v, _mod) *= a; };
  Mint operator/(Mint a) const { return Mint(v, _mod) /= a; };

  Mint operator-() const { return v ? Mint(_mod - v, _mod) : Mint(v, _mod); }

  bool operator==(const Mint a) const { return v == a.v; }
  bool operator!=(const Mint a) const { return v != a.v; }
  bool operator<(const Mint a) const { return v < a.v; }
};
ostream &operator<<(ostream &os, Mint m) { return os << m.v; }

// ***************************************

ll pc(ll x) {
  ll cnt = 0;
  while (x > 0) {
    if (x & 1)
      cnt++;
    x = x >> 1;
  }

  return cnt;
}

ll f(Mint X) {
  if (X.v == 0)
    return 1;

  return f(Mint(X.v, pc(X.v))) + 1;
}

int main() {
  ll N;
  cin >> N;

  string S;
  cin >> S;

  ll n2 = 0;
  for (char c : S) {
    if (c == '1')
      n2++;
  }

  Mint Xp(0, n2 + 1), Xm(0, n2 - 1);
  REP(i, N) {
    if (S[i] == '1') {
      Xp += Mint(2, n2 + 1).pow(N - i - 1);
      Xm += Mint(2, n2 - 1).pow(N - i - 1);
    }
  }

  REP(i, N) {
    Mint X;
    if (S[i] == '1') {
      X = Xm - Mint(2, n2 - 1).pow(N - i - 1);
    } else {
      X = Xp + Mint(2, n2 + 1).pow(N - i - 1);
    }

    if (X._mod == 0) {
      cout << 0 << endl;
      continue;
    }

    ll ans = f(X);
    cout << ans << endl;
  }

  return 0;
}