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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template <class T> using VE = vector<T>;
template <class T> using P = pair<T, T>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, k, n) for (int i = (k); i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define output(x, y) cout << fixed << setprecision(y) << x << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
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; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
ll two[202020];
ll two2[202020];
ll two3[202020];
ll popcount(ll x) {
  int res = 0;
  for (; x > 0; x >>= 1)
    res += (x & 1);
  return res;
}
ll modpow(string s, int mod) {
  ll res = 0;
  ll now = 1;
  rep(i, s.size()) {
    if (s[i] == '1')
      res = res + now % mod;
    now = now * 2 % mod;
  }
  return res;
}
ll rem(ll x, int s) {
  if (x == 0)
    return s;
  ll nxt = popcount(x);
  if (nxt == 0)
    return s;
  return rem(x % nxt, s + 1);
}
int main() {
  two[0] = 1;
  two2[0] = 1;
  two3[0] = 1;
  int n;
  string s;
  cin >> n >> s;
  reverse(all(s));
  int ppcnt = 0;
  rep(i, n) { ppcnt += s[i] - '0'; }
  if (ppcnt == 0) {
    rep(i, n) { cout << 1 << endl; }
    return 0;
  } else if (ppcnt == 1) {
    V ans(n);
    rep(i, n) {
      if (s[i] == '1')
        ans[i] = 0;
      else {
        if (s[0] == '1')
          ans[i] = 2;
        else {
          if (i == 0)
            ans[i] = 2;
          else
            ans[i] = 1;
        }
      }
    }
    reverse(all(ans));
    rep(i, n) cout << ans[i] << endl;
    return 0;
  }
  rep1(i, 200005) two[i] = (two[i - 1] * 2) % ppcnt;
  rep1(i, 200005) two2[i] = (two2[i - 1] * 2) % (ppcnt + 1);
  if (ppcnt != 1) {
    rep1(i, 200005) two3[i] = (two3[i - 1] * 2) % (ppcnt - 1);
  } else {
    rep1(i, 200005) two3[i] = -1;
  }
  ll x = modpow(s, ppcnt);
  ll x2 = modpow(s, ppcnt + 1);
  ll x3 = (ppcnt != 0) ? modpow(s, ppcnt - 1) : -1;
  // 割り算はlog回で収束しそう
  V ans(n);
  rep(i, n) {
    int pc = ppcnt;
    if (s[i] == '0') {
      pc++;
      if (pc == 1) {
        ans[i] = 1;
        continue;
      }
      ll tmp = (x2 + two2[i]) % pc;
      if (tmp == 0)
        ans[i] = 1;
      else
        ans[i] = rem(tmp, 1);
    } else {
      pc--;
      if (pc == 1) {
        ans[i] = 1;
        continue;
      } else if (pc == 0) {
        ans[i] = 0;
        continue;
      }
      ll tmp = (x3 - two3[i] + pc) % pc;
      if (tmp == 0)
        ans[i] = 1;
      else
        ans[i] = rem(tmp, 1);
    }
  }
  reverse(all(ans));
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template <class T> using VE = vector<T>;
template <class T> using P = pair<T, T>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, k, n) for (int i = (k); i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define output(x, y) cout << fixed << setprecision(y) << x << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
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; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
ll two[202020];
ll two2[202020];
ll two3[202020];
ll popcount(ll x) {
  int res = 0;
  for (; x > 0; x >>= 1)
    res += (x & 1);
  return res;
}
ll modpow(string s, int mod) {
  ll res = 0;
  ll now = 1;
  rep(i, s.size()) {
    if (s[i] == '1')
      res = res + now % mod;
    now = now * 2 % mod;
  }
  return res;
}
ll rem(ll x, int s) {
  if (x == 0)
    return s;
  ll nxt = popcount(x);
  if (nxt == 0)
    return s;
  return rem(x % nxt, s + 1);
}
int main() {
  two[0] = 1;
  two2[0] = 1;
  two3[0] = 1;
  int n;
  string s;
  cin >> n >> s;
  reverse(all(s));
  int ppcnt = 0;
  rep(i, n) { ppcnt += s[i] - '0'; }
  if (ppcnt == 0) {
    rep(i, n) { cout << 1 << endl; }
    return 0;
  }
  rep1(i, 200005) two[i] = (two[i - 1] * 2) % ppcnt;
  rep1(i, 200005) two2[i] = (two2[i - 1] * 2) % (ppcnt + 1);
  if (ppcnt != 1) {
    rep1(i, 200005) two3[i] = (two3[i - 1] * 2) % (ppcnt - 1);
  } else {
    rep1(i, 200005) two3[i] = -1;
  }
  ll x = modpow(s, ppcnt);
  ll x2 = modpow(s, ppcnt + 1);
  ll x3 = (ppcnt != 0) ? modpow(s, ppcnt - 1) : -1;
  // 割り算はlog回で収束しそう
  V ans(n);
  rep(i, n) {
    int pc = ppcnt;
    if (s[i] == '0') {
      pc++;
      if (pc == 1) {
        ans[i] = 1;
        continue;
      }
      ll tmp = (x2 + two2[i]) % pc;
      if (tmp == 0)
        ans[i] = 1;
      else
        ans[i] = rem(tmp, 1);
    } else {
      pc--;
      if (pc == 1) {
        ans[i] = 1;
        continue;
      } else if (pc == 0) {
        ans[i] = 0;
        continue;
      }
      ll tmp = (x3 - two3[i] + pc) % pc;
      if (tmp == 0)
        ans[i] = 1;
      else
        ans[i] = rem(tmp, 1);
    }
  }
  reverse(all(ans));
  rep(i, n) cout << ans[i] << endl;
  return 0;
}