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

#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define popf pop_front
#define p() cout << '\\n'
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\\n'
#define p2(x, y) cout << x << " " << y << '\\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\\n'
#define printv(v)                                                              \\
  for (ll i = 0; i < v.size(); ++i)                                            \\
    cout << v[i] << " ";                                                       \\
  cout << '\\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\\n'
#define mod 1000000007
#define mod1 998244353
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define get(n)                                                                 \\
  ll n;                                                                        \\
  cin >> n
#define getvec(v, n)                                                           \\
  vector<ll> v(n);                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> v[i];
#define getstr(s)                                                              \\
  string s;                                                                    \\
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

ll solve(ll n) {
  ll ans = 0;
  while (n != 0) {
    n = n % countBits(n);
    ++ans;
  }
  return ans;
}

void mainSolve() {

  get(n);
  getstr(s);
  ll one(0);
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      ++one;
  vl power1(n, 1);
  vl power0(n, 1);
  for (int i = 1; i < n; i++) {
    ll temp = one;
    if (one == 1)
      one = 2;
    power1[i] = (power1[i - 1] * 2ll) % (one - 1ll);
    one = temp;
    power0[i] = (power0[i - 1] * 2ll) % (one + 1ll);
  }
  ll x1(0), x0(0);
  ll cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ll temp = one;
      if (one == 1)
        one = 2;
      x1 = (x1 + power1[cnt]) % (one - 1ll);
      one = temp;
      x0 = (x0 + power0[cnt]) % (one + 1ll);
    }
    ++cnt;
  }
  for (int i = 0; i < n; i++) {
    ll currBits = one;
    if (s[i] == '1') {
      if (one == 1) {
        p1(0);
        continue;
      }
      ll curr = x1;
      --one;
      curr = (curr - power1[n - 1 - i]) % one;
      curr = (curr + one) % one;
      curr = 1ll + solve(curr);
      p1(curr);
    } else {
      ++one;
      ll curr = x0;
      curr = (curr + power0[n - 1 - i]) % one;
      curr = 1ll + solve(curr);
      p1(curr);
    }
    one = currBits;
  }
}

int main() {
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // get(t);
  ll t = 1;
  while (t--) {
    mainSolve();
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define popf pop_front
#define p() cout << '\\n'
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\\n'
#define p2(x, y) cout << x << " " << y << '\\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\\n'
#define printv(v)                                                              \\
  for (ll i = 0; i < v.size(); ++i)                                            \\
    cout << v[i] << " ";                                                       \\
  cout << '\\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\\n'
#define mod 1000000007
#define mod1 998244353
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define get(n)                                                                 \\
  ll n;                                                                        \\
  cin >> n
#define getvec(v, n)                                                           \\
  vector<ll> v(n);                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> v[i];
#define getstr(s)                                                              \\
  string s;                                                                    \\
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

ll solve(ll n) {
  ll ans = 0;
  while (n != 0) {
    n = n % countBits(n);
    ++ans;
  }
  return ans;
}

void mainSolve() {

  get(n);
  getstr(s);
  ll one(0);
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      ++one;
  vl power1(n, 1);
  vl power0(n, 1);
  for (int i = 1; i < n; i++) {
    power1[i] = (power1[i - 1] * 2ll) % (one - 1ll);
    power0[i] = (power0[i - 1] * 2ll) % (one + 1ll);
  }
  ll x1(0), x0(0);
  ll cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      x1 = (x1 + power1[cnt]) % (one - 1ll);
      x0 = (x0 + power0[cnt]) % (one + 1ll);
    }
    ++cnt;
  }
  for (int i = 0; i < n; i++) {
    ll currBits = one;
    if (s[i] == '1') {
      if (one == 1) {
        p1(0);
        continue;
      }
      ll curr = x1;
      --one;
      curr = (curr - power1[n - 1 - i]) % one;
      curr = (curr + one) % one;
      curr = 1ll + solve(curr);
      p1(curr);
    } else {
      ++one;
      ll curr = x0;
      curr = (curr + power0[n - 1 - i]) % one;
      curr = 1ll + solve(curr);
      p1(curr);
    }
    one = currBits;
  }
}

int main() {
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // get(t);
  ll t = 1;
  while (t--) {
    mainSolve();
  }
  return 0;
}