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

// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using o_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) find_by_order(i) #define
// ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
using cd = complex<double>;
const double pi = acos(-1);
// #define mod 1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \\
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \\
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\\n' //....????
#define int ll
vector<ll> ft(1, 1LL);

/*======================================================================*/
void solve() {
  int n, cunt[2] = {0};
  string s;
  cin >> n >> s;
  vi v(n + 1, 0);
  rep(i, 1, n + 1) {
    int c = 0, j = i;
    while (j != 0) {
      j = j % __builtin_popcount(j);
      c++;
    }
    v[i] = c;
  }
  for (auto i : s)
    cunt[0] += i == '1';
  int mo[2] = {0, 0};
  int p[2] = {1, 1};
  cunt[1] = cunt[0] + 1, cunt[0]--;
  reverse(all(s));
  rep(i, 0, 2) {
    if (cunt[i] <= 0)
      continue;
    rep(j, 0, n) {
      mo[i] = (mo[i] + (s[j] - '0') * p[i]) % cunt[i];
      p[i] = (p[i] * 2) % cunt[i];
    }
  }
  vi ans;
  p[0] = p[1] = 1;
  rep(j, 0, n) {
    if (s[j] == '1') {
      if (cunt[0] == 0)
        ans.pb(-1);
      else
        ans.pb(v[(mo[0] - p[0] + cunt[0]) % cunt[0]]);
    } else {
      ans.pb(v[(mo[1] + p[1]) % cunt[1]]);
    }
    rep(i, 0, 2) if (cunt[i] != 0) p[i] = (p[i] * 2) % cunt[i];
  }
  reverse(all(ans));
  for (auto i : ans)
    cout << i + 1 << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}

Error: 0

Fixed submission:

// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using o_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) find_by_order(i) #define
// ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
using cd = complex<double>;
const double pi = acos(-1);
// #define mod 1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \\
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \\
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\\n' //....????
#define int ll
vector<ll> ft(1, 1LL);

/*======================================================================*/
void solve() {
  int n, cunt[2] = {0};
  string s;
  cin >> n >> s;
  vi v(n + 1, 0);
  rep(i, 1, n + 1) {
    int c = 0, j = i;
    while (j != 0) {
      j = j % __builtin_popcount(j);
      c++;
    }
    v[i] = c;
  }
  for (auto i : s)
    cunt[0] += i == '1';
  int mo[2] = {0, 0};
  int p[2] = {1, 1};
  cunt[1] = cunt[0] + 1, cunt[0]--;
  reverse(all(s));
  rep(i, 0, 2) {
    if (cunt[i] <= 0)
      continue;
    rep(j, 0, n) {
      mo[i] = (mo[i] + (s[j] - '0') * p[i]) % cunt[i];
      p[i] = (p[i] * 2) % cunt[i];
    }
  }
  vi ans;
  p[0] = p[1] = 1;
  rep(j, 0, n) {
    if (s[j] == '1') {
      if (cunt[0] == 0)
        ans.pb(1);
      else
        ans.pb(v[(mo[0] - p[0] + cunt[0]) % cunt[0]]);
    } else {
      ans.pb(v[(mo[1] + p[1]) % cunt[1]]);
    }
    rep(i, 0, 2) p[i] = (p[i] * 2) % cunt[i];
  }
  reverse(all(ans));
  for (auto i : ans)
    cout << i + 1 << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}