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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip> //cout << fixed << setprecision(15) << x << endl;
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1e18;
#define Pint pair<int, int>
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rnr(i, a, b) for (int i = int(a); i >= int(b); i--)
#define rep(i, b) rng(i, 0, b)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
/* -- template -- */

int solve(int bin) {
  if (bin == 0)
    return 0;
  int ret = 1;
  while (bin != 0) {
    bin = bin % __builtin_popcount(bin);
    ++ret;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int modPlus1 = 0, modMinus1 = 0, pow2 = 1, x_popcount = 0;
  rep(i, n) {
    if (x[i] == '1')
      ++x_popcount;
  }
  for (int i = x.size() - 1; i >= 0; --i) {
    modPlus1 = (modPlus1 + pow2 * (x[i] == '1')) % (x_popcount + 1);
    pow2 = (pow2 * 2) % (x_popcount + 1);
  }
  pow2 = 1;
  if (x_popcount > 1) {
    for (int i = x.size() - 1; i >= 0; --i) {
      modMinus1 = (modMinus1 + pow2 * (x[i] == '1')) % (x_popcount - 1);
      pow2 = (pow2 * 2) % (x_popcount - 1);
    }
    pow2 = 1;
  }
  int pow2Plus1 = 1, pow2Minus1 = 1;
  vector<int> ans;
  for (int i = x.size() - 1; i >= 0; --i) {
    if (x[i] == '1') {
      int mod = (x_popcount - 1);
      if (mod == 0) {
        ans.pb(0);
      } else if (x_popcount != 1 && (modMinus1 - pow2Minus1 + mod) % mod == 0) {
        ans.pb(1);
      } else {
        ans.pb(solve((modMinus1 - pow2Minus1 + mod) % mod));
      }
    } else {
      int mod = (x_popcount + 1);
      if ((modPlus1 + pow2Plus1) % mod == 0) {
        ans.pb(1);
      } else {
        ans.pb(solve((modPlus1 + pow2Plus1) % mod));
      }
    }
    if (x_popcount - 1 != 0)
      pow2Minus1 = (pow2Minus1 * 2) % (x_popcount - 1);
    pow2Plus1 = (pow2Plus1 * 2) % (x_popcount + 1);
  }
  rep(i, ans.size()) { cout << ans[ans.size() - 1 - i] << endl; }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip> //cout << fixed << setprecision(15) << x << endl;
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1e18;
#define Pint pair<int, int>
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rnr(i, a, b) for (int i = int(a); i >= int(b); i--)
#define rep(i, b) rng(i, 0, b)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
/* -- template -- */

int solve(int bin) {
  if (bin == 0)
    return 0;
  int ret = 1;
  while (bin != 0) {
    bin = bin % __builtin_popcount(bin);
    ++ret;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int modPlus1 = 0, modMinus1 = 0, pow2 = 1, x_popcount = 0;
  rep(i, n) {
    if (x[i] == '1')
      ++x_popcount;
  }
  for (int i = x.size() - 1; i >= 0; --i) {
    modPlus1 = (modPlus1 + pow2 * (x[i] == '1')) % (x_popcount + 1);
    pow2 = (pow2 * 2) % (x_popcount + 1);
  }
  pow2 = 1;
  if (x_popcount > 1) {
    for (int i = x.size() - 1; i >= 0; --i) {
      modMinus1 = (modMinus1 + pow2 * (x[i] == '1')) % (x_popcount - 1);
      pow2 = (pow2 * 2) % (x_popcount - 1);
    }
    pow2 = 1;
  }
  int pow2Plus1 = 1, pow2Minus1 = 1;
  vector<int> ans;
  for (int i = x.size() - 1; i >= 0; --i) {
    if (x[i] == '1') {
      int mod = (x_popcount - 1);
      if (x_popcount != 1 && (modMinus1 - pow2Minus1 + mod) % mod == 0) {
        ans.pb(1);
      } else {
        ans.pb(solve((modMinus1 - pow2Minus1 + mod) % mod));
      }
    } else {
      int mod = (x_popcount + 1);
      if ((modPlus1 + pow2Plus1) % mod == 0) {
        ans.pb(1);
      } else {
        ans.pb(solve((modPlus1 + pow2Plus1) % mod));
      }
    }
    pow2Minus1 = (pow2Minus1 * 2) % (x_popcount - 1);
    pow2Plus1 = (pow2Plus1 * 2) % (x_popcount + 1);
  }
  rep(i, ans.size()) { cout << ans[ans.size() - 1 - i] << endl; }
}