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

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'
#define all(x) x.begin(), x.end()

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using vvll = vector<vector<ll>>;
using vll = vector<ll>;
using vb = vector<bool>;
using pll = pair<ll, ll>;

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

void pre() {}

ll go(ll n) {
  if (n == 0)
    return 0;
  ll bits = __builtin_popcount(n);
  return 1 + go(n % bits);
}

void solve() {

  ll n;
  cin >> n;
  string s;
  cin >> s;
  reverse(all(s));
  ll c = 0;
  for (auto ch : s)
    if (ch == '1')
      c++;

  vll mod = {c - 1, c, c + 1};
  vll A[3];
  for (ll i = 0; i < 3; i++) {
    A[i].resize(n + 1);
    fill(all(A[i]), 1);
    for (ll j = 0; j < n; j++) {
      if (mod[i]) {
        A[i][j + 1] = A[i][j] * 2 % mod[i];
      }
    }
  }

  vll tot(3, 0);
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      for (ll j = 0; j < 3; j++) {
        tot[j] += A[j][i];
      }
    }
  }

  vll ans(n, 0);
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (c == 1) {
        ans[i] = 0;
      } else {
        ans[i] = 1 + go((tot[0] - A[0][i] + mod[0]) % mod[0]);
      }
    } else {
      ans[i] = 1 + go((tot[2] + A[2][i]) % mod[2]);
    }
  }
  reverse(all(ans));
  for (ll i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input00.txt", "r", stdin);
#endif
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'
#define all(x) x.begin(), x.end()

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using vvll = vector<vector<ll>>;
using vll = vector<ll>;
using vb = vector<bool>;
using pll = pair<ll, ll>;

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

void pre() {}

ll go(ll no) {
  if (no == 0)
    return 0;
  ll bits = __builtin_popcount(no);
  return 1 + go(no % bits);
}

vector<ll> A[2];
vector<ll> POW[2];

ll get(string s, ll n, ll mod, ll ind) {
  ll no = 0;
  ll p = 1 % mod;
  POW[ind].resize(n);
  A[ind].resize(n);
  for (ll i = n - 1; i >= 0; i--) {
    A[ind][i] = 0;
    if (s[i] == '1')
      no += p, A[ind][i] = p;
    POW[ind][i] = p;
    p *= 2;
    p %= mod;
    no %= mod;
  }
  return no;
}

void solve() {

  ll n;
  cin >> n;
  string s;
  cin >> s;
  if (n == 1) {
    if (s[0] == '1') {
      cout << 0;
    } else {
      cout << 1;
    }
    return;
  }
  ll one = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      one++;
  }
  if (one == 0) {
    for (ll i = 0; i < n; i++)
      cout << 1 << endl;
    return;
  }
  ll no = get(s, n, one + 1, 0);
  ll no1 = get(s, n, one - 1, 1);
  vll pre[2];
  vll suf[2];
  pre[0].resize(n);
  pre[1].resize(n);
  suf[0].resize(n);
  suf[1].resize(n);
  for (ll ind = 0; ind < 2; ind++) {
    ll mod = one + 1;
    if (ind)
      mod = one - 1;
    for (ll i = 0; i < n; i++) {
      if (i) {
        pre[ind][i] = (pre[ind][i - 1] + A[ind][i]) % mod;
      } else {
        pre[ind][i] = A[ind][i];
      }
    }
  }
  for (ll ind = 0; ind < 2; ind++) {
    ll mod = one + 1;
    if (ind)
      mod = one - 1;
    for (ll i = n - 1; i >= 0; i--) {
      if (i != n - 1) {
        suf[ind][i] = (suf[ind][i + 1] + A[ind][i]) % mod;
      } else {
        suf[ind][i] = A[ind][i];
      }
    }
  }

  vll ans(n);
  if (s[0] == '0') {
    ll no = suf[0][1];
    no += POW[0][0];
    no %= (one + 1);
    // cout << no << " ";
    ans[0] = 1 + go(no);
  } else {
    ll no = suf[1][1];
    if (one == 1) {
      ans[0] = 0;
    } else
      ans[0] = 1 + go(no);
  }
  for (ll i = 1; i < n - 1; i++) {
    if (s[i] == '1') {
      if (one == 1) {
        ans[i] = 0;
      } else {
        ll no = pre[1][i - 1] + suf[1][i + 1];
        // cout << i << " " << no << "\\n";
        ans[i] = 1 + go(no);
      }
    } else {
      ll no = pre[0][i - 1] + suf[0][i + 1];
      no += POW[0][i];
      no %= (one + 1);
      ans[i] = 1 + go(no);
    }
  }
  if (s[n - 1] == '0') {
    ll no = pre[0][n - 2];
    no += POW[0][n - 1];
    no %= (one + 1);
    ans[n - 1] = 1 + go(no);
  } else {
    ll no = pre[1][n - 2];
    if (one == 1) {
      ans[n - 1] = 0;
    } else {
      ans[n - 1] = 1 + go(no);
    }
  }

  for (ll i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input00.txt","r",stdin);
  // #endif
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}