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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define tr(con, it) for (auto it = con.begin(); it != con.end(); it++)
#define present(con, ele) (con.find(ele) != con.end())
#define cpresent(con, ele) (find(all(con), ele) != con.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
ll fn(ll a) {
  if (a) {
    return -1;
  }
  return 1;
}
ll power(ll a, ll b, ll m = mod) {
  if (b == 0) {
    return 1 % m;
  }
  ll u = power(a, b / 2, m);
  u = (u * u) % m;
  if (b % 2 == 1) {
    u = (u * a) % m;
  }
  return u;
}
int32_t main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, cnt = 0;
  vll ans(2, 0);
  cin >> n;
  string a;
  cin >> a;
  fr(i, 0, n) {
    if (a[i] - '0') {
      cnt++;
    }
  }
  cnt++;
  for (int i = n - 1; i >= 0; i--) {
    ans[0] = (ans[0] + ((a[i] - '0') * power(2, n - i - 1, cnt)) % cnt) % cnt;
  }
  if (cnt > 2) {
    cnt -= 2;
    for (int i = n - 1; i >= 0; i--) {
      ans[1] = (ans[1] + ((a[i] - '0') * power(2, n - i - 1, cnt)) % cnt) % cnt;
    }
    cnt += 2;
  }
  fr(i, 0, n) {
    if (cnt - 2 * (a[i] - '0') == 0) {
      cout << 0 << "\\n";
      continue;
    }
    ll tp = (ans[a[i] - '0'] +
             fn(a[i] - '0') * power(2, n - i - 1, cnt - 2 * (a[i] - '0'))) %
            (cnt - 2 * (a[i] - '0')),
       an = 1;
    if (tp < 0) {
      tp += (cnt - 2 * (a[i] - '0'));
    }
    while (tp != 0) {
      an++;
      tp %= __builtin_popcount(tp);
    }
    cout << an << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define tr(con, it) for (auto it = con.begin(); it != con.end(); it++)
#define present(con, ele) (con.find(ele) != con.end())
#define cpresent(con, ele) (find(all(con), ele) != con.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
ll fn(ll a) {
  if (a) {
    return -1;
  }
  return 1;
}
int32_t main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, cnt = 0;
  vll ans(2, 0);
  cin >> n;
  string a;
  cin >> a;
  fr(i, 0, n) {
    if (a[i] - '0') {
      cnt++;
    }
  }
  cnt++;
  for (int i = n - 1; i >= 0; i--) {
    ans[0] = (ans[0] + ((a[i] - '0') * (1 << (n - i - 1))) % cnt) % cnt;
  }
  if (cnt >= 2) {
    cnt -= 2;
    for (int i = n - 1; i >= 0; i--) {
      ans[1] = (ans[1] + ((a[i] - '0') * (1 << (n - i - 1))) % cnt) % cnt;
    }
  }
  cnt += 2;
  fr(i, 0, n) {
    ll tp = (ans[a[i] - '0'] + fn(a[i] - '0') * (1 << (n - i - 1))) %
            (cnt - 2 * (a[i] - '0')),
       an = 1;
    if (tp < 0) {
      tp += cnt - 2 * (a[i] - '0');
    }
    while (tp != 0) {
      an++;
      tp %= __builtin_popcount(tp);
    }
    cout << an << "\\n";
  }
  return 0;
}