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

#include <bits/stdc++.h>
#include <cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll>
#define vd vector<double>
#define vld vector<long double>
#define ll long long int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for (ll i = 0; i < n; i++)
#define forr1(i, n) for (ll i = 1; i <= n; i++)
using namespace std;
const ld PI = 3.1415926535897923846;
const ll N = 998244353;
ll power(ll x, ll n, ll MOD) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}
ll modinverse(ll a) { return power(a, N - 2, N); }
void solve() {
  ll n;
  string str;
  cin >> n >> str;
  ll total1 = 0, total2 = 0;
  ll check = 0;
  forr(i, str.length()) {
    if (str[i] == '1')
      check++;
  }
  vl plus(n), minus(n);
  forr(i, str.length()) {
    ll num = str[i] - '0';
    if (num) {
      total1 += (ll)power(2, n - 1 - i, check + 1);
      if (check > 1)
        total2 += (ll)power(2, n - 1 - i, check - 1);
      total1 %= (check + 1);
      if (check > 1)
        total2 %= (check - 1);
      if (check > 1) {
        if (total2 < 0)
          total2 += (check - 1);
      }
      if (total1 < 0)
        total1 += (check + 1);
    }
  }
  forr(i, n) {
    ll num = str[i] - '0';
    ll popc = check;
    ll ans = 1;
    ll norm = 0;
    if (num) {
      popc--;
      norm = total2;
      if (check == 1) {
        cout << 0 << endl;
        continue;
      }
      if (check > 1)
        norm -= power(2, n - 1 - i, check - 1);
      if (check > 1)
        norm %= (check - 1);
    } else {
      popc++;
      norm = total1;
      norm += power(2, n - 1 - i, check + 1);
      norm %= (check + 1);
    }
    if (norm < 0)
      norm += popc;
    while (true) {
      if (norm == 0) {
        cout << ans << endl;
        break;
      }
      norm %= __builtin_popcount(norm);
      ans++;
    }
  }
}
int main() {
  ll test = 1;
  // cin>>test;
  while (test--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll>
#define vd vector<double>
#define vld vector<long double>
#define ll long long int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for (ll i = 0; i < n; i++)
#define forr1(i, n) for (ll i = 1; i <= n; i++)
using namespace std;
const ld PI = 3.1415926535897923846;
const ll N = 998244353;
ll power(ll x, ll n, ll MOD) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}
ll modinverse(ll a) { return power(a, N - 2, N); }
void solve() {
  ll n;
  string str;
  cin >> n >> str;
  ll total1 = 0, total2 = 0;
  ll check = 0;
  forr(i, str.length()) {
    if (str[i] == '1')
      check++;
  }
  vl plus(n), minus(n);
  forr(i, str.length()) {
    ll num = str[i] - '0';
    if (num) {
      total1 += (ll)power(2, n - 1 - i, check + 1);
      total2 += (ll)power(2, n - 1 - i, check - 1);
      total1 %= (check + 1);
      total2 %= (check - 1);
      if (total2 < 0)
        total2 += (check - 1);
      if (total1 < 0)
        total1 += (check + 1);
    }
  }
  forr(i, n) {
    ll num = str[i] - '0';
    ll popc = check;
    ll ans = 1;
    ll norm = 0;
    if (num) {
      popc--;
      norm = total2;
      norm -= power(2, n - 1 - i, check - 1);
      norm %= (check - 1);
    } else {
      popc++;
      norm = total1;
      norm += power(2, n - 1 - i, check + 1);
      norm %= (check + 1);
    }
    if (norm < 0)
      norm += popc;
    while (true) {
      if (norm == 0) {
        cout << ans << endl;
        break;
      }
      norm %= __builtin_popcount(norm);
      ans++;
    }
  }
}
int main() {
  ll test = 1;
  // cin>>test;
  while (test--) {
    solve();
  }
}