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

#include <bits/stdc++.h>
typedef long long ll;
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define fore(i, b, e) for (int i = (int)b; i < (int)e; ++i)
#define forr(i, b, e) for (int i = (int)b; i < (int)e; ++i)
#define deb(x) cerr << "# " << (#x) << " = " << (x) << endl;
int faster_in() {
  int r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
const int INF = 1234567890;

ll modPow(ll b, ll e, ll mod) {
  if (mod == 0)
    return 0;
  if (e == 0)
    return 1;
  ll res = modPow(b, e >> 1, mod);
  res = res * res % mod;
  if (e & 1)
    res = b * res % mod;
  return res;
}

int steps(int n) {
  if (n == 0)
    return 0;
  return 1 + steps(n % __builtin_popcount(n));
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen("","r",stdin);
  // freopen("","w",stdout);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ones = 0;
  for (char c : s) {
    ones += (c == '1');
  }
  int mod = 0, modUp = 0, modDown = 0;
  reverse(all(s));
  fore(i, 0, n) {
    if (s[i] == '1') {
      mod += modPow(2, i, ones);
      modUp += modPow(2, i, ones + 1);
      modDown += modPow(2, i, ones - 1);
      if (ones > 0)
        mod %= ones;
      modUp %= (ones + 1);
      if (ones > 1)
        modDown %= (ones - 1);
    }
  }
  vi ans;
  fore(i, 0, n) {
    int pc = ones;
    if (s[i] == '1') {
      pc--;
      if (pc == 0) {
        ans.pb(0);
        continue;
      }
      ll val = (modDown - modPow(2, i, pc) + pc) % pc;
      ans.pb(1 + steps(val));
    } else {
      pc++;
      ll val = (modUp + modPow(2, i, pc) + pc) % pc;
      ans.pb(1 + steps(val));
    }
  }
  reverse(all(ans));
  for (int x : ans) {
    cout << x << '\\n';
  }
  return 0;
}
// Dinosaurs are cool

Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define fore(i, b, e) for (int i = (int)b; i < (int)e; ++i)
#define forr(i, b, e) for (int i = (int)b; i < (int)e; ++i)
#define deb(x) cerr << "# " << (#x) << " = " << (x) << endl;
int faster_in() {
  int r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
const int INF = 1234567890;

ll modPow(ll b, ll e, ll mod) {
  if (mod == 0)
    return 0;
  if (e == 0)
    return 1;
  ll res = modPow(b, e >> 1, mod);
  res = res * res % mod;
  if (e & 1)
    res = b * res % mod;
  return res;
}

int steps(int n) {
  if (n == 0)
    return 0;
  return 1 + steps(n % __builtin_popcount(n));
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen("","r",stdin);
  // freopen("","w",stdout);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ones = 0;
  for (char c : s) {
    ones += (c == '1');
  }
  int mod = 0, modUp = 0, modDown = 0;
  reverse(all(s));
  fore(i, 0, n) {
    if (s[i] == '1') {
      mod += modPow(2, i, ones);
      modUp += modPow(2, i, ones + 1);
      modDown += modPow(2, i, ones - 1);
      mod %= ones;
      modUp %= (ones + 1);
      modDown %= (ones - 1);
    }
  }
  vi ans;
  fore(i, 0, n) {
    int pc = ones;
    if (s[i] == '1') {
      pc--;
      if (pc == 0) {
        ans.pb(0);
        continue;
      }
      ll val = (modDown - modPow(2, i, pc) + pc) % pc;
      ans.pb(1 + steps(val));
    } else {
      pc++;
      ll val = (modUp + modPow(2, i, pc) + pc) % pc;
      ans.pb(1 + steps(val));
    }
  }
  reverse(all(ans));
  for (int x : ans) {
    cout << x << '\\n';
  }
  return 0;
}
// Dinosaurs are cool