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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 1000000000000000000, mod = 1000000007, BS, k;
ll power(ll a, ll b) {
  if (a == 0)
    return 1;
  ll res = 1, curr = 2;
  while (a) {
    if (a & 1)
      res = (res * curr) % b;
    curr = (curr * curr) % b;
    a /= 2;
  }
  return res;
}
ll fun(ll num) {
  if (num == 0)
    return 0;
  ll coun, res = 0;
  while (num) {
    coun = __builtin_popcount(num);
    if (coun == 0)
      break;
    res++;
    num %= coun;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll sum = 0;
  for (ll i = 0; i < n; i++)
    if (s[i] == '1')
      sum++;
  ll curr = 1, num = 0;
  ll plu, neg;
  sum++;
  for (ll i = s.length() - 1; i >= 0; i--) {
    if (s[i] == '1') {
      num = (num + curr) % sum;
    }
    curr = (curr * 2) % sum;
  }
  plu = num;
  // cout<<power(4,3)<<"y";
  // for(ll i=0;i<n;i++)plu[i]=num;
  sum -= 2;
  curr = 1;
  num = 0;
  // cout<<sum<<" ";
  for (ll i = s.length() - 1; i >= 0; i--) {
    if (sum == 0) {
      num = 0;
      break;
    }
    if (s[i] == '1') {
      num = (num + curr) % sum;
    }
    curr = (curr * 2) % sum;
  }
  neg = num;
  sum++;
  // ll n=s.length();
  // cout<<plu<<" "<<neg;
  ll ans;
  for (ll i = 0; i < n; i++) {
    ans = 0;
    if (s[i] == '1') {
      num = neg;
      // cout<<sum-1<<" ";
      if (sum - 1 == 0) {
        num = 0;
        cout << "0" << endl;
        continue;
      }
      num = (num - power(n - i - 1, sum - 1) + sum - 1) % (sum - 1);
    } else {
      num = plu;
      // cout<<n-i-1<<" ";
      // cout<<power(n-i-1,sum+1)<<" ";
      num = (num + power(n - i - 1, sum + 1) + sum + 1) % (sum + 1);
    }
    ans = fun(num) + 1;
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 1000000000000000000, mod = 1000000007, BS, k;
ll power(ll a, ll b) {
  if (a == 0)
    return 1;
  ll res = 1, curr = 2;
  while (a) {
    if (a & 1)
      res = (res * curr) % b;
    curr = (curr * curr) % b;
    a /= 2;
  }
  return res;
}
ll fun(ll num) {
  if (num == 0)
    return 1;
  ll coun, res = 1;
  while (num) {
    coun = __builtin_popcount(num);
    res++;
    if (coun == 0)
      break;
    num %= coun;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll sum = 0;
  for (ll i = 0; i < n; i++)
    if (s[i] == '1')
      sum++;
  ll curr = 1, num = 0;
  ll plu, neg;
  sum++;
  for (ll i = s.length() - 1; i >= 0; i--) {
    if (s[i] == '1') {
      num = (num + curr) % sum;
    }
    curr = (curr * 2) % sum;
  }
  plu = num;
  // cout<<power(4,3)<<"y";
  // for(ll i=0;i<n;i++)plu[i]=num;
  sum -= 2;
  curr = 1;
  num = 0;
  // cout<<sum<<" ";
  for (ll i = s.length() - 1; i >= 0; i--) {
    if (s[i] == '1') {
      num = (num + curr) % sum;
    }
    curr = (curr * 2) % sum;
  }
  neg = num;
  sum++;
  // ll n=s.length();
  // cout<<plu<<" "<<neg;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      num = neg;
      // cout<<sum-1<<" ";
      num = (num - power(n - i - 1, sum - 1) + sum - 1) % (sum - 1);
    } else {
      num = plu;
      // cout<<n-i-1<<" ";
      // cout<<power(n-i-1,sum+1)<<" ";
      num = (num + power(n - i - 1, sum + 1) + sum + 1) % (sum + 1);
    }
    cout << fun(num) << endl;
  }
}