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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) ((x).begin()), ((x).end())

#define READ(x) (cin >> (x))
#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))
#define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No"))

#define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed

bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); }

string xor_bit(const string &s, int n) {
  string s_copy = s;
  if (s_copy[n] == '0') {
    s_copy[n] = '1';
  } else {
    s_copy[n] = '0';
  }
  return s_copy;
}

vector<int> calc_first_mod(const string &s) {
  int s_size = s.size();
  vector<int> digit_mod_more0(s_size, 1);
  vector<int> digit_mod_more1(s_size, 1);

  int mod_more0 = (s[s_size - 1] == '1' ? 1 : 0);
  int mod_more1 = mod_more0;

  int pop_count_default = count(ALL(s), '1');
  for (int i = s_size - 2; i >= 0; i--) {
    if (pop_count_default >= 2) {
      digit_mod_more0[i] =
          (digit_mod_more0[i + 1] * 2) % (pop_count_default - 1);
      mod_more0 = (mod_more0 + (s[i] == '1' ? digit_mod_more0[i] : 0)) %
                  (pop_count_default - 1);
    }
    digit_mod_more1[i] = (digit_mod_more1[i + 1] * 2) % (pop_count_default + 1);
    mod_more1 = (mod_more1 + (s[i] == '1' ? digit_mod_more1[i] : 0)) %
                (pop_count_default + 1);
  }
  vector<int> retv(s_size, 1);
  for (size_t i = 0; i < s_size; i++) {
    if (s[i] == '0') {
      retv[i] = (mod_more1 + digit_mod_more1[i]) % (pop_count_default + 1);
    } else {
      if (pop_count_default >= 2) {
        retv[i] = (mod_more0 + pop_count_default - 1 - digit_mod_more0[i]) %
                  (pop_count_default - 1);
      } else {
        retv[i] = -1;
      }
    }
  }

  return retv;
}

int calc_pop_count(int i) {
  int retv = 0;
  while (i > 0) {
    retv += i % 2;
    i /= 2;
  }
  return retv;
}

vector<int> solve(string X) {
  std::vector<int> retv(X.size(), 1);
  auto first_mods = calc_first_mod(X);
  for (size_t i = 0; i < X.size(); i++) {
    int x = first_mods[i];

    if (x == -1) {
      retv[i] = 0;
      continue;
    }

    if (x == 0) {
      continue;
    }

    while (x > 0) {
      x %= calc_pop_count(x);
      retv[i]++;
    }
  }
  return retv;
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  string X;
  cin >> N;
  cin >> X;

  // main procedure
  vector<int> ans = solve(X);

  // output
  for (auto &&x : ans) {
    cout << x << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) ((x).begin()), ((x).end())

#define READ(x) (cin >> (x))
#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))
#define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No"))

#define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed

bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); }

string xor_bit(const string &s, int n) {
  string s_copy = s;
  if (s_copy[n] == '0') {
    s_copy[n] = '1';
  } else {
    s_copy[n] = '0';
  }
  return s_copy;
}

int calc_first_mod(const string &s) {
  int pop_count = count(ALL(s), '1');
  int retv = 0;
  int current_digit_mod = 1;

  for (int i = s.size() - 1; i >= 0; i--) {
    retv = (retv + (s[i] == '1' ? current_digit_mod : 0)) % pop_count;
    current_digit_mod = (current_digit_mod * 2) % pop_count;
  }

  return retv;
}

int calc_pop_count(int i) {
  int retv = 0;
  while (i > 0) {
    retv += i % 2;
    i /= 2;
  }
  return retv;
}

vector<int> solve(string X) {
  std::vector<int> retv(X.size(), 0);
  for (size_t i = 0; i < X.size(); i++) {
    int x = calc_first_mod(xor_bit(X, i));

    retv[i] = 1;
    if (x == 0) {
      continue;
    }

    while (x > 0) {
      x %= calc_pop_count(x);
      retv[i]++;
    }
  }
  return retv;
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  string X;
  cin >> N;
  cin >> X;

  // main procedure
  vector<int> ans = solve(X);

  // output
  for (auto &&x : ans) {
    cout << x << endl;
  }

  return 0;
}