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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = long long;
ll f(ll n, ll d, ll &ans) {
  ll rem;
  if (d != 0)
    rem = n % d;
  else
    return 0;
  n = rem;

  ans++;
  if (n == 0)
    return 0;

  d = __builtin_popcount(n);
  return f(n, d, ans);
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll remender(ll arr[], ll n, ll d) {
  reverse(arr, arr + n);
  ll sum = 0;
  ll f = 1;
  for (ll i = 0; i < n; i++) {
    sum = (sum + (arr[i] * f) % d) % d;
    f = (f * 2) % d;
  }
  return sum;
}
int main() {
  ll n;
  cin >> n;
  ll arr[n];
  char brr[n];
  ll cnt1 = 0;
  for (ll i = 0; i < n; i++) {
    cin >> brr[i];
    arr[i] = brr[i] - '0';
    if (arr[i] == 1)
      cnt1++;
  }
  int g = 0;
  ll rem0 = 0, rem1 = 0, rem2 = 0;
  if (cnt1 > 1)
    rem0 = remender(arr, n, cnt1 - 1), g++;
  if (cnt1 > 0)
    rem1 = remender(arr, n, cnt1), g++;

  if (cnt1 + 1 > 0)
    rem2 = remender(arr, n, cnt1 + 1), g++;
  if (g % 2 == 1)
    reverse(arr, arr + n);
  for (ll i = 0; i < n; i++) {
    ll ans = 0;
    if (arr[i] == 0) {
      ll d = cnt1 + 1;

      //   cout << "f " << rem2 << endl;
      //  cout <<"g " << i << " " << ( rem2 + binpow(2,n - i - 1,d))%d << endl;
      if (d > 0)
        f((rem2 + binpow(2, n - i - 1, d)) % d, d, ans);
      // cout << ans << endl;
    } else {
      ll d = cnt1 - 1;
      if (d > 0)
        f((rem0 + d - binpow(2, n - i - 1, d)) % d, d, ans);
      // cout << ans << endl;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = long long;
ll f(ll n, ll d, ll &ans) {
  ll rem;
  if (d != 0)
    rem = n % d;
  else
    return 0;
  n = rem;

  ans++;
  if (n == 0)
    return 0;

  d = __builtin_popcount(n);
  return f(n, d, ans);
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll remender(ll arr[], ll n, ll d) {
  reverse(arr, arr + n);
  ll sum = 0;
  ll f = 1;
  for (ll i = 0; i < n; i++) {
    sum = (sum + (arr[i] * f) % d) % d;
    f = (f * 2) % d;
  }
  return sum;
}
int main() {
  ll n;
  cin >> n;
  ll arr[n];
  char brr[n];
  ll cnt1 = 0;
  for (ll i = 0; i < n; i++) {
    cin >> brr[i];
    arr[i] = brr[i] - '0';
    if (arr[i] == 1)
      cnt1++;
  }

  ll rem0 = 0, rem1 = 0, rem2 = 0;
  // if(cnt1 > 1)
  rem0 = remender(arr, n, cnt1 - 1);
  // if(cnt1 > 0)
  rem1 = remender(arr, n, cnt1);

  // if(cnt1 + 1 >0)
  rem2 = remender(arr, n, cnt1 + 1);
  reverse(arr, arr + n);
  for (ll i = 0; i < n; i++) {
    ll ans = 0;
    if (arr[i] == 0) {
      ll d = cnt1 + 1;

      //   cout << "f " << rem2 << endl;
      //  cout <<"g " << i << " " << ( rem2 + binpow(2,n - i - 1,d))%d << endl;
      if (d > 0)
        f((rem2 + binpow(2, n - i - 1, d)) % d, d, ans);
      // cout << ans << endl;
    } else {
      ll d = cnt1 - 1;
      if (d > 0)
        f((rem0 + d - binpow(2, n - i - 1, d)) % d, d, ans);
      // cout << ans << endl;
    }
    cout << ans << endl;
  }

  return 0;
}