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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int pop_count(const string &s) {
  int result = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      result++;
    }
  }
  return result;
}

int pop_count(const int x) { return __builtin_popcount(x); }

int f(int x) {
  int count = 0;
  while (x > 0) {
    x %= pop_count(x);
    count++;
  }
  return count;
}

int calc_mod(const string &xs, int mod, vector<int> mods) {
  if (mod == 0) {
    return 0;
  }
  int result = 0;

  for (int i = 0; i < xs.size(); i++) {
    if (xs[i] == '1') {
      result += mods[xs.size() - i - 1];
      result %= mod;
    }
  }
  return result;
}

vector<int> calc_pow2_mod(int mod, int n) {
  vector<int> mods(n + 1, 0);
  if (mod == 0) {
    return mods;
  }
  mods[0] = 1;
  for (int i = 1; i <= n; i++) {
    mods[i] = (1L * mods[i - 1] * 2) % mod;
  }
  return mods;
}

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string xs;
  cin >> n;
  cin >> xs;

  int p = pop_count(xs);

  vector<int> pow2_mod_p_minus_1 = calc_pow2_mod(p - 1, n);
  vector<int> pow2_mod_p_plus_1 = calc_pow2_mod(p + 1, n);

  int xs_mod_p_minus_1 = calc_mod(xs, p - 1, pow2_mod_p_minus_1);
  int xs_mod_p_plus_1 = calc_mod(xs, p + 1, pow2_mod_p_plus_1);

  vector<int> fx1(n, 0);
  for (int i = 0; i < n; i++) {
    int deg = n - i - 1;
    if (xs[i] == '1') {
      if (p == 1) {
        fx1[i] = -1;
      } else {
        fx1[i] = (xs_mod_p_minus_1 - pow2_mod_p_minus_1[deg] + p - 1) % (p - 1);
      }
    } else {
      // 0
      fx1[i] = (xs_mod_p_plus_1 + pow2_mod_p_plus_1[deg]) % (p + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    if (fx1[i] == -1) {
      cout << 0;
    } else {
      cout << 1 + f(fx1[i]);
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int pop_count(const string &s) {
  int result = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      result++;
    }
  }
  return result;
}

int pop_count(const int x) { return __builtin_popcount(x); }

int f(int x) {
  int count = 0;
  while (x > 0) {
    x %= pop_count(x);
    count++;
  }
  return count;
}

int calc_mod(const string &xs, int mod, vector<int> mods) {
  int result = 0;
  for (int i = 0; i < xs.size(); i++) {
    if (xs[i] == '1') {
      result += mods[xs.size() - i - 1];
      result %= mod;
    }
  }
  return result;
}

vector<int> calc_pow2_mod(int mod, int n) {
  vector<int> mods(n + 1, 0);
  mods[0] = 1;
  for (int i = 1; i <= n; i++) {
    mods[i] = (1L * mods[i - 1] * 2) % mod;
  }
  return mods;
}

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string xs;
  cin >> n;
  cin >> xs;

  int p = pop_count(xs);

  vector<int> pow2_mod_p_minus_1 = calc_pow2_mod(p - 1, n);
  vector<int> pow2_mod_p_plus_1 = calc_pow2_mod(p + 1, n);

  int xs_mod_p_minus_1 = calc_mod(xs, p - 1, pow2_mod_p_minus_1);
  int xs_mod_p_plus_1 = calc_mod(xs, p + 1, pow2_mod_p_plus_1);

  vector<int> fx1(n, 0);
  for (int i = 0; i < n; i++) {
    int deg = n - i - 1;
    if (xs[i] == '1') {
      if (p == 1) {
        fx1[i] = -1;
      } else {
        fx1[i] = (xs_mod_p_minus_1 - pow2_mod_p_minus_1[deg] + p - 1) % (p - 1);
      }
    } else {
      // 0
      fx1[i] = (xs_mod_p_plus_1 + pow2_mod_p_plus_1[deg]) % (p + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    if (fx1[i] == -1) {
      cout << 0;
    } else {
      cout << 1 + f(fx1[i]);
    }
    cout << endl;
  }

  return 0;
}