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

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

int main() {
  long long n;
  cin >> n;
  string s;
  cin >> s;

  long long original_pop_count = 0;

  for (long long i = 0; i < n; i++) {
    if (s[i] == '1')
      original_pop_count++;
  }
  long long one_pop_count = original_pop_count - 1;
  long long zero_pop_count = original_pop_count + 1;

  long long one_mod = 0, zero_mod = 0;

  for (long long i = 0; i < n; i++) {
    if (one_pop_count != 0)
      one_mod = (one_mod * 2 + (s[i] - '0')) % one_pop_count;
    zero_mod = (zero_mod * 2 + (s[i] - '0')) % zero_pop_count;
  }

  // f
  vector<long long> f(220000, 0);
  vector<long long> pop_count(220000, 0);
  for (long long i = 1; i < 220000; i++) {
    pop_count[i] = pop_count[i / 2] + i % 2;
    f[i] = f[i % pop_count[i]] + 1;
  }

  // pow2
  vector<long long> one_pow2(220000, 1);
  vector<long long> zero_pow2(220000, 1);

  for (long long i = 1; i < 220000; i++) {
    if (one_pop_count != 0)
      one_pow2[i] = one_pow2[i - 1] * 2 % one_pop_count;
    zero_pow2[i] = zero_pow2[i - 1] * 2 % zero_pop_count;
  }
  // printf("one_mod %lld zero_mod %lld\\n", one_mod, zero_mod);

  for (long long i = n - 1; i >= 0; i--) {
    if (s[n - 1 - i] == '1') {
      if (one_pop_count != 0) {
        long long nxt = one_mod;
        // todo
        nxt -= one_pow2[i];
        if (nxt < 0)
          nxt += one_pop_count;
        nxt %= one_pop_count;
        printf("%lld\\n", f[nxt] + 1);
      } else
        printf("0\\n");

    } else {
      long long nxt = zero_mod;
      // todo
      nxt += zero_pow2[i];
      nxt %= zero_pop_count;
      printf("%lld\\n", f[nxt] + 1);
    }
  }

  return 0;
}

Error: WA

Fixed submission:

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

long long popcount(long long n, long long count) {
  // printf("n %lld count %lld\\n", n, count);
  long long count_1 = 0;
  long long n2 = n;
  while (n2 > 0) {
    if (n2 % 2 == 1) {
      count_1++;
    }
    n2 /= 2;
  }
  // printf("count_1 %lld\\n", count_1);
  long long amari;
  amari = n % count_1;
  // printf("amari %lld\\n", amari);
  count++;
  if (amari == 0) {
    long long ans = count;
    // printf("ans %lld\\n", ans);
    return ans;
  } else {
    return popcount(amari, count);
  }
}

int main() {
  long long n;
  cin >> n;
  string s;
  cin >> s;

  long long original_pop_count = 0;

  for (long long i = 0; i < n; i++) {
    if (s[i] == '1')
      original_pop_count++;
  }
  long long one_pop_count = original_pop_count - 1;
  long long zero_pop_count = original_pop_count + 1;

  long long one_mod = 0, zero_mod = 0;

  for (long long i = n - 1; i >= 0; i--) {
    if (one_pop_count != 0)
      one_mod = (one_mod * 2 + (s[i] - '0')) % one_pop_count;
    zero_mod = (zero_mod * 2 + (s[i] - '0')) % zero_pop_count;
  }

  // f
  vector<long long> f(220000, 0);
  vector<long long> pop_count(220000, 0);
  for (long long i = 1; i < 220000; i++) {
    pop_count[i] = pop_count[i / 2] + i % 2;
    f[i] = f[i % pop_count[i]] + 1;
  }

  // pow2
  vector<long long> one_pow2(220000, 1);
  vector<long long> zero_pow2(220000, 1);

  for (long long i = 1; i < 220000; i++) {
    if (one_pop_count != 0)
      one_pow2[i] = one_pow2[i - 1] * 2 % one_pop_count;
    zero_pow2[i] = zero_pow2[i - 1] * 2 % zero_pop_count;
  }
  // printf("one_mod %lld zero_mod %lld\\n", one_mod, zero_mod);

  for (long long i = n - 1; i >= 0; i--) {
    if (s[n - 1 - i] == '1') {
      if (one_pop_count != 0) {
        long long nxt = one_mod;
        // todo
        nxt -= one_pow2[i];
        printf("%lld\\n", f[nxt] + 1);
      } else
        printf("0\\n");

    } else {
      long long nxt = zero_mod;
      // todo
      nxt += zero_pow2[i];
      printf("%lld\\n", f[nxt] + 1);
    }
  }

  return 0;
}