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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#define l(x) (x << 1)
#define r(x) ((x << 1) + 1)
#define IL inline
#define reg register
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int n, i, x;
int p[5], u[5], s[2][200010];
char c[200010];

IL int Abs(int x) { return (x < 0) ? -x : x; }
IL void Swap(int &a, int &b) { a ^= b ^= a ^= b; }
IL int Min(int a, int b) { return (a < b) ? a : b; }
IL int Max(int a, int b) { return (a > b) ? a : b; }

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

IL int Cal(int x) {
  int cnt = 0, y = 0, t = 0;
  while (x) {
    y = 0;
    t = x;
    while (x) {
      y += (x & 1);
      x /= 2;
    }
    cnt++;
    x = t % y;
  }
  return cnt;
}

IL void Ready() {
  int i = 0, j = 0;
  p[0] = x - 1;
  p[1] = x + 1;
  if (p[0] > 0)
    for (i = 0, j = 1; i < n; i++, j = (j << 1) % p[0]) {
      s[0][i] = j;
      if (c[i] == '1')
        u[0] = (u[0] + j) % p[0];
    }
  if (p[1] > 0)
    for (i = 0, j = 1; i < n; i++, j = (j << 1) % p[1]) {
      s[1][i] = j;
      if (c[i] == '1')
        u[1] = (u[1] + j) % p[1];
    }
}

IL int Work(int no, int f) {
  if (!f) {
    return 1 + Cal((u[1] + s[1][no]) % p[1]);
  } else {
    if (p[0] == 0)
      return 0;
    else
      return 1 + Cal((u[0] - s[0][no] + p[0]) % p[0]);
  }
}

int main() {
#ifdef __Marvolo
  freopen("zht.in", "r", stdin);
  freopen("zht.out", "w", stdout);
#endif
  n = read();
  scanf("%s", &c);
  char s = 0;
  for (i = 0; i < n; i++)
    x += c[i] - 48;
  for (i = 0; n - i - 1 > i; i++) {
    s = c[i];
    c[i] = c[n - i - 1];
    c[n - i - 1] = s;
  }
  if (x == 0) {
    for (i = 1; i <= n; i++)
      puts("1");
    return 0;
  }
  Ready();
  for (i = n - 1; i >= 0; i--) {
    if (c[i] == '0')
      printf("%d\\n", Work(i, 0));
    else
      printf("%d\\n", Work(i, 1));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#define l(x) (x << 1)
#define r(x) ((x << 1) + 1)
#define IL inline
#define reg register
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int n, i, x;
int p[5], u[5], s[2][200010];
char c[200010];

IL int Abs(int x) { return (x < 0) ? -x : x; }
IL void Swap(int &a, int &b) { a ^= b ^= a ^= b; }
IL int Min(int a, int b) { return (a < b) ? a : b; }
IL int Max(int a, int b) { return (a > b) ? a : b; }

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

IL int Cal(int x) {
  int cnt = 0, y = 0, t = 0;
  while (x) {
    y = 0;
    t = x;
    while (x) {
      y += (x & 1);
      x /= 2;
    }
    cnt++;
    x = t % y;
  }
  return cnt;
}

IL void Ready() {
  int i = 0, j = 0;
  p[0] = x - 1;
  p[1] = x + 1;
  for (i = 0, j = 1; i < n; i++, j = (j << 1) % p[0]) {
    s[0][i] = j;
    if (c[i] == '1')
      u[0] = (u[0] + j) % p[0];
  }
  for (i = 0, j = 1; i < n; i++, j = (j << 1) % p[1]) {
    s[1][i] = j;
    if (c[i] == '1')
      u[1] = (u[1] + j) % p[1];
  }
}

IL int Work(int no, int f) {
  if (!f) {
    return 1 + Cal((u[1] + s[1][no]) % p[1]);
  } else {
    return 1 + Cal((u[0] - s[0][no] + p[0]) % p[0]);
  }
}

int main() {
#ifdef __Marvolo
  freopen("zht.in", "r", stdin);
  freopen("zht.out", "w", stdout);
#endif
  n = read();
  scanf("%s", &c);
  char s;
  for (i = 0; n - i - 1 > i; i++) {
    s = c[i];
    c[i] = c[n - i - 1];
    c[n - i - 1] = s;
  }
  for (i = 0; i < n; i++)
    x += c[i] - 48;
  if (x == 0) {
    for (i = 1; i <= n; i++)
      puts("0");
    return 0;
  }
  Ready();
  for (i = n - 1; i >= 0; i--) {
    if (c[i] == '0')
      printf("%d\\n", Work(i, 0));
    else
      printf("%d\\n", Work(i, 1));
  }
  return 0;
}