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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define endl "\\n"
#define sz(x) ((ll)(x).size())
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define prt(x) cout << (x)
#define prtl(x) cout << (x) << endl
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i > b; i--)
#define mp(a, b) make_pair(a, b)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define debug(v) cout << #v << ": " << v << endl;
#define fst first
#define snd second
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using vvc = vector<vector<char>>;
using ll = long long int;
using P = pair<long long int, long long int>;
using Map = map<long long int, long long int>;
ll INF = 1LL << 60;
ll M = 1000000007;

ll solve(ll a, ll &c) {
  if (a == 0) {
    return c;
  }
  if (a == 1) {
    return c + 1;
  }
  ll j = __builtin_popcount(a);
  if (a % j == 0) {
    return c + 1;
  } else {
    c++;
    return solve(a % j, c);
  }
}

int main() {
  ll n;
  string x;
  cin >> n >> x;
  ll c = 0;
  rep(i, 0, n) {
    if (x[i] == '1') {
      c++;
    }
  }
  vll mdl(n + 1), mdr(n + 1);
  ll l = c - 1, r = c + 1;
  // debug(l) debug(r)
  ll ml = 0;
  ll md = 1 % l;
  if (l > 0) {
    mdl[n] = md;
    if (x[n - 1] == '1') {
      ml += md;
    }
    rrep(i, n - 2, -1) {
      md *= 2;
      md %= l;
      mdl[i + 1] = md;
      if (x[i] == '1') {
        ml += md;
      }
      ml %= l;
    }
  }
  ll mr = 0;
  md = 1 % r;
  mdr[n] = md;
  if (x[n - 1] == '1') {
    mr += md;
  }
  rrep(i, n - 2, -1) {
    md *= 2;
    md %= r;
    mdr[i + 1] = md;
    if (x[i] == '1') {
      mr += md;
    }
    mr %= r;
  }
  // debug(ml) debug(mr)
  rep(i, 0, n) {
    ll v;
    ll b = 1;
    if (x[i] == '1') {
      if (l == 0) {
        prtl(0);
      } else {
        v = (ml - mdl[i + 1] + l) % l;
        prtl(solve(v, b));
      }
    } else {
      v = (mr + mdr[i + 1]) % r;
      prtl(solve(v, b));
    }
  }
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define endl "\\n"
#define sz(x) ((ll)(x).size())
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define prt(x) cout << (x)
#define prtl(x) cout << (x) << endl
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i > b; i--)
#define mp(a, b) make_pair(a, b)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define debug(v) cout << #v << ": " << v << endl;
#define fst first
#define snd second
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using vvc = vector<vector<char>>;
using ll = long long int;
using P = pair<long long int, long long int>;
using Map = map<long long int, long long int>;
ll INF = 1LL << 60;
ll M = 1000000007;

ll solve(ll a, ll &c) {
  if (a == 0) {
    return c;
  }
  if (a == 1) {
    return c + 1;
  }
  ll j = __builtin_popcount(a);
  if (a % j == 0) {
    return c + 1;
  } else {
    c++;
    return solve(a % j, c);
  }
}

int main() {
  ll n;
  string x;
  cin >> n >> x;
  ll c = 0;
  rep(i, 0, n) {
    if (x[i] == '1') {
      c++;
    }
  }
  vll mdl(n + 1), mdr(n + 1);
  ll l = c - 1, r = c + 1;
  // debug(l) debug(r)
  ll ml = 0;
  ll md = 1 % l;
  mdl[n] = md;
  if (x[n - 1] == '1') {
    ml += md;
  }
  rrep(i, n - 2, -1) {
    md *= 2;
    md %= l;
    mdl[i + 1] = md;
    if (x[i] == '1') {
      ml += md;
    }
    ml %= l;
  }
  ll mr = 0;
  md = 1 % r;
  mdr[n] = md;
  if (x[n - 1] == '1') {
    mr += md;
  }
  rrep(i, n - 2, -1) {
    md *= 2;
    md %= r;
    mdr[i + 1] = md;
    if (x[i] == '1') {
      mr += md;
    }
    mr %= r;
  }
  // debug(ml) debug(mr)
  rep(i, 0, n) {
    ll v;
    ll b = 1;
    if (x[i] == '1') {
      v = (ml - mdl[i + 1] + l) % l;
      prtl(solve(v, b));
    } else {
      v = (mr + mdr[i + 1]) % r;
      prtl(solve(v, b));
    }
  }
}