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

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
// #include<ext/rope>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
template <typename T> void debug(T x) {
  cout << "value=" << x << endl;
  return;
}
ll fpow(ll a, ll b, ll p) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}
signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  ll n = read();
  string s;
  cin >> s;
  bitset<200001> bit(s);
  // for(ll i=n-1;i>=0;i--) cout<<bit[i];
  // cout<<endl;
  ll more, les, cnt = 0;
  for (ll i = 0; i <= n - 1; i++)
    if (s[i] == '1')
      cnt++;
  more = cnt + 1;
  les = cnt - 1;
  ll mod_more = 0, mod_less = 0;
  for (ll i = 0; i <= n - 1; i++) {
    if (bit[i] == 1) {
      mod_more = (mod_more % more + fpow(2, i, more)) % more;
      if (les != 0)
        mod_less = (mod_less % les + fpow(2, i, les)) % les;
    }
  }
  for (ll i = n - 1; i >= 0; i--) {
    ll ans = 0;
    if (bit[i] == 1) {
      if (cnt == 1)
        printf("0\\n");
      else {
        ans = (mod_less - fpow(2, i, les) + les) % les;
        ll num = 0;
        while (ans > 0) {
          ans = ans % (__builtin_popcountll((unsigned ll)ans));
          num++;
        }
        cout << num + 1 << endl;
      }
    } else {
      ans = (mod_more + fpow(2, i, more)) % more;
      ll num = 0;
      while (ans > 0) {
        ans = ans % (__builtin_popcountll((unsigned ll)ans));
        num++;
      }
      cout << num + 1 << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
// #include<ext/rope>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
template <typename T> void debug(T x) {
  cout << "value=" << x << endl;
  return;
}
ll fpow(ll a, ll b, ll p) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}
signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  ll n = read();
  string s;
  cin >> s;
  bitset<200005> bit(s);
  // for(ll i=n-1;i>=0;i--) cout<<bit[i];
  // cout<<endl;
  ll more, les, cnt = 0;
  for (ll i = 0; i <= n - 1; i++)
    if (s[i] == '1')
      cnt++;
  more = cnt + 1;
  les = cnt - 1;
  ll mod_more = 0, mod_less = 0;
  for (ll i = 0; i <= n - 1; i++) {
    if (bit[i] == 1) {
      mod_more = (mod_more % more + fpow(2, i, more)) % more;
      mod_less = (mod_less % les + fpow(2, i, les)) % les;
    }
  }
  for (ll i = n - 1; i >= 0; i--) {
    ll ans = 0;
    if (bit[i] == 1) {
      ans = (mod_less - fpow(2, i, les) + les) % les;
      ll num = 0;
      while (ans > 0) {
        ans = ans % (__builtin_popcountll((unsigned ll)ans));
        num++;
      }

      cout << num + 1 << endl;
    } else {
      ans = (mod_more + fpow(2, i, more)) % more;
      ll num = 0;
      while (ans > 0) {
        ans = ans % (__builtin_popcountll((unsigned ll)ans));
        num++;
      }
      cout << num + 1 << endl;
    }
  }
  return 0;
}