Problem p02609 (C++)

Buggy submission (status: Runtime Error):
// I make this just for fun because i'm done
// Aimi Haraguni >> Konomi Suzuki >> Yui >>  Ikimono Gakari >> Garnidelia >>
// Kalafina >> Eir Aoi.   .. dude? problems that involves any kind of persistent
// data structures are the best of the best, are not them?

// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define pb push_back
#define ff first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define all(v) (v).begin(), (v).end()
#define FER(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define IFR(i, a, b) for (ll i = ll(a); i >= ll(b); --i)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

#define N 31000000
// #define M 19

// #define mod  11092019
#define mod1 1000000007
#define mod2 1000000009
#define bas 987625403
#define sqr(x) (x) * (x)
#define INF 2000000000000

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> tri;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template <typename t> void fff(const char *x, t &&val1) {
  cout << x << " : " << val1 << "\n";
}
template <typename t1, typename... t2>
void fff(const char *x, t1 &&val1, t2 &&...val2) {
  const char *xd = strchr(x + 1, ',');
  cout.write(x, xd - x) << " : " << val1 << " | ";
  fff(xd + 1, val2...);
}

inline ll mul(ll a, ll b, ll mod) { return (long long)a * b % mod; }
inline ll add(ll a, ll b, ll mod) { return a + b < mod ? a + b : a + b - mod; }
inline void Mul(ll &a, ll b, ll mod) { a = (long long)a * b % mod; }
inline ll bp(ll a, ll p, ll mod) {
  ll ret;
  for (ret = 1; p; p >>= 1, Mul(a, a, mod))
    (p & 1) && (Mul(ret, a, mod), 1);
  return ret;
}

static inline ll get_num() {
  ll num = 0;
  char c = getchar_unlocked();
  while (!((c >= '0') & (c <= '9')))
    c = getchar_unlocked();
  while (c >= '0' && c <= '9') {
    num = (num << 1) + (num << 3) + c - '0';
    c = getchar_unlocked();
  }
  return num;
}

static inline void read(ll &x) { x = get_num(); }

ll cnt[1 << 19];

inline ll solve(ll x) {
  if (x == 0)
    return 0;
  ll ret = __builtin_popcountll(x);
  return 1 + solve(x % ret);
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (auto xd : s)
    if (xd == '1')
      cnt++;
  ll a = cnt - 1, b = cnt + 1;
  ll valor1 = 0, valor2 = 0;
  if (a > 0) {
    ll tot = 1;
    IFR(i, n - 1, 0) {
      valor1 += (tot * (s[i] - '0')) % a;
      if (valor1 >= a)
        valor1 -= a;
      tot <<= 1;
      tot %= a;
    }
  }
  if (b > 0) {
    ll tot = 1;
    IFR(i, n - 1, 0) {
      valor2 += (tot * (s[i] - '0')) % b;
      if (valor2 >= b)
        valor2 -= b;
      tot <<= 1;
      tot %= b;
    }
  }
  FER(i, 0, n) {
    if (s[i] == '1') {
      // resto es a;
      ll valor = (valor1 - bp(2, n - i - 1, a) + a) % a;
      if (valor == 0)
        cout << "1\n";
      else
        cout << solve(valor) + 1 << "\n";
    } else {
      // resto es b;
      ll valor = (valor2 + bp(2, n - i - 1, b) + b) % b;
      if (valor == 0)
        cout << "1\n";
      else
        cout << solve(valor) + 1 << "\n";
    }
  }
  return 0;
}

Fixed submission:
// I make this just for fun because i'm done
// Aimi Haraguni >> Konomi Suzuki >> Yui >>  Ikimono Gakari >> Garnidelia >>
// Kalafina >> Eir Aoi.   .. dude? problems that involves any kind of persistent
// data structures are the best of the best, are not them?

// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define pb push_back
#define ff first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define all(v) (v).begin(), (v).end()
#define FER(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define IFR(i, a, b) for (ll i = ll(a); i >= ll(b); --i)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

#define N 31000000
// #define M 19

// #define mod  11092019
#define mod1 1000000007
#define mod2 1000000009
#define bas 987625403
#define sqr(x) (x) * (x)
#define INF 2000000000000

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> tri;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template <typename t> void fff(const char *x, t &&val1) {
  cout << x << " : " << val1 << "\n";
}
template <typename t1, typename... t2>
void fff(const char *x, t1 &&val1, t2 &&...val2) {
  const char *xd = strchr(x + 1, ',');
  cout.write(x, xd - x) << " : " << val1 << " | ";
  fff(xd + 1, val2...);
}

inline ll mul(ll a, ll b, ll mod) { return (long long)a * b % mod; }
inline ll add(ll a, ll b, ll mod) { return a + b < mod ? a + b : a + b - mod; }
inline void Mul(ll &a, ll b, ll mod) { a = (long long)a * b % mod; }
inline ll bp(ll a, ll p, ll mod) {
  ll ret;
  for (ret = 1; p; p >>= 1, Mul(a, a, mod))
    (p & 1) && (Mul(ret, a, mod), 1);
  return ret;
}

static inline ll get_num() {
  ll num = 0;
  char c = getchar_unlocked();
  while (!((c >= '0') & (c <= '9')))
    c = getchar_unlocked();
  while (c >= '0' && c <= '9') {
    num = (num << 1) + (num << 3) + c - '0';
    c = getchar_unlocked();
  }
  return num;
}

static inline void read(ll &x) { x = get_num(); }

ll cnt[1 << 19];

inline ll solve(ll x) {
  if (x == 0)
    return 0;
  ll ret = __builtin_popcountll(x);
  return 1 + solve(x % ret);
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (auto xd : s)
    if (xd == '1')
      cnt++;
  ll a = cnt - 1, b = cnt + 1;
  ll valor1 = 0, valor2 = 0;
  if (a > 0) {
    ll tot = 1;
    IFR(i, n - 1, 0) {
      valor1 += (tot * (s[i] - '0')) % a;
      if (valor1 >= a)
        valor1 -= a;
      tot <<= 1;
      tot %= a;
    }
  }
  if (b > 0) {
    ll tot = 1;
    IFR(i, n - 1, 0) {
      valor2 += (tot * (s[i] - '0')) % b;
      if (valor2 >= b)
        valor2 -= b;
      tot <<= 1;
      tot %= b;
    }
  }
  FER(i, 0, n) {
    if (s[i] == '1') {
      // resto es a;
      if (!a) {
        cout << 0 << "\n";
        continue;
      }
      ll valor;
      if (a > 0)
        valor = (valor1 - bp(2, n - i - 1, a) + a) % a;
      if (valor == 0)
        cout << "1\n";
      else
        cout << solve(valor) + 1 << "\n";
    } else {
      // resto es b;
      ll valor = (valor2 + bp(2, n - i - 1, b) + b) % b;
      if (valor == 0)
        cout << "1\n";
      else
        cout << solve(valor) + 1 << "\n";
    }
  }
  return 0;
}