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

#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\\n"
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
// freopen("input.txt", "r", stdin);

int get_cnt(ll x) {
  int ans = 0;
  while (x > 0) {
    int b = __builtin_popcount(x);
    x %= b;
    ans += 1;
  }
  return ans;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  vector<int> ans(n, 0);
  ll cnt_1 = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cnt_1 += 1;
    }
  }
  if (cnt_1 == 1) {
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        if (x.back() == '1' || i == n - 1) {
          cout << 2 << endl;
        } else {
          cout << 1 << endl;
        }
      }
    }
    return 0;
  } else if (cnt_1 == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << endl;
    return 0;
  }
  ll cur_sub = 0;
  ll cur_add = 0;
  ll mod_sub = cnt_1 - 1;
  ll mod_add = cnt_1 + 1;
  reverse(x.begin(), x.end());
  ll fac_sub = 1 % mod_sub;
  ll fac_add = 1 % mod_add;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cur_sub += fac_sub;
      cur_sub %= mod_sub;
      cur_add += fac_add;
      cur_add %= mod_add;
    }
    fac_sub *= 2;
    fac_sub %= mod_sub;
    fac_add *= 2;
    fac_add %= mod_add;
  }
  fac_sub = 1 % mod_sub;
  fac_add = 1 % mod_add;
  vector<int> tmp(n, 0);
  // cout << cnt_1 << endl;
  // cout << cur_sub << " " << cur_add <<endl;
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      tmp[i] = cur_add + fac_add;
      tmp[i] %= mod_add;
    } else {
      tmp[i] = cur_sub - fac_sub;
      tmp[i] = (tmp[i] + (mod_sub * 100)) % mod_sub;
    }
    // cout << tmp[i] << " ";
    if (tmp[i] == 0) {
      ans[i] = 1;
    } else {
      ans[i] = get_cnt(tmp[i]) + 1;
    }
    fac_sub *= 2;
    fac_sub %= (cnt_1 - 1);
    fac_add *= 2;
    fac_add %= (cnt_1 + 1);
  }
  reverse(ans.begin(), ans.end());
  for (auto &val : ans)
    cout << val << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\\n"
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
// freopen("input.txt", "r", stdin);

int get_cnt(ll x) {
  int ans = 0;
  while (x > 0) {
    int b = __builtin_popcount(x);
    x %= b;
    ans += 1;
  }
  return ans;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  vector<int> ans(n, 0);
  ll cnt_1 = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cnt_1 += 1;
    }
  }
  if (cnt_1 == 0) {
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        if (x.back() == '1' || i == n - 1) {
          cout << 1 << endl;
        } else {
          cout << 0 << endl;
        }
      }
    }
    return 0;
  }
  ll cur_sub = 0;
  ll cur_add = 0;
  ll mod_sub = cnt_1 - 1;
  ll mod_add = cnt_1 + 1;
  reverse(x.begin(), x.end());
  ll fac_sub = 1 % mod_sub;
  ll fac_add = 1 % mod_add;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      cur_sub += fac_sub;
      cur_sub %= mod_sub;
      cur_add += fac_add;
      cur_add %= mod_add;
    }
    fac_sub *= 2;
    fac_sub %= mod_sub;
    fac_add *= 2;
    fac_add %= mod_add;
  }
  fac_sub = 1 % mod_sub;
  fac_add = 1 % mod_add;
  vector<int> tmp(n, 0);
  // cout << cnt_1 << endl;
  // cout << cur_sub << " " << cur_add <<endl;
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      tmp[i] = cur_add + fac_add;
      tmp[i] %= mod_add;
    } else {
      tmp[i] = cur_sub - fac_sub;
      tmp[i] = (tmp[i] + (mod_sub * 100)) % mod_sub;
    }
    // cout << tmp[i] << " ";
    if (tmp[i] == 0) {
      ans[i] = 1;
    } else {
      ans[i] = get_cnt(tmp[i]) + 1;
    }
    fac_sub *= 2;
    fac_sub %= (cnt_1 - 1);
    fac_add *= 2;
    fac_add %= (cnt_1 + 1);
  }
  reverse(ans.begin(), ans.end());
  for (auto &val : ans)
    cout << val << endl;
  return 0;
}