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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<vector<vector<int>>> v3int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef vector<vector<vector<ll>>> v3ll;
typedef list<int> liint;
typedef pair<int, int> pint;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ll, int>> vpll_int;
typedef vector<pair<int, ll>> vpint_ll;
typedef set<pair<int, int>> spint;
typedef set<pair<ll, int>> spll;
typedef unordered_map<int, unordered_set<int>> Graph;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

int pop_count(string &S) {
  int ret = 0;
  rep(i, S.size()) {
    if (S[i] == '1')
      ret++;
  }
  return ret;
}

ll g(string &S) {
  int p = pop_count(S);
  ll ret = 0;
  ll twopow = 1;
  string Srev = S;
  reverse(Srev.begin(), Srev.end());
  rep(i, S.size()) {
    if (Srev[i] == '1') {
      ret += twopow;
      ret %= p;
      twopow *= 2;
      twopow %= p;
    }
  }
  return ret;
}
long long mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0)
    res += m;
  return res;
}

string bin(ll x) {
  string ans;
  while (x > 1) {
    ans += char(x % 2 + 0x30);
    x /= 2;
  }
  ans += char(x + 0x30);
  reverse(ans.begin(), ans.end());
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N;
  cin >> N;
  string S;
  cin >> S;
  vll vtwopow(N);
  vll vtwopowp1(N);
  vll vtwopowm1(N);
  ll two = 1;

  ll p = pop_count(S);

  two = 1;
  if ((p - 1) != 0) {
    rep(i, N) {
      vtwopowm1[i] = two;
      two *= 2;

      two %= (p - 1);
    }
  }
  two = 1;
  rep(i, N) {
    vtwopowp1[i] = two;
    two *= 2;
    two %= (p + 1);
  }
  ll orip1 = 0, orim1 = 0;

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

  rep(i, N) {

    if (S[i] == '1') {
      orip1 += vtwopowp1[i];
      orip1 %= (p + 1);
      if ((p - 1) != 0) {
        orim1 += vtwopowm1[i];
        orim1 %= (p - 1);
      }
    }
  }

  vint vans(N);
  rep(i, N) {
    ll x = 0;

    ll ans = 1;
    if (S[i] == '0') {
      x = (orip1 + vtwopowp1[i]) % (p + 1);
    } else {
      if (p - 1 == 0) {
        ans = 0;
        vans[i] = ans;
        continue;
      }
      x = mod(orim1 - vtwopowm1[i], p - 1);
    }

    while (x > 0) {
      ans++;
      auto s = bin(x);
      int t = pop_count(s);
      x %= t;
    }
    vans[i] = ans;
  }

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

  rep(i, N) cout << vans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<vector<vector<int>>> v3int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef vector<vector<vector<ll>>> v3ll;
typedef list<int> liint;
typedef pair<int, int> pint;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ll, int>> vpll_int;
typedef vector<pair<int, ll>> vpint_ll;
typedef set<pair<int, int>> spint;
typedef set<pair<ll, int>> spll;
typedef unordered_map<int, unordered_set<int>> Graph;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

int pop_count(string &S) {
  int ret = 0;
  rep(i, S.size()) {
    if (S[i] == '1')
      ret++;
  }
  return ret;
}

ll g(string &S) {
  int p = pop_count(S);
  ll ret = 0;
  ll twopow = 1;
  string Srev = S;
  reverse(Srev.begin(), Srev.end());
  rep(i, S.size()) {
    if (Srev[i] == '1') {
      ret += twopow;
      ret %= p;
      twopow *= 2;
      twopow %= p;
    }
  }
  return ret;
}
long long mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0)
    res += m;
  return res;
}

string bin(ll x) {
  string ans;
  while (x > 1) {
    ans += char(x % 2 + 0x30);
    x /= 2;
  }
  ans += char(x + 0x30);
  reverse(ans.begin(), ans.end());
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N;
  cin >> N;
  string S;
  cin >> S;
  vll vtwopow(N);
  vll vtwopowp1(N);
  vll vtwopowm1(N);
  ll two = 1;

  ll p = pop_count(S);
  rep(i, N) {
    vtwopow[i] = two;
    two *= 2 % (p);
  }
  two = 1;
  rep(i, N) {
    vtwopowm1[i] = two;
    two *= 2;
    two %= (p - 1);
  }
  two = 1;
  rep(i, N) {
    vtwopowp1[i] = two;
    two *= 2;
    two %= (p + 1);
  }
  ll orip1 = 0, orim1 = 0;

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

  rep(i, N) {

    if (S[i] == '1') {
      orip1 += vtwopowp1[i];
      orip1 %= (p + 1);
      orim1 += vtwopowm1[i];
      orim1 %= (p - 1);
    }
  }

  vint vans(N);
  rep(i, N) {
    ll x = 0;

    ll ans = 1;
    if (S[i] == '0') {
      x = (orip1 + vtwopowp1[i]) % (p + 1);
    } else {
      if (p - 1 == 0) {
        ans = 0;
        vans[i] = ans;
        continue;
      }
      x = mod(orim1 - vtwopowm1[i], p - 1);
    }

    while (x > 0) {
      ans++;
      auto s = bin(x);
      int t = pop_count(s);
      x %= t;
    }
    vans[i] = ans;
  }

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

  rep(i, N) cout << vans[i] << endl;
  return 0;
}