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

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\\n';
#define OUT(n) cout << (n) << '\\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS int n;
  cin >> n;
  string X;
  cin >> X;

  int popbase = 0;
  REP(i, n) if (X[i] == '1') popbase++;
  // int ap = 0, am = 0; // f(X)
  int mp = 0, mm = 0; // あまり
  int popp = popbase + 1, popm = popbase - 1;
  REP(i, 2) {
    int p = (i == 0) ? popp : popm;
    if (p == 0)
      continue;
    ll a = 0;
    int j = 0;
    while (j < X.size()) {
      a = (a << 1) + (X[j] == '1' ? 1 : 0);
      j++;
      a %= p;
    }
    if (i == 0) {
      mp = a;
    } else {
      mm = a;
    }
  }

  // int tx = bitset<32>(X).to_ulong();
  cerr << popp << " " << popm << endl;
  cerr << mp << " " << mm << endl;
  // cerr << tx << endl;

  auto calc = [](int x, int m) {
    // cerr << x << " " << m << endl;
    int ret = 0;
    while (x > 0) {
      x %= m;
      bitset<32> bs(x);
      m = bs.count();
      ret++;
    }
    return ret;
  };

  reverse(ALL(X));
  int powp = 1, powm = 1;
  vector<int> ans(n);
  REP(i, n) {
    int a = 0;
    if (X[i] == '0') {
      a = (mp + powp);
      if (a == 0)
        ans[i] = 1;
      else
        ans[i] = calc(a, popp);
      // cerr << "p:" << a << " " <<ans[i] << " " << powp << endl;
    } else {
      if (popm == 0)
        continue;

      a = (mm - powm);
      if (a < 0)
        a += popm;

      if (a == 0)
        ans[i] = 1;
      else
        ans[i] = calc(a, popm);
      // cerr << "m:" << a << " " <<ans[i] << " " << powm << endl;
    }
    powp = powp * 2 % popp;
    if (popm != 0)
      powm = powm * 2 % popm;
    // cerr << powm << " " << (int)(pow(2,i+1)) % popm << endl;
  }

  reverse(ALL(ans));
  REP(i, n) cout << ans[i] << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\\n';
#define OUT(n) cout << (n) << '\\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS int n;
  cin >> n;
  string X;
  cin >> X;

  int popbase = 0;
  REP(i, n) if (X[i] == '1') popbase++;
  // int ap = 0, am = 0; // f(X)
  int mp = 0, mm = 0; // あまり
  int popp = popbase + 1, popm = popbase - 1;
  REP(i, 2) {
    int p = (i == 0) ? popp : popm;
    if (p == 0)
      continue;
    ll a = 0;
    int j = 0;
    while (j < X.size()) {
      a = (a << 1) + (X[j] == '1' ? 1 : 0);
      j++;
      a %= p;
    }
    if (i == 0) {
      mp = a;
    } else {
      mm = a;
    }
  }

  // int tx = bitset<32>(X).to_ulong();
  // cerr << popp << " " << popm << endl;
  // cerr << mp << " " << mm << endl;
  // cerr << tx << endl;

  auto calc = [](int x, int m) {
    // cerr << x << " " << m << endl;
    int ret = 0;
    while (x > 0) {
      x %= m;
      bitset<32> bs(x);
      m = bs.count();
      ret++;
    }
    return ret;
  };

  reverse(ALL(X));
  int powp = 1, powm = 1;
  vector<int> ans(n);
  REP(i, n) {
    int a = 0;
    if (X[i] == '0') {
      a = (mp + powp);
      if (a == 0)
        ans[i] = 1;
      else
        ans[i] = calc(a, popp);
      // cerr << "p:" << a << " " <<ans[i] << " " << powp << endl;
    } else {
      a = (mm - powm);
      if (a < 0)
        a += popm;
      if (popm == 0)
        ans[i] = 0;
      else if (a == 0)
        ans[i] = 1;
      else
        ans[i] = calc(a, popm);
      // cerr << "m:" << a << " " <<ans[i] << " " << powm << endl;
    }
    powp = powp * 2 % popp;
    powm = powm * 2 % popm;
    // cerr << powm << " " << (int)(pow(2,i+1)) % popm << endl;
  }

  reverse(ALL(ans));
  REP(i, n) cout << ans[i] << '\\n';

  return 0;
}