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

#include <iostream>
#include <vector>
using namespace std;

#include <vector>
#define debug(x) print(x, #x)
#define debug_1d(arr, len) print(arr, len, #arr)
#define debug_2d(arr, row, col)                                                \\
  print((decltype(&arr[0][0]))arr, row, col, sizeof(*arr) / sizeof(**arr), #arr)
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a1, a2, a3, x, ...) x
#define deb(...)                                                               \\
  CHOOSE((__VA_ARGS__, debug_2d, debug_1d, debug, ~))(__VA_ARGS__)
template <typename T> void print(T x, string tag = "") {
  if (tag.empty())
    cout << x << endl;
  else
    cout << tag << ": " << x << endl;
}
template <typename T> void print(T *ar, int len, string tag = "") {
  if (!tag.empty())
    cout << tag << ": ";
  for (int i = 0; i < len; ++i)
    cout << ar[i] << ' ';
  cout << endl;
}
template <typename T>
void print(T *ar, int row, int col, int C, string tag = "") {
  if (!tag.empty())
    cout << tag << ": " << endl;
  for (int i = 0; i < row; ++i) {
    for (int j = 0; j < col; ++j) {
      cout << ar[i * C + j] << ' ';
    }
    cout << endl;
  }
}
template <typename T> void print(vector<T> v, string tag = "") {
  if (!tag.empty())
    cout << tag << ": ";
  for (T e : v)
    cout << e << ' ';
  cout << endl;
}
template <typename T> void print(vector<vector<T>> v, string tag = "") {
  if (!tag.empty())
    cout << tag << ": " << endl;
  for (auto ev : v) {
    for (auto e : ev)
      cout << e << ' ';
    cout << endl;
  }
}

long long mod(long long a, long long m) {
  a %= m;
  return a >= 0 ? a : a + m;
}

int popcount(long long x) {
  int res = 0;
  while (x > 0) {
    if (x & 1)
      res++;
    x >>= 1;
  }
  return res;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> X(n);
  vector<int> ans(n);
  long long num_of_ones = 0;
  for (int i = 0; i < n; i++) {
    X[i] = s[n - 1 - i] - '0';
    if (X[i] == 1)
      num_of_ones++;
  }
  long long mod_minus = num_of_ones - 1, mod_plus = num_of_ones + 1;
  vector<long long> pow2plus(n + 2), pow2minus(n + 2);
  pow2plus[0] = pow2minus[0] = 1;
  for (int i = 1; i < n + 2; i++) {
    pow2plus[i] = pow2plus[i - 1] * 2 % mod_plus;
    if (mod_minus > 0)
      pow2minus[i] = pow2minus[i - 1] * 2 % mod_minus;
  }
  long long sum_plus = 0, sum_minus = 0;
  for (int i = 0; i < n; i++) {
    sum_plus += X[i] * pow2plus[i];
    if (mod_minus > 0)
      sum_minus += X[i] * pow2minus[i];
  }
  for (int i = 0; i < n; i++) {
    if (X[i] == 0) {
      long long k = mod(sum_plus + pow2plus[i], mod_plus);
      long long a = 1;
      while (k > 0) {
        a++;
        k = k % popcount(k);
      }
      ans[i] = a;
    } else {
      if (mod_minus == 0) {
        ans[i] = 0;
        continue;
      }
      long long k = mod(sum_minus - pow2minus[i], mod_minus);
      long long a = 1;
      while (k > 0) {
        a++;
        k = k % popcount(k);
      }
      ans[i] = a;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#include <vector>
#define debug(x) print(x, #x)
#define debug_1d(arr, len) print(arr, len, #arr)
#define debug_2d(arr, row, col)                                                \\
  print((decltype(&arr[0][0]))arr, row, col, sizeof(*arr) / sizeof(**arr), #arr)
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a1, a2, a3, x, ...) x
#define deb(...)                                                               \\
  CHOOSE((__VA_ARGS__, debug_2d, debug_1d, debug, ~))(__VA_ARGS__)
template <typename T> void print(T x, string tag = "") {
  if (tag.empty())
    cout << x << endl;
  else
    cout << tag << ": " << x << endl;
}
template <typename T> void print(T *ar, int len, string tag = "") {
  if (!tag.empty())
    cout << tag << ": ";
  for (int i = 0; i < len; ++i)
    cout << ar[i] << ' ';
  cout << endl;
}
template <typename T>
void print(T *ar, int row, int col, int C, string tag = "") {
  if (!tag.empty())
    cout << tag << ": " << endl;
  for (int i = 0; i < row; ++i) {
    for (int j = 0; j < col; ++j) {
      cout << ar[i * C + j] << ' ';
    }
    cout << endl;
  }
}
template <typename T> void print(vector<T> v, string tag = "") {
  if (!tag.empty())
    cout << tag << ": ";
  for (T e : v)
    cout << e << ' ';
  cout << endl;
}
template <typename T> void print(vector<vector<T>> v, string tag = "") {
  if (!tag.empty())
    cout << tag << ": " << endl;
  for (auto ev : v) {
    for (auto e : ev)
      cout << e << ' ';
    cout << endl;
  }
}

long long mod(long long a, long long m) {
  a %= m;
  return a >= 0 ? a : a + m;
}

long long pow_mod(long long a, long long n, long long m) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * a) % m;
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}

int popcount(long long x) {
  int res = 0;
  while (x > 0) {
    if (x & 1)
      res++;
    x >>= 1;
  }
  return res;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> X(n);
  vector<int> ans(n);
  long long num_of_ones = 0;
  for (int i = 0; i < n; i++) {
    X[i] = s[n - 1 - i] - '0';
    if (X[i] == 1)
      num_of_ones++;
  }
  long long mod_minus = num_of_ones - 1, mod_plus = num_of_ones + 1;
  vector<long long> pow2plus(n + 2), pow2minus(n + 2);
  pow2plus[0] = pow2minus[0] = 1;
  for (int i = 1; i < n + 2; i++) {
    pow2plus[i] = pow2plus[i - 1] * 2 % mod_plus;
    pow2minus[i] = pow2minus[i - 1] * 2 % mod_minus;
  }
  long long sum_plus = 0, sum_minus = 0;
  for (int i = 0; i < n; i++) {
    sum_plus += X[i] * pow2plus[i];
    sum_minus += X[i] * pow2minus[i];
  }
  for (int i = 0; i < n; i++) {
    if (X[i] == 0) {
      long long k = mod(sum_plus + pow2plus[i], mod_plus);
      long long a = 1;
      while (k > 0) {
        a++;
        k = k % popcount(k);
      }
      ans[i] = a;
    } else {
      long long k = mod(sum_minus - pow2minus[i], mod_minus);
      long long a = 1;
      while (k > 0) {
        a++;
        k = k % popcount(k);
      }
      ans[i] = a;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }

  return 0;
}