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

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
const int MAXN = 1000005, MAXM = 30000000;
int P;
#define L 1048576
#define real long double
#define eps 1e-9
char _buf[L], *S, *T, _buf1[L], *S1 = _buf1;
inline char gc() {
  if (S == T) {
    T = (S = _buf) + fread(_buf, 1, L, stdin);
    if (S == T)
      return 0;
  }
  return *S++;
}
template <class T> void get(T &x) {
  char c;
  for (c = gc(); (c < '0' || c > '9') && c != '-'; c = gc())
    ;
  bool b = 0;
  if (c == '-') {
    b = 1;
    c = gc();
  }
  for (x = c ^ '0', c = gc(); c >= '0' && c <= '9'; c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  if (b)
    x = -x;
}
inline void pc(char c) {
  if (S1 == _buf1 + L) {
    fwrite(_buf1, 1, L, stdout);
    S1 = _buf1;
  }
  *S1++ = c;
}
void out(ll x) {
  if (x > 9)
    out(x / 10);
  pc(x % 10 ^ '0');
}
int Pow(int x, int y) {
  int s = 1;
  for (; y; y >>= 1, x = (ll)x * x % P)
    if (y & 1)
      s = (ll)s * x % P;
  return s;
}
int A(int x) { return x < 0 ? -x : x; }
int n, i, j, p[2], a[MAXN], t[2][MAXN], s[2], f[MAXN], o[MAXN];
char c[MAXN];
int main() {
  scanf("%d%s", &n, c);
  for (i = 0; i < n; i++)
    a[i] = c[i] ^ '0';
  for (i = 0; i < n; i++)
    p[0] += a[i];
  p[1] = max(p[0] - 1, 1);
  p[0]++;
  t[0][0] = 1 % p[0];
  t[1][0] = (p[1] - 1) % p[1];
  for (i = 0; i < 2; i++)
    for (j = 1; j <= n; j++)
      t[i][j] = t[i][j - 1] * 2 % p[i];
  for (i = 0; i < 2; i++)
    for (j = 0; j < n; j++)
      s[i] = (s[i] * 2 + a[j]) % p[i];
  for (i = 1; i <= n; i++)
    o[i] = o[i >> 1] + (i & 1);
  for (i = 1; i <= n; i++)
    f[i] = f[i % o[i]] + 1;
  for (i = 0; i < n; i++) {
    if (a[i] == 1 && p[0] == 2)
      out(0);
    else
      out(f[(s[a[i]] + t[a[i]][n - 1 - i]) % p[a[i]]] + 1);
    pc('\\n');
  }
  fwrite(_buf1, 1, S1 - _buf1, stdout);
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
const int MAXN = 1000005, MAXM = 30000000;
int P;
#define L 1048576
#define real long double
#define eps 1e-9
char _buf[L], *S, *T, _buf1[L], *S1 = _buf1;
inline char gc() {
  if (S == T) {
    T = (S = _buf) + fread(_buf, 1, L, stdin);
    if (S == T)
      return 0;
  }
  return *S++;
}
template <class T> void get(T &x) {
  char c;
  for (c = gc(); (c < '0' || c > '9') && c != '-'; c = gc())
    ;
  bool b = 0;
  if (c == '-') {
    b = 1;
    c = gc();
  }
  for (x = c ^ '0', c = gc(); c >= '0' && c <= '9'; c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  if (b)
    x = -x;
}
inline void pc(char c) {
  if (S1 == _buf1 + L) {
    fwrite(_buf1, 1, L, stdout);
    S1 = _buf1;
  }
  *S1++ = c;
}
void out(ll x) {
  if (x > 9)
    out(x / 10);
  pc(x % 10 ^ '0');
}
int Pow(int x, int y) {
  int s = 1;
  for (; y; y >>= 1, x = (ll)x * x % P)
    if (y & 1)
      s = (ll)s * x % P;
  return s;
}
int A(int x) { return x < 0 ? -x : x; }
int n, i, j, p[2], a[MAXN], t[2][MAXN], s[2], f[MAXN], o[MAXN];
char c[MAXN];
int main() {
  scanf("%d%s", &n, c);
  for (i = 0; i < n; i++)
    a[i] = c[i] ^ '0';
  for (i = 0; i < n; i++)
    p[0] += a[i];
  p[1] = p[0] - 1;
  p[0]++;
  t[0][0] = 1 % p[0];
  t[1][0] = (p[1] - 1) % p[1];
  for (i = 0; i < 2; i++)
    for (j = 1; j <= n; j++)
      t[i][j] = t[i][j - 1] * 2 % p[i];
  for (i = 0; i < 2; i++)
    for (j = 0; j < n; j++)
      s[i] = (s[i] * 2 + a[j]) % p[i];
  for (i = 1; i <= n; i++)
    o[i] = o[i >> 1] + (i & 1);
  for (i = 1; i <= n; i++)
    f[i] = f[i % o[i]] + 1;
  for (i = 0; i < n; i++) {
    out(f[(s[a[i]] + t[a[i]][n - 1 - i]) % p[a[i]]] + 1);
    pc('\\n');
  }
  fwrite(_buf1, 1, S1 - _buf1, stdout);
  return 0;
}