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

#include <bits/stdc++.h>
#define inf 1000000007
using namespace std;

typedef long long ll;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') {
      f = -f;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f * x;
}

int ma1[500005], ma2[500005], dp[500005], k = 0, k1;

int fun(int n) {
  if (n == 0) {
    return 0;
  }
  if (dp[n] != 0) {
    return dp[n];
  }
  int ji = 1;
  int p = n, z = 0;
  while (p) {
    if (p & 1) {
      z++;
    }
    p = p / 2;
  }
  dp[n] = ji + fun(n % z);
  return dp[n];
}

int main() {
  memset(dp, 0, sizeof(dp));
  int n;
  scanf("%d", &n);
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      k++;
    }
  }
  if (k == 1) {
    k1 = k + 1;
    int z = 0, y2;
    for (int i = 0; i < n; i++) {
      z = z * 2 + s[i] - 48;
      z = z % k1;
    }
    y2 = z;
    z = 1;
    for (int i = 0; i < n; i++) {
      z = z % k1;
      ma2[i] = z;
      z = z * 2;
    }
    for (int i = 0; i < n; i++) {
      int p;
      int z;
      if (s[i] == '1') {
        printf("0\\n");
        continue;
      } else {
        p = (y2 + ma2[n - i - 1] + 3 * k1) % k1;
      }
      z = fun(p) + 1;
      printf("%d\\n", z);
    }
    return 0;
  } else if (k == 0) {
    for (int i = 0; i < n; i++) {
      printf("1\\n");
    }
    return 0;
  }
  k1 = k + 1;
  int z = 0, y1, y2;
  k--;
  for (int i = 0; i < n; i++) {
    z = z * 2 + s[i] - 48;
    z = z % k;
  }
  y1 = z;
  z = 0;
  for (int i = 0; i < n; i++) {
    z = z * 2 + s[i] - 48;
    z = z % k1;
  }
  y2 = z;
  z = 1;
  for (int i = 0; i < n; i++) {
    z = z % k;
    ma1[i] = z;
    z = z * 2;
  }
  z = 1;
  for (int i = 0; i < n; i++) {
    z = z % k1;
    ma2[i] = z;
    z = z * 2;
  }
  for (int i = 0; i < n; i++) {
    int p;
    int z;
    if (s[i] == '1') {
      p = (y1 - ma1[n - i - 1] + 3 * k) % k;
    } else {
      p = (y2 + ma2[n - i - 1] + 3 * k1) % k1;
    }
    z = fun(p) + 1;
    printf("%d\\n", z);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define inf 1000000007
using namespace std;

typedef long long ll;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') {
      f = -f;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f * x;
}

int ma1[200005], ma2[200005], dp[200005], k = 0, k1;

int fun(int n) {
  if (n == 0) {
    return 0;
  }
  if (dp[n] != 0) {
    return dp[n];
  }
  int ji = 1;
  int p = n, z = 0;
  while (p) {
    if (p & 1) {
      z++;
    }
    p = p / 2;
  }
  dp[n] = ji + fun(n % z);
  return dp[n];
}

int main() {
  int n;
  scanf("%d", &n);
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      k++;
    }
  }
  k1 = k + 1;
  int z = 0, y1, y2;
  k--;
  for (int i = 0; i < n; i++) {
    z = z * 2 + s[i] - 48;
    z = z % k;
  }
  y1 = z;
  z = 0;
  for (int i = 0; i < s.length(); i++) {
    z = z * 2 + s[i] - 48;
    z = z % k1;
  }
  y2 = z;
  z = 1;
  for (int i = 0; i < n; i++) {
    z = z % k;
    ma1[i] = z;
    z = z * 2;
  }
  z = 1;
  for (int i = 0; i < n; i++) {
    z = z % k1;
    ma2[i] = z;
    z = z * 2;
  }
  for (int i = 0; i < n; i++) {
    int p;
    int z;
    if (s[i] == '1') {
      p = (y1 - ma1[n - i - 1] + k) % k;
    } else {
      p = (y2 + ma2[n - i - 1] + k1) % k1;
    }
    z = fun(p) + 1;
    printf("%d\\n", z);
  }
  return 0;
}