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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;
#define endl '\\n'

template <typename T> inline bool chmin(T &x, T a) {
  if (x >= a) {
    x = a;
    return true;
  }
  return false;
}

template <typename T> inline bool chmax(T &x, T a) {
  if (x <= a) {
    x = a;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(0);

  int N;
  cin >> N;
  vector<bool> v(N, false);

  int n = 0;
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    v[i] = (c == '1');
    if (v[i]) {
      n++;
    }
  }

  int x0 = n + 1;
  int x1 = n - 1;

  int res0 = 0, res1 = 0;
  vector<int> pow0(N, 0), pow1(N, 0);
  if (x0 >= 1) {
    res0 = 0;
    for (int i = 0; i < N; i++) {
      res0 *= 2;
      if (v[i])
        res0++;
      res0 %= x0;
    }
    pow0[N - 1] = 1 % x0;
    for (int i = N - 2; i >= 0; i--) {
      pow0[i] = pow0[i + 1] * 2;
      pow0[i] %= x0;
    }
  }
  if (x1 >= 1) {
    res1 = 0;
    for (int i = 0; i < N; i++) {
      res1 *= 2;
      if (v[i])
        res1++;
      res1 %= x1;
    }
    pow1[N - 1] = 1 % x1;
    for (int i = N - 2; i >= 0; i--) {
      pow1[i] = pow1[i + 1] * 2;
      pow1[i] %= x1;
    }
  }

  for (int i = 0; i < N; i++) {
    if (v[i] == false) { // x0
      int temp = (res0 + pow0[i]) % x0;
      int ans = 1;
      int a = temp;
      while (a > 0) {
        int n = a;
        int b = 0;
        while (n > 0) {
          if (n % 2 == 1) {
            b++;
          }
          n /= 2;
        }
        ans++;
        a %= b;
      }
      cout << ans << endl;
    } else { // x1
      if (x1 == 0) {
        cout << 0 << endl;
        continue;
      }
      int temp = (res1 - pow1[i] + x1) % x1;
      int ans = 1;
      int a = temp;
      while (a > 0) {
        int n = a;
        int b = 0;
        while (n > 0) {
          if (n % 2 == 1) {
            b++;
          }
          n /= 2;
        }
        ans++;
        a %= b;
      }
      cout << ans << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;
#define endl '\\n'

template <typename T> inline bool chmin(T &x, T a) {
  if (x >= a) {
    x = a;
    return true;
  }
  return false;
}

template <typename T> inline bool chmax(T &x, T a) {
  if (x <= a) {
    x = a;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(0);

  int N;
  cin >> N;
  vector<bool> v(N, false);

  int n = 0;
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    v[i] = (c == '1');
    if (v[i]) {
      n++;
    }
  }

  int x0 = n + 1;
  int x1 = n - 1;

  int res0 = 0, res1 = 0;
  vector<int> pow0(N, 0), pow1(N, 0);
  if (x0 >= 1) {
    res0 = 0;
    for (int i = 0; i < N; i++) {
      res0 *= 2;
      if (v[i])
        res0++;
      res0 %= x0;
    }
    pow0[N - 1] = 1 % x0;
    for (int i = N - 2; i >= 0; i--) {
      pow0[i] = pow0[i + 1] * 2;
      pow0[i] %= x0;
    }
  }
  if (x1 >= 1) {
    res1 = 0;
    for (int i = 0; i < N; i++) {
      res1 *= 2;
      if (v[i])
        res1++;
      res1 %= x1;
    }
    pow1[N - 1] = 1 % x1;
    for (int i = N - 2; i >= 0; i--) {
      pow1[i] = pow1[i + 1] * 2;
      pow1[i] %= x1;
    }
  }

  for (int i = 0; i < N; i++) {
    if (v[i] == false) { // x0
      int temp = (res0 + pow0[i]) % x0;
      int ans = 1;
      int a = temp;
      while (a > 0) {
        int n = a;
        int b = 0;
        while (n > 0) {
          if (n % 2 == 1) {
            b++;
          }
          n /= 2;
        }
        ans++;
        a %= b;
      }
      cout << ans << endl;
    } else { // x1
      int temp = (res1 - pow1[i] + x1) % x1;
      int ans = 1;
      int a = temp;
      while (a > 0) {
        int n = a;
        int b = 0;
        while (n > 0) {
          if (n % 2 == 1) {
            b++;
          }
          n /= 2;
        }
        ans++;
        a %= b;
      }
      cout << ans << endl;
    }
  }

  return 0;
}