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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define MP make_pair
#define PB push_back
#define PF push_front
#define F first
#define S second
#define endl "\\n"
#define pi 3.14159265358979323846264338327950288
#define MOD 1000000
#define INF -100000000000000
#define ordered_set                                                            \\
  tree<pair<ll int, ll int>, null_type, less<pair<ll int, ll int>>,            \\
       rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#define lld long double
using namespace __gnu_pbds;
ll int setbits(ll int n) {
  ll int count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
int main() {
  fast;
  //  #ifndef ONLINE_JUDGE
  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
  //  #endif
  ll int n;
  cin >> n;
  string s;
  cin >> s;
  ll int i, c = 0, less, more, w, val1 = 0, val2 = 0, temp, ans;
  for (i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      c++;
  }
  ll int pwr[n], pwr1[n];
  less = c - 1;
  ll int f = 0;
  if (less == 0) {
    f = 1;
    less = INT_MAX;
  }
  more = c + 1;
  pwr[0] = 1;
  pwr1[0] = 1;
  // cout<<less<<" "<<more<<endl;
  for (i = 1; i < n; i++) {
    pwr[i] = (pwr[i - 1] * 2) % less;
    pwr1[i] = (pwr1[i - 1] * 2) % more;
  }
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      val1 = (val1 + pwr[n - 1 - i]) % less;
      val2 = (val2 + pwr1[n - 1 - i]) % more;
    }
  }
  // cout<<val1<<" "<<val2<<endl;

  for (i = 0; i < n; i++) {
    if (s[i] == '0')
      temp = (val2 + pwr1[n - 1 - i]) % more;
    else
      temp = (val1 - pwr[n - 1 - i] + less) % less;
    if (s[i] == '1' && f == 1) {
      cout << 0 << endl;
      continue;
    }
    ans = 1;
    while (temp != 0) {
      w = setbits(temp);
      temp = temp % w;
      ans++;
    }
    cout << ans << endl;
  }
  //	ll int z=5%(-2);cout<<z<<endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define MP make_pair
#define PB push_back
#define PF push_front
#define F first
#define S second
#define endl "\\n"
#define pi 3.14159265358979323846264338327950288
#define MOD 1000000
#define INF -100000000000000
#define ordered_set                                                            \\
  tree<pair<ll int, ll int>, null_type, less<pair<ll int, ll int>>,            \\
       rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#define lld long double
using namespace __gnu_pbds;
ll int setbits(ll int n) {
  ll int count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
int main() {
  fast;
  //  #ifndef ONLINE_JUDGE
  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
  //  #endif
  ll int n;
  cin >> n;
  string s;
  cin >> s;
  ll int i, c = 0, less, more, w, val1 = 0, val2 = 0, temp, ans;
  for (i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      c++;
  }
  ll int pwr[n], pwr1[n];
  less = c - 1;
  more = c + 1;
  pwr[0] = 1;
  pwr1[0] = 1;
  // cout<<less<<" "<<more<<endl;
  for (i = 1; i < n; i++) {
    pwr[i] = (pwr[i - 1] * 2) % less;
    pwr1[i] = (pwr1[i - 1] * 2) % more;
  }
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      val1 = (val1 + pwr[n - 1 - i]) % less;
      val2 = (val2 + pwr1[n - 1 - i]) % more;
    }
  }
  // cout<<val1<<" "<<val2<<endl;

  for (i = 0; i < n; i++) {
    if (s[i] == '0')
      temp = (val2 + pwr1[n - 1 - i]) % more;
    else
      temp = (val1 - pwr[n - 1 - i] + less) % less;

    ans = 1;
    while (temp != 0) {
      w = setbits(temp);
      temp = temp % w;
      ans++;
    }
    cout << ans << endl;
  }
}