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 sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, val) memset(v, val, sizeof(v))
#define lcm(a, b) ((a * b) / (__gcd(a, b)))
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
using namespace __gnu_pbds;
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
ll setBit1(ll num, ll idx) { return num | (1ll << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 200000 + 9, mod = 1e9 + 7;
int a[200000 + 9], n;
ll P[4][N], ans[4];
int solve(int val, int f, int pos) {
  if (val == 0) {
    return 0;
  }
  ll res;
  if (f == 2) {
    res = (ans[f] + P[f][pos]) % val;
  } else {
    res = ((ans[f] - P[f][pos]) % val + val) % val;
  }

  ll cur = res;
  bitset<20> p(cur);
  int num = p.count();
  int cnt = 1;
  // cout<<cur<<" "<<num<<endl;
  while (cur != 0) {

    cur = cur % num;
    bitset<20> pp(cur);
    num = pp.count();
    cnt++;
  }

  return cnt;
}
int main() {
  GO();
  cin >> n;
  int cnt = 0;
  string ss;
  cin >> ss;
  for (int i = 0; i < n; i++) {
    a[i] = ss[i] - '0';
    if (a[i] == 1)
      cnt++;
  }

  P[1][0] = 1;
  for (int i = 1; i < N && cnt - 1 > 0; i++) {
    P[1][i] = (P[1][i - 1] * 2) % (cnt - 1);
  }
  P[2][0] = 1;
  for (int i = 1; i < N; i++) {
    P[2][i] = (P[2][i - 1] * 2) % (cnt + 1);
  }

  for (int i = 0; i < n && cnt - 1 > 0; i++) {
    int ni = n - 1 - i;
    if (a[i] == 1) {
      ans[1] = (ans[1] + P[1][ni]) % (cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    int ni = n - 1 - i;
    if (a[i] == 1) {
      ans[2] = (ans[2] + P[2][ni]) % (cnt + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      //  a[i]^=1;
      cout << solve(cnt - 1, 1, n - 1 - i) << "\\n";
      //  a[i]^=1;
    } else {
      //  a[i]^=1;
      cout << solve(cnt + 1, 2, n - 1 - i) << "\\n";
      //    a[i]^=1;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, val) memset(v, val, sizeof(v))
#define lcm(a, b) ((a * b) / (__gcd(a, b)))
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
using namespace __gnu_pbds;
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
ll setBit1(ll num, ll idx) { return num | (1ll << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 200000 + 9, mod = 1e9 + 7;
int a[200000 + 9], n;
ll P[4][N], ans[4];
int solve(int val, int f, int pos) {
  if (val == 0) {
    return 0;
  }
  ll res;
  if (f == 2) {
    res = (ans[f] + P[f][pos]) % val;
  } else {
    res = ((ans[f] - P[f][pos]) % val + val) % val;
  }

  ll cur = res;
  bitset<20> p(cur);
  int num = p.count();
  int cnt = 1;
  // cout<<cur<<" "<<num<<endl;
  while (cur != 0) {

    cur = cur % num;
    bitset<20> pp(cur);
    num = pp.count();
    cnt++;
  }

  return cnt;
}
int main() {
  GO();
  cin >> n;
  int cnt = 0;
  string ss;
  cin >> ss;
  for (int i = 0; i < n; i++) {
    a[i] = ss[i] - '0';
    if (a[i] == 1)
      cnt++;
  }

  P[1][0] = 1;
  for (int i = 1; i < N && cnt > 0; i++) {
    P[1][i] = (P[1][i - 1] * 2) % (cnt - 1);
  }
  P[2][0] = 1;
  for (int i = 1; i < N; i++) {
    P[2][i] = (P[2][i - 1] * 2) % (cnt + 1);
  }

  for (int i = 0; i < n && cnt > 0; i++) {
    int ni = n - 1 - i;
    if (a[i] == 1) {
      ans[1] = (ans[1] + P[1][ni]) % (cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    int ni = n - 1 - i;
    if (a[i] == 1) {
      ans[2] = (ans[2] + P[2][ni]) % (cnt + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      //  a[i]^=1;
      cout << solve(cnt - 1, 1, n - 1 - i) << "\\n";
      //  a[i]^=1;
    } else {
      //  a[i]^=1;
      cout << solve(cnt + 1, 2, n - 1 - i) << "\\n";
      //    a[i]^=1;
    }
  }
}