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

#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
// ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a, int b, const int &m = MOD) {
  if (b == 0)
    return 1;
  int x = poww(a, b / 2, m);
  x = x * x % m;
  if (b & 1)
    x = x * a % m;
  return x;
}
int ceil(const int &a, const int &b) { return (a + b - 1) / b; }
////Read:
// Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

int mp[400005];

signed main() {
  FASTER;
  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < 400005; i++) {
    int tmp = i;
    int ans = 0;
    while (tmp) {
      tmp = tmp % __builtin_popcountll(tmp);
      ans++;
    }
    mp[i] = ans;
  }

  int cnt = 0;
  for (char c : s)
    cnt += (c == '1');

  int Pp1 = 0, Pm1 = 0;
  for (int i = n - 1, id = 0; i >= 0; i--, id++) {
    if (s[i] == '1') {
      Pp1 = (Pp1 + poww(2, id, cnt + 1)) % (cnt + 1);
      if (cnt >= 2)
        Pm1 = (Pm1 + poww(2, id, cnt - 1)) % (cnt - 1);
    }
  }

  vi cp1mod(n + n, 1);
  for (int i = 1; i < 2 * n; i++) {
    cp1mod[i] = cp1mod[i - 1] * 2 % (cnt + 1);
  }
  vi cm1mod(n + n, 1);
  for (int i = 1; i < 2 * n && cnt > 1; i++) {
    cm1mod[i] = cm1mod[i - 1] * 2 % (cnt - 1);
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int rem = (Pp1 + cp1mod[n - i - 1]) % (cnt + 1);
      int cntt = 1 + mp[rem];
      cout << cntt << "\\n";
    } else {
      if (cnt == 1) {
        cout << 0 << "\\n";
      } else {
        int rem = (Pm1 - cm1mod[n - i - 1] + cnt - 1) % (cnt - 1);
        int cntt = 1 + mp[rem];
        cout << cntt << "\\n";
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
// ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a, int b, const int &m = MOD) {
  if (b == 0)
    return 1;
  int x = poww(a, b / 2, m);
  x = x * x % m;
  if (b & 1)
    x = x * a % m;
  return x;
}
int ceil(const int &a, const int &b) { return (a + b - 1) / b; }
////Read:
// Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

int mp[400005];

signed main() {
  FASTER;
  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < 400005; i++) {
    int tmp = i;
    int ans = 0;
    while (tmp) {
      tmp = tmp % __builtin_popcountll(tmp);
      ans++;
    }
    mp[i] = ans;
  }

  int cnt = 0;
  for (char c : s)
    cnt += (c == '1');

  int Pp1 = 0, Pm1 = 0;
  for (int i = n - 1, id = 0; i >= 0; i--, id++) {
    if (s[i] == '1') {
      Pp1 = (Pp1 + poww(2, id, cnt + 1)) % (cnt + 1);
      if (cnt >= 2)
        Pm1 = (Pm1 + (1LL << id)) % (cnt - 1);
    }
  }

  vi cp1mod(n + n, 1);
  for (int i = 1; i < 2 * n; i++) {
    cp1mod[i] = cp1mod[i - 1] * 2 % (cnt + 1);
  }
  vi cm1mod(n + n, 1);
  for (int i = 1; i < 2 * n && cnt > 1; i++) {
    cm1mod[i] = cm1mod[i - 1] * 2 % (cnt - 1);
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int rem = (Pp1 + cp1mod[n - i - 1]) % (cnt + 1);
      int cntt = 1 + mp[rem];
      cout << cntt << "\\n";
    } else {
      if (cnt == 1) {
        cout << 0 << "\\n";
      } else {
        int rem = (Pm1 - cm1mod[n - i - 1] + cnt - 1) % (cnt - 1);
        int cntt = 1 + mp[rem];
        cout << cntt << "\\n";
      }
    }
  }
}