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

#include <bits/stdc++.h>
using namespace std;
template <typename T> inline void readin(T &x) {
  x = 0;
  T fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= fh;
}
template <typename T> inline void wt(T x) {
  if (x > 9)
    wt(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void writeln(T x, char c) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  wt(x);
  putchar(c);
}
int f[10000005];
inline void dfs(int x, int dep, int tot) {
  f[x] = tot;
  if (dep >= 18)
    return;
  dfs(x << 1, dep + 1, tot);
  dfs(x << 1 | 1, dep + 1, tot + 1);
}
char s[1000005];
int n, m[3], cnt;
inline int qp(int x, int y, int mod) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (1ll * res * x) % mod;
    x = (1ll * x * x) % mod;
    y >>= 1;
  }
  return res;
}
inline int work(int x) {
  int sum = 0;
  if (s[x] == '1') {
    sum = m[1];
    sum -= qp(2, n - x, cnt - 1);
    sum %= (cnt - 1);
    if (sum < 0)
      sum += cnt - 1;
  } else {
    sum = m[2];
    sum += qp(2, n - x, cnt + 1);
    sum %= (cnt + 1);
  }
  return sum;
}
int main() {
  dfs(0, 0, 0);
  readin(n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (cnt > 1) {
        m[1] = (m[1] + qp(2, n - i, cnt - 1)) % (cnt - 1);
      }
      m[2] = (m[2] + qp(2, n - i, cnt + 1)) % (cnt + 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1' && cnt == 1) {
      puts("0");
      continue;
    }
    int k = work(i);
    int tim = 1;
    while (k) {
      k %= f[k];
      tim++;
    }
    writeln(tim, '\\n');
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T> inline void readin(T &x) {
  x = 0;
  T fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= fh;
}
template <typename T> inline void wt(T x) {
  if (x > 9)
    wt(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void writeln(T x, char c) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  wt(x);
  putchar(c);
}
int f[10000005];
inline void dfs(int x, int dep, int tot) {
  f[x] = tot;
  if (dep >= 18)
    return;
  dfs(x << 1, dep + 1, tot);
  dfs(x << 1 | 1, dep + 1, tot + 1);
}
char s[1000005];
int n, m[3], cnt;
inline int qp(int x, int y, int mod) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (1ll * res * x) % mod;
    x = (1ll * x * x) % mod;
    y >>= 1;
  }
  return res;
}
inline int work(int x) {
  int sum = 0;
  if (s[x] == '1') {
    sum = m[1];
    sum -= qp(2, n - x, cnt - 1);
    sum %= (cnt - 1);
    if (sum < 0)
      sum += cnt - 1;
  } else {
    sum = m[2];
    sum += qp(2, n - x, cnt + 1);
    sum %= (cnt + 1);
  }
  return sum;
}
int main() {
  dfs(0, 0, 0);
  readin(n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      if (cnt > 1)
        m[1] += qp(2, n - i, cnt - 1);
      m[2] += qp(2, n - i, cnt + 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1' && cnt == 1) {
      puts("0");
      continue;
    }
    int k = work(i);
    int tim = 1;
    while (k) {
      k %= f[k];
      tim++;
    }
    writeln(tim, '\\n');
  }
  return 0;
}