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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define input_from_file freopen("input.txt", "r", stdin);
using namespace std::chrono;
ll fun(ll a, ll b, ll m) {
  ll ans = 1;
  while (b >= 1) {
    if (b & 1)
      ans = (ans * a) % m;
    a = (a * a) % m;
    b = b / 2;
  }
  return ans % m;
}
int main() {
  // input_from_file
  fastIO ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> dp(2e5 + 5, 0);
  for (int i = 1; i <= 2e5 + 4; i++) {
    ll x = i, val = 0;
    while (x != 0) {
      if (x & 1)
        val++;
      x = x >> 1;
    }
    dp[i] = dp[i % val] + 1;
  }
  // for(int i=0;i<=n;i++) cout<<dp[i]<<" ";cout<<endl;
  ll ctr = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ctr++;
  }

  ll plus_one = 0, minus_one = 0;
  ll val = 1;
  for (int i = n - 1; i >= 0; i--) {
    plus_one += (s[i] - '0') * val;
    plus_one = plus_one % (ctr + 1);
    val = val * 2;
    val = val % (ctr + 1);
  }
  ll x;
  if (ctr == 1) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        x = (plus_one + fun(2, n - 1 - i, ctr + 1)) % (ctr + 1);
        cout << dp[x] + 1 << endl;
      }
    }
    return 0;
  }
  val = 1;
  for (int i = n - 1; i >= 0; i--) {
    minus_one += (s[i] - '0') * val;
    minus_one = minus_one % (ctr - 1);
    val = val * 2;
    val = val % (ctr - 1);
  }
  // cout<<"ctr="<<ctr<<" ,plus_one="<<plus_one<<"
  // ,minus_one="<<minus_one<<endl; cout<<fun(2,21,ctr+1)<<endl<<endl;;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      x = (minus_one - fun(2, n - 1 - i, ctr - 1) + (ctr - 1)) % (ctr - 1);
    else
      x = (plus_one + fun(2, n - 1 - i, ctr + 1)) % (ctr + 1);
    cout << dp[x] + 1 << endl;
  }
  // cout<<"aaaaaaaa";
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define input_from_file freopen("input.txt", "r", stdin);
using namespace std::chrono;
ll fun(ll a, ll b, ll m) {
  ll ans = 1;
  while (b >= 1) {
    if (b & 1)
      ans = (ans * a) % m;
    a = (a * a) % m;
    b = b / 2;
  }
  return ans % m;
}
int main() {
  input_from_file fastIO ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> dp(2e5 + 5, 0);
  for (int i = 1; i <= 2e5 + 4; i++) {
    ll x = i, val = 0;
    while (x != 0) {
      if (x & 1)
        val++;
      x = x >> 1;
    }
    dp[i] = dp[i % val] + 1;
  }
  // for(int i=0;i<=n;i++) cout<<dp[i]<<" ";cout<<endl;
  ll ctr = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ctr++;
  }

  ll plus_one = 0, minus_one = 0;
  ll val = 1;
  for (int i = n - 1; i >= 0; i--) {
    plus_one += (s[i] - '0') * val;
    plus_one = plus_one % (ctr + 1);
    val = val * 2;
    val = val % (ctr + 1);
  }
  ll x;
  if (ctr == 1) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        x = (plus_one + fun(2, n - 1 - i, ctr + 1)) % (ctr + 1);
        cout << dp[x] + 1 << endl;
      }
    }
    return 0;
  }
  val = 1;
  for (int i = n - 1; i >= 0; i--) {
    minus_one += (s[i] - '0') * val;
    minus_one = minus_one % (ctr - 1);
    val = val * 2;
    val = val % (ctr - 1);
  }
  // cout<<"ctr="<<ctr<<" ,plus_one="<<plus_one<<"
  // ,minus_one="<<minus_one<<endl; cout<<fun(2,21,ctr+1)<<endl<<endl;;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      x = (minus_one - fun(2, n - 1 - i, ctr - 1) + (ctr - 1)) % (ctr - 1);
    else
      x = (plus_one + fun(2, n - 1 - i, ctr + 1)) % (ctr + 1);
    cout << dp[x] + 1 << endl;
  }
  // cout<<"aaaaaaaa";
  return 0;
}