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

#include <bits/stdc++.h>

const int N = 220000;
int bin[N], pow0[N], pow1[N];
int n, cnt;

void f(int pow[], int k, int p) {
  int pos = k >> 1;
  if (pow[pos] == -1)
    f(pow, pos, p);

  pow[k] = ((k & 1) == 0 ? 1ll * pow[pos] * pow[pos] % p
                         : 1ll * pow[pos] * pow[pos] * 2 % p);
}
int popcount(int x) {
  if (x == 0)
    return 0;

  int num = 0, x1 = x;
  while (x1) {
    num += x1 & 1;
    x1 >>= 1;
  }

  return (popcount(x % num) + 1);
}

int main() {
  // freopen("D.txt", "r", stdin);

  scanf("%d", &n);

  cnt = 0;
  char ch = getchar();
  for (int i = 1; i <= n; ++i) {
    ch = getchar();
    if (ch == '1') {
      bin[i] = 1;
      ++cnt;
    } else
      bin[i] = 0;
  }

  int cnt0 = cnt - 1, cnt1 = cnt + 1;
  if (cnt0 > 0) {
    memset(pow0, -1, sizeof pow0);
    pow0[0] = 1;
    pow0[1] = 2;
    for (int i = n; i > 1; --i)
      if (pow0[i] == -1)
        f(pow0, i, cnt0);
  }
  memset(pow1, -1, sizeof pow1);
  pow1[0] = 1;
  pow1[1] = 2;
  for (int i = n; i > 1; --i)
    if (pow1[i] == -1)
      f(pow1, i, cnt1);

  int sum0 = 0, sum1 = 0;
  for (int i = 1; i <= n; ++i) {
    if (bin[n - i + 1] == 0)
      continue;
    if (cnt0 != 0)
      sum0 = (sum0 + pow0[i - 1]) % cnt0;
    sum1 = (sum1 + pow1[i - 1]) % cnt1;
  }

  for (int i = 1; i <= n; ++i) {
    if (bin[i] == 0)
      printf("%d\\n", 1 + popcount((sum1 + pow1[n - i]) % cnt1));
    else {
      if (cnt0 == 0)
        printf("0\\n");
      else
        printf("%d\\n", 1 + popcount((sum0 - pow0[n - i] + cnt0) % cnt0));
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

const int N = 220000;
int bin[N], pow0[N], pow1[N];
int n, cnt;

void f(int pow[], int k, int p) {
  int pos = k >> 1;
  if (pow[pos] == -1)
    f(pow, pos, p);

  pow[k] = ((k & 1) == 0 ? 1ll * pow[pos] * pow[pos] % p
                         : 1ll * pow[pos] * pow[pos] * 2 % p);
}
int popcount(int x) {
  if (x == 0)
    return 0;

  int num = 0, x1 = x;
  while (x1) {
    num += x1 & 1;
    x1 >>= 1;
  }

  return (popcount(x % num) + 1);
}

int main() {
  // freopen("D.txt", "r", stdin);

  scanf("%d", &n);

  cnt = 0;
  char ch = getchar();
  for (int i = 1; i <= n; ++i) {
    ch = getchar();
    if (ch == '1') {
      bin[i] = 1;
      ++cnt;
    } else
      bin[i] = 0;
  }

  int cnt0 = cnt - 1, cnt1 = cnt + 1;
  if (cnt0 > 0) {
    memset(pow0, -1, sizeof pow0);
    pow0[0] = 1;
    pow0[1] = 2;
    for (int i = n; i > 1; --i)
      if (pow0[i] == -1)
        f(pow0, i, cnt0);
  }
  memset(pow1, -1, sizeof pow1);
  pow0[0] = 1;
  pow1[1] = 2;
  for (int i = n; i > 1; --i)
    if (pow1[i] == -1)
      f(pow1, i, cnt1);

  int sum0 = 0, sum1 = 0;
  for (int i = 1; i <= n; ++i) {
    if (bin[n - i + 1] == 0)
      continue;
    sum0 = (sum0 + pow0[i - 1]) % cnt0;
    sum1 = (sum1 + pow1[i - 1]) % cnt1;
  }

  for (int i = 1; i <= n; ++i) {
    if (bin[i] == 0)
      printf("%d\\n", 1 + popcount((sum1 + pow1[n - i]) % cnt1));
    else {
      if (cnt0 == 0)
        printf("0\\n");
      else
        printf("%d\\n", 1 + popcount((sum0 - pow0[n - i] + cnt0) % cnt0));
    }
  }

  return 0;
}