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

#include <bits/stdc++.h>

// #define DEBUG

using namespace std;
constexpr int mxN = 2e5;
bool bit_arr[mxN];
int two_pow_1[mxN + 1];
int two_pow_2[mxN + 1];
int pre_calculate_f[mxN + 1];
template <class T> void print_vect(std::vector<T> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}

void compute_two_pow(int mod, int *arr) {
  if (mod > 0) {
    int last_result = 1 % mod;
    for (int i = 0; i < mxN; i++) {
      arr[i] = last_result;
      last_result = last_result * 2;
      if (last_result >= mod) {
        last_result = last_result % mod;
      }
    }
  }
}

void calculate() {
  pre_calculate_f[1] = 1;
  for (int i = 2; i <= mxN; i++) {
    pre_calculate_f[i] = pre_calculate_f[i % __builtin_popcount(i)] + 1;
  }
}

void solve() {
  uint32_t N;
  cin >> N;
  int count_1 = 0;
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == '0') {
      bit_arr[i] = 0;
    } else {
      bit_arr[i] = 1;
    }
    count_1 += bit_arr[i];
  }
  compute_two_pow(count_1 + 1, two_pow_1);
  compute_two_pow(count_1 - 1, two_pow_2);
  if (count_1 > 1) {
    int original_pop_count_1 = 0;
    int original_pop_count_2 = 0;
    for (int i = 0; i < N; i++) {
      original_pop_count_1 =
          (original_pop_count_1 + two_pow_1[i] * bit_arr[N - i - 1]);
      if (original_pop_count_1 >= (count_1 + 1)) {
        original_pop_count_1 = original_pop_count_1 % (count_1 + 1);
      }
    }
    for (int i = 0; i < N; i++) {
      original_pop_count_2 =
          (original_pop_count_2 + two_pow_2[i] * bit_arr[N - i - 1]);
      if (original_pop_count_2 >= (count_1 - 1)) {
        original_pop_count_2 = original_pop_count_2 % (count_1 - 1);
      }
    }
    vector<int> pop_count_vector;
    pop_count_vector.reserve(N);
    for (int i = 0; i < N; i++) {
      if (bit_arr[i] == 0) {
        int modified_pop_count = (original_pop_count_1 + two_pow_1[N - 1 - i]);
        if (modified_pop_count >= (count_1 + 1)) {
          modified_pop_count = modified_pop_count % (count_1 + 1);
        }
        pop_count_vector.push_back(modified_pop_count);
      } else {
        int modified_pop_count =
            (original_pop_count_2 + count_1 - 1 - two_pow_2[N - 1 - i]);
        if (modified_pop_count >= (count_1 - 1)) {
          modified_pop_count = modified_pop_count % (count_1 - 1);
        }
        pop_count_vector.push_back(modified_pop_count);
      }
    }
    calculate();
    for (int i = 0; i < N; i++) {
      cout << 1 + pre_calculate_f[pop_count_vector[i]] << "\\n";
    }
  } else if (count_1 == 1) {
    int pop_time = (bit_arr[N - 1] == 1) ? 2 : 1;
    for (int i = 0; i < N; i++) {
      if (bit_arr[i] == 1) {
        cout << 0 << "\\n";
      } else {
        if (i == N - 1)
          pop_time++;
        cout << pop_time << "\\n";
      }
    }
  } else {
    for (int i = 0; i < N; i++) {
      cout << 1 << "\\n";
    }
  }
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  /****************************/

#ifdef DEBUG
  int TEST_CASES = 4;
  while (TEST_CASES--) {
#endif // DEBBUG

    solve();

#ifdef DEBUG
  }
#endif // DEBUG

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// #define DEBUG

using namespace std;
constexpr int mxN = 2e5;
bool bit_arr[mxN];
int two_pow_1[mxN + 1];
int two_pow_2[mxN + 1];
int pre_calculate_f[mxN + 1];
template <class T> void print_vect(std::vector<T> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}

void compute_two_pow(int mod, int *arr) {
  if (mod > 0) {
    int last_result = 1;
    for (int i = 0; i < mxN; i++) {
      arr[i] = last_result;
      last_result = last_result * 2;
      if (last_result >= mod) {
        last_result = last_result % mod;
      }
    }
  }
}

void calculate() {
  pre_calculate_f[1] = 1;
  for (int i = 2; i < mxN; i++) {
    pre_calculate_f[i] = pre_calculate_f[i % __builtin_popcount(i)] + 1;
  }
}

void solve() {
  uint32_t N;
  cin >> N;
  int count_1 = 0;
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == '0') {
      bit_arr[i] = 0;
    } else {
      bit_arr[i] = 1;
    }
    count_1 += bit_arr[i];
  }
  compute_two_pow(count_1 + 1, two_pow_1);
  if (count_1 > 0) {
    compute_two_pow(count_1 - 1, two_pow_2);
  }
  // find pop count X
  int original_pop_count_1 = 0;
  int original_pop_count_2 = 0;

  for (int i = 0; i < N; i++) {
    original_pop_count_1 =
        (original_pop_count_1 + two_pow_1[i] * bit_arr[N - i - 1]);
    if (original_pop_count_1 >= (count_1 + 1)) {
      original_pop_count_1 = original_pop_count_1 % (count_1 + 1);
    }
  }
  if (count_1 > 0) {
    for (int i = 0; i < N; i++) {
      original_pop_count_2 =
          (original_pop_count_2 + two_pow_2[i] * bit_arr[N - i - 1]);
      if (original_pop_count_2 > (count_1 - 1)) {
        original_pop_count_2 = original_pop_count_2 % (count_1 - 1);
      }
    }
  }
  vector<int> pop_count_vector;
  pop_count_vector.reserve(N);
  for (int i = 0; i < N; i++) {
    if (bit_arr[i] == 0) {
      int modified_pop_count = (original_pop_count_1 + two_pow_1[N - 1 - i]);
      if (modified_pop_count >= (count_1 + 1)) {
        modified_pop_count = modified_pop_count % (count_1 + 1);
      }
      pop_count_vector.push_back(modified_pop_count);
    } else {
      int modified_pop_count =
          (original_pop_count_2 + count_1 - 1 - two_pow_2[N - 1 - i]);
      if (modified_pop_count >= (count_1 - 1)) {
        modified_pop_count = modified_pop_count % (count_1 - 1);
      }
      pop_count_vector.push_back(modified_pop_count);
    }
  }
  calculate();
  for (int i = 0; i < N; i++) {
    cout << 1 + pre_calculate_f[pop_count_vector[i]] << "\\n";
  }
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  /****************************/

#ifdef DEBUG
  int TEST_CASES = 4;
  while (TEST_CASES--) {
#endif // DEBBUG

    solve();

#ifdef DEBUG
  }
#endif // DEBUG

  return 0;
}