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

#include <bits/stdc++.h>
#define ll long long int
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007
#define rep(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define ff first
#define ss second
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define Pi 2 * acos(0.0)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define fill_(a, b) fill(a, a + n, b);
using namespace std;

ll modpow(ll a, ll b, ll m = mod) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  while (b > 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int getSteps(int x) {
  int res = 0;
  while (x > 0) {
    x = x % __builtin_popcount(x);
    res++;
  }
  return res;
}
int main() {

  boost
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // 	cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll cnt = 0;
    for (int i = 0; i < s.length(); i++) {
      cnt += (s[i] == '1' ? 1 : 0);
    }
    ll res = 0, resm = 0, resp = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '0')
        continue;
      res += modpow(2, n - i - 1, cnt);
      if (cnt > 1) {

        resm += modpow(2, n - i - 1, cnt - 1);
        resm %= (cnt - 1);
      }
      resp += modpow(2, n - i - 1, cnt + 1);
      resp %= (cnt + 1);
    }

    // cout<<cnt<<" "<<resp<<" "<<resm<<"\\n";
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '1') {

        if (cnt - 1 == 0)
          cout << "0\\n";
        else {
          int temp =
              (resm - modpow(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
          cout << 1 + getSteps(temp) << "\\n";
        }
      } else {
        int temp = (resp + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
        cout << 1 + getSteps(temp) << "\\n";
      }
    }
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007
#define rep(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define ff first
#define ss second
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define Pi 2 * acos(0.0)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define fill_(a, b) fill(a, a + n, b);
using namespace std;

ll modpow(ll a, ll b, ll m = mod) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  while (b > 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int getSteps(int x) {
  int res = 0;
  while (x > 0) {
    x = x % __builtin_popcount(x);
    res++;
  }
  return res;
}
int main() {

  boost
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // 	cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll cnt = 0;
    for (int i = 0; i < s.length(); i++) {
      cnt += (s[i] == '1' ? 1 : 0);
    }
    ll res = 0, resm = 0, resp = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '0')
        continue;
      res += modpow(2, n - i - 1, cnt);
      if (cnt > 1) {

        resm += modpow(2, n - i - 1, cnt - 1);
        resm %= (cnt - 1);
      }
      resp += modpow(2, n - i - 1, cnt + 1);
      resp %= (cnt + 1);
    }

    // cout<<cnt<<" "<<resp<<" "<<resm<<"\\n";
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '1') {
        int temp = (resm - modpow(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
        if (cnt - 1 == 0)
          cout << "0\\n";
        else
          cout << 1 + getSteps(temp) << "\\n";
      } else {
        int temp = (resp + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
        cout << 1 + getSteps(temp) << "\\n";
      }
    }
  }
  return 0;
}