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

#include "bits/stdc++.h"
using namespace std;
#define IOS                                                                    \\
  {                                                                            \\
    ios::sync_with_stdio(false);                                               \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define pb push_back
#define nl cout << "\\n"
#define endl "\\n"
#define ll long long
#define ld long double
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c.size())
#define mp make_pair
#define mt make_tuple
#define get(x, c) get<x>(c)
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define f first
#define s second

inline ll max(ll a, ll b) { return ((a > b) ? a : b); }
inline ll min(ll a, ll b) { return ((a > b) ? b : a); }
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  a %= b;
  return gcd(b, a);
}
ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll a = power(x, y / 2);
  if (y % 2 == 0)
    return a * a;
  else
    return x * a * a;
}
ll powermod(ll x, ll ex, ll md) {
  ll ans = 1ll;
  while (ex > 0) {
    if (ex & 1ll)
      ans = (ans * x) % md;
    ex >>= 1ll;
    x = (x * x) % md;
  }
  return ans;
}

const ll inf = 1e18 + 9;
const ll mod = 1e9 + 7;
const ld PI = acos(-1);
const ld eps = 1e-9;

const ll N = 1e5 + 11;
ll n;
string x;
ll count_one(string &x) {
  ll ct = 0;
  rep(i, sz(x)) ct += (x[i] == '1');
  return ct;
}
ll convert(string &x, ll mod) {
  if (mod == 0)
    return 0;
  ll ans = 0;
  rep(i, sz(x)) {
    if (x[i] == '1') {
      ans += powermod(2, sz(x) - 1 - i, mod);
      ans %= mod;
    }
  }
  return ans;
}
ll val(ll index, ll pres, ll mod) {
  if (mod == 0)
    return 0;
  if (x[index] == '1') {
    pres -= powermod(2, sz(x) - index - 1, mod);
    pres = (pres + mod) % mod;
  } else {
    pres += powermod(2, sz(x) - index - 1, mod);
    pres %= mod;
  }
  return (pres + mod) % mod;
}
void solve() {
  cin >> n >> x;
  ll one = count_one(x);
  // cout << x << " : " << one << endl; return;
  ll val_inc = convert(x, one + 1);
  ll val_dec = convert(x, one - 1);
  // cout << one <<" " << val_inc << " " << val_dec << endl;
  // return;
  rep(i, sz(x)) {
    ll op = 1;
    if (x[i] == '1') {
      if (one - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      ll num = val(i, val_dec, one - 1);
      // cout << i <<" : " << num << endl;
      while (num) {
        op++;
        num %= (__builtin_popcountll(num));
      }
    } else {
      ll num = val(i, val_inc, one + 1);
      // cout << i <<" : " << num << endl;
      while (num) {
        op++;
        num %= (__builtin_popcountll(num));
      }
    }
    cout << op << endl;
  }
}
int main() {
  IOS;
  cout << fixed << setprecision(20);
  ll NTC = 1;
  // cin >> NTC;
  ll PTC = 0;
  while (PTC++ < NTC) {
    // cout << "Case #" << PTC << ":" << ' ';
    solve();
    cout << "\\n";
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define IOS                                                                    \\
  {                                                                            \\
    ios::sync_with_stdio(false);                                               \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define pb push_back
#define nl cout << "\\n"
#define endl "\\n"
#define ll long long
#define ld long double
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c.size())
#define mp make_pair
#define mt make_tuple
#define get(x, c) get<x>(c)
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define f first
#define s second

inline ll max(ll a, ll b) { return ((a > b) ? a : b); }
inline ll min(ll a, ll b) { return ((a > b) ? b : a); }
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  a %= b;
  return gcd(b, a);
}
ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll a = power(x, y / 2);
  if (y % 2 == 0)
    return a * a;
  else
    return x * a * a;
}
ll powermod(ll x, ll ex, ll md) {
  ll ans = 1ll;
  while (ex > 0) {
    if (ex & 1ll)
      ans = (ans * x) % md;
    ex >>= 1ll;
    x = (x * x) % md;
  }
  return ans;
}

const ll inf = 1e18 + 9;
const ll mod = 1e9 + 7;
const ld PI = acos(-1);
const ld eps = 1e-9;

const ll N = 1e5 + 11;
ll n;
string x;
ll count_one(string &x) {
  ll ct = 0;
  rep(i, sz(x)) ct += (x[i] == '1');
  return ct;
}
ll convert(string &x, ll mod) {
  ll ans = 0;
  rep(i, sz(x)) {
    if (x[i] == '1') {
      ans += powermod(2, sz(x) - 1 - i, mod);
      ans %= mod;
    }
  }
  return ans;
}
ll val(ll index, ll pres, ll mod) {
  if (x[index] == '1') {
    pres -= powermod(2, sz(x) - index - 1, mod);
    pres = (pres + mod) % mod;
  } else {
    pres += powermod(2, sz(x) - index - 1, mod);
    pres %= mod;
  }
  return (pres + mod) % mod;
}
void solve() {
  cin >> n >> x;
  ll one = count_one(x);
  // cout << x << " : " << one << endl; return;
  ll val_inc = convert(x, one + 1);
  ll val_dec = convert(x, one - 1);
  // cout << one <<" " << val_inc << " " << val_dec << endl;
  // return;
  rep(i, sz(x)) {
    ll op = 1;
    if (x[i] == '1') {
      ll num = val(i, val_dec, one - 1);
      while (num) {
        op++;
        num %= (__builtin_popcountll(num));
      }
    } else {
      ll num = val(i, val_inc, one + 1);
      while (num) {
        op++;
        num %= (__builtin_popcountll(num));
      }
    }
    cout << op << endl;
  }
}
int main() {
  IOS;
  cout << fixed << setprecision(20);
  ll NTC = 1;
  // cin >> NTC;
  ll PTC = 0;
  while (PTC++ < NTC) {
    // cout << "Case #" << PTC << ":" << ' ';
    solve();
    cout << "\\n";
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}