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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)

ll INF = 1e9;
ll NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     // 最小公倍数

void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }

//-----------------------------------------
ll num(ll x) {
  ll res = 0;
  while (x > 0) {
    if (x % 2 == 1) {
      res++;
    }
    x /= 2;
  }
  return res;
}

long long power_mod(long long x, long long y, long long m) {
  long long res = 1;
  if (y > 0) {
    res = power_mod(x, y / 2, m);
    if (y % 2 == 0) {
      res = (res * res) % m;
    } else {
      res = (((res * res) % m) * x) % m;
    }
  }
  return res;
}
//-----------------------------------------
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  string s;
  cin >> n >> s;
  reverse(ALL(s));

  ll m = 0;
  REP(i, n) {
    if (s[i] == '1') {
      m++;
    }
  }

  vll mp(n, 0);
  vll pp(n, 0);

  REP(i, n) {
    if (m != 1) {
      mp[i] = power_mod(2, i, m - 1);
    }
    pp[i] = power_mod(2, i, m + 1);
  }

  ll a = 0;
  ll b = 0;

  REP(i, n) {
    if (s[i] == '1') {
      if (m != 1) {
        a += mp[i];
        a %= (m - 1);
      }
      b += pp[i];
      b %= (m + 1);
    }
  }

  vll vec;

  ll hoge, ans;

  REP(i, n) {
    if (s[i] == '0') {
      hoge = b;
      hoge += pp[i];
      hoge %= (m + 1);
      ans = 1;
      while (hoge > 0) {
        ans++;
        hoge %= num(hoge);
      }
      vec.PB(ans);
    } else {
      if (m == 1) {
        vec.PB(0);
        continue;
      }
      hoge = a;
      hoge -= mp[i];
      hoge += (m - 1);
      hoge %= (m - 1);
      ans = 1;
      while (hoge > 0) {
        ans++;
        hoge %= num(hoge);
      }
      vec.PB(ans);
    }
  }

  reverse(ALL(vec));

  REP(i, n) { cout << vec[i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)

ll INF = 1e9;
ll NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     // 最小公倍数

void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }

//-----------------------------------------
ll num(ll x) {
  ll res = 0;
  while (x > 0) {
    if (x % 2 == 1) {
      res++;
    }
    x /= 2;
  }
  return res;
}

long long power_mod(long long x, long long y, long long m) {
  long long res = 1;
  if (y > 0) {
    res = power_mod(x, y / 2, m);
    if (y % 2 == 0) {
      res = (res * res) % m;
    } else {
      res = (((res * res) % m) * x) % m;
    }
  }
  return res;
}
//-----------------------------------------
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  string s;
  cin >> n >> s;
  reverse(ALL(s));

  ll m = 0;
  REP(i, n) {
    if (s[i] == '1') {
      m++;
    }
  }

  vll mp(n, 0);
  vll pp(n, 0);

  REP(i, n) {
    if (m != 1) {
      mp[i] = power_mod(2, i, m - 1);
    }
    pp[i] = power_mod(2, i, m + 1);
  }

  ll a = 0;
  ll b = 0;

  REP(i, n) {
    if (s[i] == '1') {
      if (m != 1) {
        a += mp[i];
        a %= (m - 1);
      }
      b += pp[i];
      b %= (m + 1);
    }
  }

  vll vec;

  ll hoge, ans;

  REP(i, n) {
    if (s[i] == '0') {
      hoge = b;
      hoge += pp[i];
      hoge %= (m + 1);
      ans = 1;
      while (hoge > 0) {
        ans++;
        hoge %= num(hoge);
      }
      vec.PB(ans);
    } else {
      hoge = a;
      hoge -= mp[i];
      hoge += (m - 1);
      hoge %= (m - 1);
      ans = 1;
      while (hoge > 0) {
        ans++;
        hoge %= num(hoge);
      }
      vec.PB(ans);
    }
  }

  reverse(ALL(vec));

  REP(i, n) { cout << vec[i] << endl; }

  return 0;
}