Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(x) cout << x
#define pb push_back
#include <string.h>
ll modex(ll a, ll b, ll mod) {
  ll ans = 1;
  while (b) {
    if (b % 2)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return ans;
}
ll count(ll val) {
  ll cnt = 0;
  while (val) {
    if (val % 2)
      cnt++;
    val /= 2;
  }
  return cnt;
}
ll fun(ll val) {
  if (val == 0)
    return 0;
  return (1 + fun(val % count(val)));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = 1;
  // cin>>T;
  while (T--) {
    ll n;
    cin >> n;
    string str;
    cin >> str;
    ll val = 0, val1 = 0, val2 = 0, cnt = 0;
    for (int i = 0; i < n; i++)
      if (str[i] == '1')
        cnt++;
    for (int i = 0; i < n; i++) {
      val = (val + (int)(str[i] - '0') * modex(2, n - 1 - i, cnt)) % cnt;
      val1 = (val1 + (int)(str[i] - '0') * modex(2, n - 1 - i, cnt + 1)) %
             (cnt + 1);
      val2 = (val2 + (int)(str[i] - '0') * modex(2, n - 1 - i, cnt - 1)) %
             (cnt - 1);
    }
    for (int i = 0; i < n; i++) {
      if (str[i] == '1') {
        if ((cnt - 1) == 0) {
          cout << "0" << endl;
          continue;
        }
        ll value =
            ((((val2 - modex(2, n - 1 - i, cnt - 1)) % (cnt - 1)) + (cnt - 1)) %
             (cnt - 1));
        cout << (1 + fun(value)) << endl;
      } else {
        ll value =
            ((val1 + modex(2, n - 1 - i, cnt + 1)) + cnt + 1) % (cnt + 1);
        cout << (1 + fun(value)) << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(x) cout << x
#define pb push_back
#include <string.h>
ll modex(ll a, ll b, ll mod) {
  ll ans = 1;
  while (b) {
    if (b % 2)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return ans;
}
ll count(ll val) {
  ll cnt = 0;
  while (val) {
    if (val % 2)
      cnt++;
    val /= 2;
  }
  return cnt;
}
ll fun(ll val) {
  if (val == 0)
    return 0;
  return (1 + fun(val % count(val)));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = 1;
  // cin>>T;
  while (T--) {
    ll n;
    cin >> n;
    string str;
    cin >> str;
    ll val = 0, val1 = 0, val2 = 0, cnt = 0;
    for (int i = 0; i < n; i++)
      if (str[i] == '1')
        cnt++;
    for (int i = 0; i < n; i++) {
      val = (val + (int)(str[i] - '0') * modex(2, n - 1 - i, cnt)) % cnt;
      val1 = (val1 + (int)(str[i] - '0') * modex(2, n - 1 - i, cnt + 1)) %
             (cnt + 1);
      if (cnt > 1)
        val2 = (val2 + (int)(str[i] - '0') * modex(2, n - 1 - i, cnt - 1)) %
               (cnt - 1);
    }
    for (int i = 0; i < n; i++) {
      if (str[i] == '1') {
        if ((cnt - 1) == 0) {
          cout << "0" << endl;
          continue;
        }
        ll value =
            ((((val2 - modex(2, n - 1 - i, cnt - 1)) % (cnt - 1)) + (cnt - 1)) %
             (cnt - 1));
        cout << (1 + fun(value)) << endl;
      } else {
        ll value =
            ((val1 + modex(2, n - 1 - i, cnt + 1)) + cnt + 1) % (cnt + 1);
        cout << (1 + fun(value)) << endl;
      }
    }
  }
  return 0;
}