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

#include <bits/stdc++.h>
using namespace std;

long long countSetBits(long long n) {
  long long count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}

long long getans(long long ans) {
  long long result = 0;
  if (ans == 0)
    return result;
  while (ans) {
    ans %= countSetBits(ans);
    result++;
  }
  return result;
}

long long modpow(long long x, long long n, long long m) {
  if (n == 0)
    return 1 % m;
  long long u = modpow(x, n / 2, m);
  u = (u * u) % m;
  if (n % 2 == 1)
    u = (u * x) % m;
  return u;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long count = 0;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '1') {
      count++;
    }
  }
  long long ans = 0, ans2 = 0;
  long long tempans;
  for (long long i = 0; i < n; i++) {
    // ans+=pow(2*(s[i]-'0'), n-i-1);
    if (s[i] == '1') {
      ans += modpow(2, n - i - 1, count + 1);
      ans %= count + 1;
      if (count - 1 > 0) {
        ans2 += modpow(2, n - i - 1, count - 1);
        ans2 %= (count - 1);
      }
    }
  }
  // cout<<ans<<" "<<count;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (count - 1 > 0) {
        tempans = ans2 - modpow(2, n - i - 1, count - 1);
        tempans = (tempans % (count - 1) + count - 1) % (count - 1);
        // cout<<tempans<<endl;
        tempans = getans(tempans);
        cout << tempans + 1 << "\\n";
      } else {
        cout << 0 << "\\n";
      }
    } else {
      tempans = ans + modpow(2, n - i - 1, count + 1);
      tempans = tempans % (count + 1);
      tempans = getans(tempans);
      cout << tempans + 1 << "\\n";
    }
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long countSetBits(long long n) {
  long long count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}

long long getans(long long ans) {
  long long result = 0;
  if (ans == 0)
    return result;
  while (ans) {
    ans %= countSetBits(ans);
    result++;
  }
  return result;
}

long long modpow(long long x, long long n, long long m) {
  if (n == 0)
    return 1 % m;
  long long u = modpow(x, n / 2, m);
  u = (u * u) % m;
  if (n % 2 == 1)
    u = (u * x) % m;
  return u;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long count = 0;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '1') {
      count++;
    }
  }
  long long ans = 0, ans2 = 0;
  long long tempans;
  for (long long i = 0; i < n; i++) {
    // ans+=pow(2*(s[i]-'0'), n-i-1);
    if (s[i] == '1') {
      ans += modpow(2, n - i - 1, count + 1);
      ans %= count + 1;
      ans2 += modpow(2, n - i - 1, count - 1);
      ans2 %= (count - 1);
    }
  }
  // cout<<ans<<" "<<count;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '1') {
      tempans = ans2 - modpow(2, n - i - 1, count - 1);
      tempans = (tempans % (count - 1) + count - 1) % (count - 1);
      // cout<<tempans<<endl;
      tempans = getans(tempans);
      cout << tempans + 1 << "\\n";
    } else {
      tempans = ans + modpow(2, n - i - 1, count + 1);
      tempans = tempans % (count + 1);
      tempans = getans(tempans);
      cout << tempans + 1 << "\\n";
    }
  }
}