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

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

#define ff(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
// #define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int, int>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; /// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int n;
string x;

ll power(ll a, int b, ll mod) {
  if (b == 0)
    return 1;
  ll pola = power(a, b / 2, mod);
  pola = pola * pola;
  pola %= mod;
  if (b % 2 == 1)
    pola *= a;
  pola %= mod;
  return pola;
}

ll nadji(ll mod) {
  ll st = 1;
  ll res = 0;
  fb(i, n - 1, 0) {
    res += (st * (x[i] - '0'));
    res %= mod;
    st *= 2;
    st %= mod;
  }
  return res;
}

int res[200005];
int resi(int a) {
  if (a == 0)
    return 0;
  int st = 0;
  int pom = 0;
  while ((1 << st) <= a) {
    if ((1 << st) & a)
      pom++;
    st++;
  }
  return 1 + resi(a % pom);
}

int main() {

  // ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n >> x;
  int kol = 0;
  ff(i, 0, n - 1) {
    if (x[i] == '1')
      kol++;
  }
  int ost0 = 0;
  int ost1 = 0;
  int ost2 = 0;
  if (kol) {
    ost0 = nadji(kol);
  } else
    ost0 = -1;
  if (kol - 1) {
    ost1 = nadji(kol - 1);
  } else
    ost1 = -1;

  ost2 = nadji(kol + 1);

  ll st1 = 1;
  ll st2 = 1;
  if (kol == 1)
    st1 = 0;
  fb(i, n - 1, 0) {
    if (x[i] == '1') {
      if (kol == 1) {
        st1 *= 2;
        st2 *= 2;
        st2 %= (kol + 1);
        if (kol - 1)
          st1 %= (kol - 1);
        continue;
      }
      int ost = ost1 - st1;
      ost %= (kol - 1);
      if (ost < 0)
        ost += (kol - 1);
      res[i] = 1 + resi(ost);
    } else {
      int ost = ost2 + st2;
      ost %= (kol + 1);
      res[i] = 1 + resi(ost);
    }
    st1 *= 2;
    st2 *= 2;
    st2 %= (kol + 1);
    if (kol - 1)
      st1 %= (kol - 1);
  }
  ff(i, 0, n - 1) { cout << res[i] << "\\n"; }
  return 0;
}


Error: 0

Fixed submission:

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

#define ff(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
// #define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int, int>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; /// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int n;
string x;

ll power(ll a, int b, ll mod) {
  if (b == 0)
    return 1;
  ll pola = power(a, b / 2, mod);
  pola = pola * pola;
  pola %= mod;
  if (b % 2 == 1)
    pola *= a;
  pola %= mod;
  return pola;
}

ll nadji(ll mod) {
  ll st = 1;
  ll res = 0;
  fb(i, n - 1, 0) {
    res += (st * (x[i] - '0'));
    res %= mod;
    st *= 2;
    st %= mod;
  }
  return res;
}

int res[200005];
int resi(int a) {
  if (a == 0)
    return 0;
  int st = 0;
  int pom = 0;
  while ((1 << st) <= a) {
    if ((1 << st) & a)
      pom++;
    st++;
  }
  return 1 + resi(a % pom);
}

int main() {

  // ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n >> x;
  int kol = 0;
  ff(i, 0, n - 1) {
    if (x[i] == '1')
      kol++;
  }
  int ost0 = 0;
  int ost1 = 0;
  int ost2 = 0;
  if (kol) {
    ost0 = nadji(kol);
  } else
    ost0 = -1;
  if (kol - 1) {
    ost1 = nadji(kol - 1);
  } else
    ost1 = -1;

  ost2 = nadji(kol + 1);

  ll st = 1;

  fb(i, n - 1, 0) {
    if (x[i] == '1') {
      if (kol == 1) {
        st *= 2;
        continue;
      }
      int ost = ost1 - st;
      ost %= (kol - 1);
      if (ost < 0)
        ost += (kol - 1);
      res[i] = 1 + resi(ost);
    } else {
      int ost = ost2 + st;
      ost %= (kol + 1);
      // cout << ost << "\\n";
      // cout << (broj + 1) % (kol + 1);
      // return 0;
      res[i] = 1 + resi(ost);
    }
    st *= 2;
  }
  ff(i, 0, n - 1) { cout << res[i] << "\\n"; }
  return 0;
}