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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg, (x).ed
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (ll i = (n); i >= 1; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll maxx(ll x, ll y, ll z) { return max(max(x, y), z); }
ll minn(ll x, ll y, ll z) { return min(min(x, y), z); }
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * (y / gcd(x, y)); }
ll digsz(ll x) {
  if (x == 0)
    return 1;
  else {
    ll ans = 0;
    while (x) {
      x /= 10;
      ans++;
    }
    return ans;
  }
}
ll digsum(ll x) {
  ll sum = 0;
  while (x) {
    sum += x % 10;
    x /= 10;
  }
  return sum;
}
vector<ll> pw2(62, 1);
vector<ll> pw10(19, 1);
// 2の進数表示の最高位
// 例: 7->2 8->3
ll high_order_bit(ll n) {
  ll tmp = 1, i = -1;
  while (tmp <= n) {
    tmp *= 2;
    i++;
  }
  if (n == 0)
    return 0;
  else
    return i;
}
// 6 -> vec[0] [1] [2]
//          0   1   1
// vec[i]:2^iの位
vector<ll> binary_expansion(ll n) {
  vector<ll> B(high_order_bit(n) + 1);
  ll tmp = n;
  for (ll i = high_order_bit(n); i >= 0; i--) {
    B[i] = tmp / pw2[i];
    tmp %= pw2[i];
  }
  return B;
}
ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  { rep1(i, 61) pw2[i] = 2 * pw2[i - 1]; }
  { rep1(i, 18) pw10[i] = 10 * pw10[i - 1]; }

  ll N;
  cin >> N;
  string X;
  cin >> X;
  ll cnt = 0;
  rep(i, sz(X)) {
    if (X[i] == '1')
      cnt++;
  }

  // cnt-1のあまり
  if (cnt == 0) {
    rep(i, N) cout << 1 << endl;
  } else if (cnt == 1) {
    ll rem = 0;
    rep(i, N) {
      if (X[i] == '1')
        rem = i;
    }
    rep(i, N) {
      if (i == rem)
        cout << 0 << endl;
      else {
        if (i == N - 1 || rem == N - 1)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
  } else {
    ll M_1 = 0, M_2 = 0;
    ll tmp = 0;
    rep(i, N) {
      if (X[i] == '1') {
        tmp += mod_pow(2, N - 1 - i, cnt - 1);
        tmp %= cnt - 1;
      }
    }
    M_1 = tmp;
    // cnt+1のあまり
    ll tmp2 = 0;
    rep(i, N) {
      if (X[i] == '1') {
        tmp2 += mod_pow(2, N - 1 - i, cnt + 1);
        tmp2 %= cnt + 1;
      }
    }
    M_2 = tmp2;

    rep(i, N) {
      if (X[i] == '1') {
        ll ans = M_1;
        ans -= mod_pow(2, N - 1 - i, cnt - 1);
        if (ans < 0)
          ans += cnt - 1;

        ll play = 1;
        while (ans != 0) {
          // ansにやる
          vector<ll> v = binary_expansion(ans);
          ll cnt1 = 0;
          rep(j, sz(v)) if (v[j] == 1) cnt1++;
          ans %= cnt1;
          play++;
        }
        cout << play << endl;
      } else if (X[i] == '0') {
        ll ans = M_2;
        ans += mod_pow(2, N - 1 - i, cnt + 1);
        ans %= cnt + 1;

        ll play = 1;
        while (ans != 0) {
          // ansにやる
          vector<ll> v = binary_expansion(ans);
          ll cnt1 = 0;
          rep(j, sz(v)) if (v[j] == 1) cnt1++;
          ans %= cnt1;
          play++;
        }
        cout << play << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg, (x).ed
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (ll i = (n); i >= 1; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll maxx(ll x, ll y, ll z) { return max(max(x, y), z); }
ll minn(ll x, ll y, ll z) { return min(min(x, y), z); }
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * (y / gcd(x, y)); }
ll digsz(ll x) {
  if (x == 0)
    return 1;
  else {
    ll ans = 0;
    while (x) {
      x /= 10;
      ans++;
    }
    return ans;
  }
}
ll digsum(ll x) {
  ll sum = 0;
  while (x) {
    sum += x % 10;
    x /= 10;
  }
  return sum;
}
vector<ll> pw2(62, 1);
vector<ll> pw10(19, 1);
// 2の進数表示の最高位
// 例: 7->2 8->3
ll high_order_bit(ll n) {
  ll tmp = 1, i = -1;
  while (tmp <= n) {
    tmp *= 2;
    i++;
  }
  if (n == 0)
    return 0;
  else
    return i;
}
// 6 -> vec[0] [1] [2]
//          0   1   1
// vec[i]:2^iの位
vector<ll> binary_expansion(ll n) {
  vector<ll> B(high_order_bit(n) + 1);
  ll tmp = n;
  for (ll i = high_order_bit(n); i >= 0; i--) {
    B[i] = tmp / pw2[i];
    tmp %= pw2[i];
  }
  return B;
}
ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  { rep1(i, 61) pw2[i] = 2 * pw2[i - 1]; }
  { rep1(i, 18) pw10[i] = 10 * pw10[i - 1]; }

  ll N;
  cin >> N;
  string X;
  cin >> X;
  ll cnt = 0;
  rep(i, sz(X)) {
    if (X[i] == '1')
      cnt++;
  }

  ll M_1 = INF, M_2 = 0;
  // cnt-1のあまり
  if (cnt == 0) {
    rep(i, N) cout << 1 << endl;
  } else if (cnt == 1) {
    ll rem = 0;
    rep(i, N) {
      if (X[i] == '1')
        rem = i;
    }
    rep(i, N) {
      if (i == rem)
        cout << 0 << endl;
      else {
        if (rem == N - 1)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
  }
  ll tmp = 0;
  rep(i, N) {
    if (X[i] == '1') {
      tmp += mod_pow(2, N - 1 - i, cnt - 1);
      tmp %= cnt - 1;
    }
  }
  M_1 = tmp;
  // cnt+1のあまり
  ll tmp2 = 0;
  rep(i, N) {
    if (X[i] == '1') {
      tmp2 += mod_pow(2, N - 1 - i, cnt + 1);
      tmp2 %= cnt + 1;
    }
  }
  M_2 = tmp2;

  rep(i, N) {
    if (cnt == 1 && X[i] == '1')
      cout << 1 << endl;
    else if (X[i] == '1') {
      ll ans = M_1;
      ans -= mod_pow(2, N - 1 - i, cnt - 1);
      if (ans < 0)
        ans += cnt - 1;

      ll play = 1;
      while (ans != 0) {
        // ansにやる
        vector<ll> v = binary_expansion(ans);
        ll cnt1 = 0;
        rep(j, sz(v)) if (v[j] == 1) cnt1++;
        ans %= cnt1;
        play++;
      }
      cout << play << endl;
    } else if (X[i] == '0') {
      ll ans = M_2;
      ans += mod_pow(2, N - 1 - i, cnt + 1);
      ans %= cnt + 1;

      ll play = 1;
      while (ans != 0) {
        // ansにやる
        vector<ll> v = binary_expansion(ans);
        ll cnt1 = 0;
        rep(j, sz(v)) if (v[j] == 1) cnt1++;
        ans %= cnt1;
        play++;
      }
      cout << play << endl;
    }
  }
}