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

#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define in(x, a, b) (a <= x && x < b)
#define CASET                                                                  \\
  int ___T;                                                                    \\
  scanf("%d", &___T);                                                          \\
  for (int cs = 1; cs <= ___T; cs++)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

ll power(ll a, ll b, ll P) {
  ll result = 1;
  while (b) {
    if (b % 2)
      result = (result * a) % P;
    a = (a * a) % P;
    b /= 2;
  }

  return result;
}

void solve() {
  int n;
  string s;
  cin >> n >> s;

  int cnt = 0;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '1')
      cnt++;

  vector<vi> values(n, vi(2));
  vi sum(2, 0);
  for (int i = 0; i < n; i++) {
    if (cnt - 1 > 0)
      values[i][0] = power(2, n - i - 1, cnt - 1);
    values[i][1] = power(2, n - i - 1, cnt + 1);
    if (cnt - 1 > 0)
      sum[0] = (sum[0] + values[i][0] * (s[i] - '0')) % (cnt - 1);
    sum[1] = (sum[1] + values[i][1] * (s[i] - '0')) % (cnt + 1);
  }

  for (int i = 0; i < n; i++) {
    int m;
    if (s[i] == '0') {
      m = (sum[1] + (values[i][1])) % (cnt + 1);
    } else {
      if (cnt - 1 > 0)
        m = (sum[0] - (values[i][0]) + cnt - 1) % (cnt - 1);
      else {
        cout << 0 << endl;
        continue;
      }
    }

    if (m == 0) {
      cout << 1 << endl;
      continue;
    }

    int ans = 0;
    while (m) {
      int mod = __builtin_popcount(m);
      m %= mod;
      ans++;
    }

    cout << ans + 1 << endl;
  }
}

int main() {
#define MULTITEST 0
#if MULTITEST

  CASET { solve(); }

#else

  solve();

#endif

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define in(x, a, b) (a <= x && x < b)
#define CASET                                                                  \\
  int ___T;                                                                    \\
  scanf("%d", &___T);                                                          \\
  for (int cs = 1; cs <= ___T; cs++)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

ll power(ll a, ll b, ll P) {
  ll result = 1;
  while (b) {
    if (b % 2)
      result = (result * a) % P;
    a = (a * a) % P;
    b /= 2;
  }

  return result;
}

void solve() {
  int n;
  string s;
  cin >> n >> s;

  int cnt = 0;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '1')
      cnt++;

  vector<vi> values(n, vi(2));
  vi sum(2, 0);
  for (int i = 0; i < n; i++) {
    values[i][0] = power(2, n - i - 1, cnt - 1);
    values[i][1] = power(2, n - i - 1, cnt + 1);
    if (cnt - 1 > 0)
      sum[0] = (sum[0] + values[i][0] * (s[i] - '0')) % (cnt - 1);
    sum[1] = (sum[1] + values[i][1] * (s[i] - '0')) % (cnt + 1);
  }

  for (int i = 0; i < n; i++) {
    int m;
    if (s[i] == '0') {
      m = (sum[1] + (values[i][1])) % (cnt + 1);
    } else {
      if (cnt - 1 > 0)
        m = (sum[0] - (values[i][0]) + cnt - 1) % (cnt - 1);
      else {
        cout << 0 << endl;
        continue;
      }
    }

    if (m == 0) {
      cout << 1 << endl;
      continue;
    }

    int ans = 0;
    while (m) {
      int mod = __builtin_popcount(m);
      m %= mod;
      ans++;
    }

    cout << ans + 1 << endl;
  }
}

int main() {
#define MULTITEST 0
#if MULTITEST

  CASET { solve(); }

#else

  solve();

#endif

  return 0;
}