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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

const int mx = 2e5 + 777;
int memo[mx];

int brute(string s, int idx) {
  if (s[idx] == '1')
    s[idx] = '0';
  else
    s[idx] = '1';
  int ret = 0, ct = 0;
  for (char ch : s)
    if (ch == '1')
      ct++;
  for (char ch : s)
    ret = (ret * 2 + (ch - '0')) % ct;
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  memo[0] = 0;
  for (int i = 1; i < mx; i++)
    memo[i] = memo[i % __builtin_popcount(i)] + 1;
  int N;
  cin >> N;
  string X;
  cin >> X;
  int ct = 0;
  for (int i = 0; i < N; i++)
    if (X[i] == '1')
      ct++;
  vector<int> minus(N + 1, 0), plus(N + 1, 0);
  int x = 1, y = 1;
  for (int i = 1; i <= N; i++) {
    minus[i] = x;
    plus[i] = y;
    if (ct > 1)
      x = (x * 2) % (ct - 1);
    y = (y * 2) % (ct + 1);
  }

  int dec = 0, inc = 0;
  for (int i = 0; i < N; i++) {
    int cur = X[i] - '0';
    if (ct > 1)
      dec = (dec * 2 + cur) % (ct - 1);
    inc = (inc * 2 + cur) % (ct + 1);
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '1' && ct == 1) {
      cout << 0 << "\\n";
    } else {
      int cur = N - i;
      int x = -1;
      if (X[i] == '1')
        x = (dec + (ct - 1) - minus[cur]) % (ct - 1);
      else
        x = (inc + plus[cur]) % (ct + 1);
      int add = memo[x];
      cout << 1 + add << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

const int mx = 2e5 + 777;
int memo[mx];

int brute(string s, int idx) {
  if (s[idx] == '1')
    s[idx] = '0';
  else
    s[idx] = '1';
  int ret = 0, ct = 0;
  for (char ch : s)
    if (ch == '1')
      ct++;
  for (char ch : s)
    ret = (ret * 2 + (ch - '0')) % ct;
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  memo[0] = 0;
  for (int i = 1; i < mx; i++)
    memo[i] = memo[i % __builtin_popcount(i)] + 1;
  int N;
  cin >> N;
  string X;
  cin >> X;
  int ct = 0;
  for (int i = 0; i < N; i++)
    if (X[i] == '1')
      ct++;
  vector<int> minus(N + 1, 0), plus(N + 1, 0);
  int x = 1, y = 1;
  for (int i = 1; i <= N; i++) {
    minus[i] = x;
    plus[i] = y;
    if (ct)
      x = (x * 2) % (ct - 1);
    y = (y * 2) % (ct + 1);
  }

  int dec = 0, inc = 0;
  for (int i = 0; i < N; i++) {
    int cur = X[i] - '0';
    if (ct)
      dec = (dec * 2 + cur) % (ct - 1);
    inc = (inc * 2 + cur) % (ct + 1);
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '1' && ct == 1) {
      cout << 0 << "\\n";
    } else {
      int cur = N - i;
      int x = -1;
      if (X[i] == '1')
        x = (dec + (ct - 1) - minus[cur]) % (ct - 1);
      else
        x = (inc + plus[cur]) % (ct + 1);
      int add = memo[x];
      cout << 1 + add << "\\n";
    }
  }
  return 0;
}