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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, n) for (ll i = 1; i <= ll(n); i++)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

vl get_modpow(ll m, ll n) {
  vl modpow(n);
  modpow[0] = 1;
  rep2(i, n - 1) modpow[i] = (modpow[i - 1] * 2) % m;
  return modpow;
}

vl get_f(ll n) {
  vl f(n);
  rep2(i, n - 1) {
    ll num = i, ppi = 0;
    while (num > 0) {
      if (num & 1)
        ppi++;
      num /= 2;
    }
    f[i] = f[i % ppi] + 1;
  }
  return f;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n;
  string x;
  cin >> n >> x;

  ll ppx = count(all(x), '1');

  vl f = get_f(n);

  vl modpow1, modpow2;
  modpow1 = get_modpow(ppx + 1, n);
  if (ppx >= 2)
    modpow2 = get_modpow(ppx - 1, n);

  ll r1 = 0, r2 = 0;
  rep(j, n)(r1 += modpow1[n - 1 - j] * (x[j] - '0')) %= ppx + 1;
  if (ppx >= 2)
    rep(j, n)(r2 += modpow2[n - 1 - j] * (x[j] - '0')) %= ppx - 1;

  rep(i, n) {
    string x_i = x;
    if (x[i] == '0') {
      x_i[i] = '1';
      ll r = r1;
      r += modpow1[n - 1 - i];
      r %= ppx + 1;
      cout << f[r] + 1 << newl;
    } else if (x[i] == '1' and ppx == 1) {
      cout << 0 << newl;
    } else if (x[i] == '1') {
      x_i[i] = '0';
      ll r = r2;
      r -= modpow2[n - 1 - i];
      r %= ppx - 1;
      r += ppx - 1;
      r %= ppx - 1;
      cout << f[r] + 1 << newl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, n) for (ll i = 1; i <= ll(n); i++)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

vl get_modpow(ll m, ll n) {
  vl modpow(n);
  modpow[0] = 1;
  rep2(i, n - 1) modpow[i] = (modpow[i - 1] * 2) % m;
  return modpow;
}

vl get_f(ll n) {
  vl f(n);
  rep2(i, n - 1) {
    ll num = i, ppi = 0;
    while (num > 0) {
      if (num & 1)
        ppi++;
      num /= 2;
    }
    f[i] = f[i % ppi] + 1;
  }
  return f;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n;
  string x;
  cin >> n >> x;

  ll ppx = count(all(x), '1');

  vl f = get_f(n);
  vl modpow1 = get_modpow(ppx + 1, n);
  vl modpow2 = get_modpow(ppx - 1, n);
  ll r1 = 0, r2 = 0;
  rep(j, n)(r1 += modpow1[n - 1 - j] * (x[j] - '0')) %= ppx + 1;
  rep(j, n)(r2 += modpow2[n - 1 - j] * (x[j] - '0')) %= ppx - 1;

  rep(i, n) {
    string x_i = x;
    if (x[i] == '0') {
      x_i[i] = '1';
      ll r = r1;
      r += modpow1[n - 1 - i];
      r %= ppx + 1;
      cout << f[r] + 1 << newl;
    } else if (x[i] == '1') {
      x_i[i] = '0';
      ll r = r2;
      r -= modpow2[n - 1 - i];
      r %= ppx - 1;
      r += ppx - 1;
      r %= ppx - 1;
      cout << f[r] + 1 << newl;
    }
  }
  return 0;
}