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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, double> iidb;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e18 + 7;
const double PI = acos(-1);
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 210000
#define offset 200
#define ptr nodo *

using namespace std;
const int mod = 1e6 + 3;
string txt;
int v[tam], dp[tam];
int f(int num) {
  if (num == 0)
    return 0;
  if (dp[num] != -1)
    return dp[num];
  return dp[num] = f(num % (__builtin_popcount(num))) + 1;
}
int main() {
  memset(dp, -1, sizeof dp);
  int n;
  cin >> n;
  cin >> txt;
  ll actnum = 0;
  int bits = 0;
  forr(i, 0, n) {
    if (txt[i] == '1')
      bits++;
  }
  int b1, b2;
  b1 = bits - 1, b2 = bits + 1;
  int actnum1, actnum2;
  actnum1 = actnum2 = 0;
  forr(i, 0, n) {
    if (b1 != 0) {
      actnum1 *= 2;
      actnum1 += (txt[i] == '1');
      actnum1 %= b1;
    }

    actnum2 *= 2;
    actnum2 += (txt[i] == '1');
    actnum2 %= b2;
  }
  reverse(all(txt));
  ll pot1 = 1, pot2 = 1;
  // cout<<txt<<endl;
  // cout<<actnum1<<" "<<actnum2<<endl;
  forr(i, 0, n) {
    ll auxanswer;
    if (txt[i] == '1') {
      if (b1 == 0) {
        v[n - 1 - i] = 0;
        continue;
      }
      auxanswer = actnum1 - pot1 + b1;
      auxanswer %= b1;
      v[n - 1 - i] = f(auxanswer) + 1;
    } else {
      auxanswer = actnum2 + pot2;
      auxanswer %= b2;
      // cout<<auxanswer<<" "<<i<<endl;
      v[n - 1 - i] = f(auxanswer) + 1;
    }
    if (b1 != 0)
      pot1 *= 2, pot1 %= b1;

    pot2 *= 2;
    pot2 %= b2;
  }
  forr(i, 0, n) { cout << v[i] << endl; }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, double> iidb;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e18 + 7;
const double PI = acos(-1);
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 210000
#define offset 200
#define ptr nodo *

using namespace std;
const int mod = 1e6 + 3;
string txt;
int v[tam], dp[tam];
int f(int num) {
  if (num == 0)
    return 0;
  if (dp[num] != -1)
    return dp[num];
  return dp[num] = f(num % (__builtin_popcount(num))) + 1;
}
int main() {
  memset(dp, -1, sizeof dp);
  int n;
  cin >> n;
  cin >> txt;
  ll actnum = 0;
  int bits = 0;
  forr(i, 0, n) {
    if (txt[i] == '1')
      bits++;
  }
  int b1, b2;
  b1 = bits - 1, b2 = bits + 1;
  int actnum1, actnum2;
  actnum1 = actnum2 = 0;
  forr(i, 0, n) {
    actnum1 *= 2;
    actnum1 += (txt[i] == '1');
    actnum1 %= b1;
    actnum2 *= 2;
    actnum2 += (txt[i] == '1');
    actnum2 %= b2;
  }
  reverse(all(txt));
  ll pot1 = 1, pot2 = 1;
  // cout<<txt<<endl;
  // cout<<actnum1<<" "<<actnum2<<endl;
  forr(i, 0, n) {
    ll auxanswer;
    if (txt[i] == '1') {
      auxanswer = actnum1 - pot1 + b1;
      auxanswer %= b1;
      v[n - 1 - i] = f(auxanswer) + 1;
    } else {
      auxanswer = actnum2 + pot2;
      auxanswer %= b2;
      // cout<<auxanswer<<" "<<i<<endl;
      v[n - 1 - i] = f(auxanswer) + 1;
    }
    pot1 *= 2;
    pot1 %= b1;
    pot2 *= 2;
    pot2 %= b2;
  }
  forr(i, 0, n) { cout << v[i] << endl; }
}