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

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

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

ll power(ll a, ll b, ll mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = ((res % mod) * (a % mod)) % mod;
    b >>= 1;
    a = ((a % mod) * (a % mod)) % mod;
  }
  return res;
}
int get(int n) {
  if (!n)
    return 0;
  int unos = __builtin_popcount(n);
  return 1 + get(n % unos);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int N;
  cin >> N;
  string str;
  cin >> str;
  int unos = 0;
  int pot = N - 1;
  forn(i, 0, N) unos += (str[i] == '1');

  ll res1 = 0; // unos+1
  ll res2 = 0; // unos-1

  forn(i, 0, N) {
    if (str[i] == '1') {
      res1 += power(2, pot, unos + 1);
      res1 %= (unos + 1);

      if (unos - 1 > 0) {
        res2 += power(2, pot, unos - 1);
        res2 %= (unos - 1);
      }
    }
    pot--;
  }
  pot = N - 1;
  vi res;
  forn(i, 0, N) {
    ll tmpres1 = res1;
    ll tmpres2 = res2;
    if (str[i] == '0') {
      tmpres1 += power(2, pot, unos + 1);
      tmpres1 %= (unos + 1);
      // cout << i << ' ' << tmpres1 << '\\n';
      res.pb(1 + get(tmpres1));
    } else {
      if (unos - 1 > 0) {
        tmpres2 -= power(2, pot, unos - 1);
        if (tmpres2 < 0)
          tmpres2 += (unos - 1);
        res.pb(1 + get(tmpres2));
      } else {
        res.pb(0);
      }
      // cout << i << ' ' << tmpres2 << '\\n';
    }
    pot--;
  }
  forn(i, 0, N) cout << res[i] << '\\n';

  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


Error: 0

Fixed submission:

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

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

ll power(ll a, ll b, ll mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = ((res % mod) * (a % mod)) % mod;
    b >>= 1;
    a = ((a % mod) * (a % mod)) % mod;
  }
  return res;
}
int get(int n) {
  if (!n)
    return 0;
  int unos = __builtin_popcount(n);
  return 1 + get(n % unos);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int N;
  cin >> N;
  string str;
  cin >> str;
  int unos = 0;
  int pot = N - 1;
  forn(i, 0, N) unos += (str[i] == '1');

  ll res1 = 0; // unos+1
  ll res2 = 0; // unos-1

  forn(i, 0, N) {
    if (str[i] == '1') {
      res1 += power(2, pot, unos + 1);
      res1 %= (unos + 1);
      res2 += power(2, pot, unos - 1);
      res2 %= (unos - 1);
    }
    pot--;
  }
  pot = N - 1;
  vi res;
  forn(i, 0, N) {
    ll tmpres1 = res1;
    ll tmpres2 = res2;
    if (str[i] == '0') {
      tmpres1 += power(2, pot, unos + 1);
      tmpres1 %= (unos + 1);
      // cout << i << ' ' << tmpres1 << '\\n';
      res.pb(1 + get(tmpres1));
    } else {
      tmpres2 -= power(2, pot, unos - 1);
      if (tmpres2 < 0)
        tmpres2 += (unos - 1);
      res.pb(1 + get(tmpres2));
      // cout << i << ' ' << tmpres2 << '\\n';
    }
    pot--;
  }
  forn(i, 0, N) cout << res[i] << '\\n';

  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/