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

// Author rahuliitkgp

/*A thing of beauty is a joy forever,
Its loveliness increases,
it will never pass into nothingness.*/
// Men at Work :)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;

#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void cpp() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll power(ll a, ll b, ll M) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return power((a * a) % M, b / 2, M);
  return (a * power(a, b - 1, M)) % M;
}

int main() {
  cpp();
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll bitsOn = 0;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] - '0') {
      bitsOn++;
    }
  }
  ll numModMP1 = 0;
  ll numModMM1 = 0;
  bitsOn++;
  for (ll i = s.size() - 1; i >= 0; i--) {
    if (s[i] == '1') {
      numModMP1 += power(2, (s.size() - 1 - i), bitsOn);
      numModMP1 %= bitsOn;
    }
  }
  numModMP1 = (numModMP1 + bitsOn) % bitsOn;
  bitsOn--;
  if (bitsOn > 1) {
    bitsOn--;
    for (ll i = s.size() - 1; i >= 0; i--) {
      if (s[i] == '1') {
        numModMM1 += power(2, (s.size() - 1 - i), bitsOn);
        numModMM1 %= (bitsOn);
      }
    }
    numModMM1 = (numModMM1 + (bitsOn)) % (bitsOn);
    bitsOn++;
  }
  vector<ll> dp(200002);
  dp[1] = 1;
  for (ll i = 2; i < dp.size(); i++) {
    ll x = setbits(i);
    dp[i] = 1 + dp[((i % x) + x) % x];
  }
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      ll y = bitsOn + 1;
      ll x = ((numModMP1 + power(2, (s.size() - 1 - i), y)) % y + y) % y;
      cout << (dp[x] + 1) << endl;
    } else {
      if (bitsOn == 1) {
        cout << 0 << endl;
      } else {
        ll y = bitsOn - 1;
        ll x = ((numModMM1 - power(2, (s.size() - 1 - i), y)) % y + y) % y;
        cout << dp[x] + 1 << endl;
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

// Author rahuliitkgp

/*A thing of beauty is a joy forever,
Its loveliness increases,
it will never pass into nothingness.*/
// Men at Work :)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;

#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void cpp() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll power(ll a, ll b, ll M) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return power((a * a) % M, b / 2, M);
  return (a * power(a, b - 1, M)) % M;
}

int main() {
  cpp();
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll num = 0;
  ll bitsOn = 0;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] - '0') {
      bitsOn++;
    }
  }
  for (ll i = s.size() - 1; i >= 0; i--) {
    num += power(2, (s.size() - 1 - i), bitsOn);
    num %= bitsOn;
  }
  num = (num + bitsOn) % bitsOn;
  vector<ll> dp(200002);
  dp[1] = 1;
  for (ll i = 2; i < dp.size(); i++) {
    ll x = setbits(i);
    dp[i] = 1 + dp[((i % x) + x) % x];
  }
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      ll y = bitsOn + 1;
      ll x = ((num + power(2, (s.size() - 1 - i), y)) % y + y) % y;
      cout << (dp[x] + 1) << endl;
    } else {
      if (bitsOn == 1) {
        cout << 1 << endl;
      } else {
        ll y = bitsOn - 1;
        ll x = ((num - power(2, (s.size() - 1 - i), y)) % y + y) % y;
        cout << dp[x] + 1 << endl;
      }
    }
  }
  return 0;
}