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

#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} fast;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
// long long mod = 998244353;
#define REP(i, n) for (long long i = 0; i < (n); i++)
#define rep(i, a, n) for (long long i = a; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
long long mod = 1000000007;
long long modpow(long long m, long long n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    long long t = modpow(m, n / 2);
    return t * t % mod;
  } else {
    return modpow(m, n - 1) * m % mod;
  }
}
long long power(long long m, long long n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    long long t = modpow(m, n / 2);
    return t * t;
  } else {
    return modpow(m, n - 1) * m;
  }
}
long long moddiv(long long a, long long b) {
  return (a * modpow(b, mod - 2)) % mod;
}
const ll INF = 1LL << 60;
string change_binary(long long a) {
  string ret;
  for (int i = 31; i >= 0; i--) {
    ret += (a >> i & 1) + '0';
  }
  return ret;
}
ll nCkmod(ll n, ll k) {
  ll denomi = 1;
  for (int i = n; i > n - k; i--) {
    denomi *= i;
    denomi %= mod;
  }
  ll numera = 1;
  REP(i, k) {
    numera *= (i + 1);
    numera %= mod;
  }
  return moddiv(denomi, numera);
}
vector<vector<ll>> graph;
vector<ll> as;
map<P, ll> number;
ll rmemo[101][300001];
ll memo[101][300001];
ll func(string x, ll pc) {
  if (pc == 0)
    return 0;
  ll nowmod = 1;
  ll ret = 0;
  for (int i = x.size() - 1; 0 <= i; i--) {
    if (x[i] == '1') {
      ret += nowmod;
      ret %= pc;
    }
    nowmod = (nowmod * 2) % pc;
  }
  return ret;
}
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popc[2] = {0, 0};
  REP(i, n) {
    if (s[i] == '1') {
      popc[0]++;
      popc[1]++;
    }
  }
  popc[0]++;
  popc[1]--;
  ll smod[2];
  smod[0] = func(s, popc[0]);
  smod[1] = func(s, popc[1]);
  ll curmod[2] = {1, 1};
  vector<ll> ans(n);
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (popc[1] == 0) {
        ans[i] = 0;
      } else {
        ll cur = (smod[1] - curmod[1] + popc[1]) % popc[1];
        ll steps = 0;
        while (cur != 0) {
          steps++;
          cur = func(change_binary(cur), __builtin_popcountll(cur));
        }
        ans[i] = steps + 1;
      }
    }
    if (s[i] == '0') {
      ll cur = (smod[0] + curmod[0] + popc[0]) % popc[0];
      ll steps = 0;
      while (cur != 0) {
        steps++;
        cur = func(change_binary(cur), __builtin_popcountll(cur));
      }
      ans[i] = steps + 1;
    }
    curmod[0] = (curmod[0] * 2) % popc[0];
    if (popc[1] != 0)
      curmod[1] = (curmod[1] * 2) % popc[1];
  }

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

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} fast;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
// long long mod = 998244353;
#define REP(i, n) for (long long i = 0; i < (n); i++)
#define rep(i, a, n) for (long long i = a; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
long long mod = 1000000007;
long long modpow(long long m, long long n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    long long t = modpow(m, n / 2);
    return t * t % mod;
  } else {
    return modpow(m, n - 1) * m % mod;
  }
}
long long power(long long m, long long n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    long long t = modpow(m, n / 2);
    return t * t;
  } else {
    return modpow(m, n - 1) * m;
  }
}
long long moddiv(long long a, long long b) {
  return (a * modpow(b, mod - 2)) % mod;
}
const ll INF = 1LL << 60;
string change_binary(long long a) {
  string ret;
  for (int i = 31; i >= 0; i--) {
    ret += (a >> i & 1) + '0';
  }
  return ret;
}
ll nCkmod(ll n, ll k) {
  ll denomi = 1;
  for (int i = n; i > n - k; i--) {
    denomi *= i;
    denomi %= mod;
  }
  ll numera = 1;
  REP(i, k) {
    numera *= (i + 1);
    numera %= mod;
  }
  return moddiv(denomi, numera);
}
vector<vector<ll>> graph;
vector<ll> as;
map<P, ll> number;
ll rmemo[101][300001];
ll memo[101][300001];
ll func(string x, ll pc) {
  ll nowmod = 1;
  ll ret = 0;
  for (int i = x.size() - 1; 0 <= i; i--) {
    if (x[i] == '1') {
      ret += nowmod;
      ret %= pc;
    }
    nowmod = (nowmod * 2) % pc;
  }
  return ret;
}
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll popc[2] = {0, 0};
  REP(i, n) {
    if (s[i] == '1') {
      popc[0]++;
      popc[1]++;
    }
  }
  popc[0]++;
  popc[1]--;
  ll smod[2];
  smod[0] = func(s, popc[0]);
  smod[1] = func(s, popc[1]);
  ll curmod[2] = {1 % popc[0], 1 % popc[1]};
  vector<ll> ans(n);
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ll cur = (smod[1] - curmod[1] + popc[1]) % popc[1];
      ll steps = 0;
      while (cur != 0) {
        steps++;
        cur = func(change_binary(cur), __builtin_popcountll(cur));
      }
      if (popc[1] == 0) {
        ans[i] = 0;
      } else {
        ans[i] = steps + 1;
      }
    }
    if (s[i] == '0') {
      ll cur = (smod[0] + curmod[0] + popc[0]) % popc[0];
      ll steps = 0;
      while (cur != 0) {
        steps++;
        cur = func(change_binary(cur), __builtin_popcountll(cur));
      }
      ans[i] = steps + 1;
    }
    curmod[0] = (curmod[0] * 2) % popc[0];
    curmod[1] = (curmod[1] * 2) % popc[1];
  }

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