Problem p03991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define forE(i, x) for (int i = head[x]; i != -1; i = ne[i])
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned u32;
typedef pair<int, int> pin;
#define mk(a, b) make_pair(a, b)
#define lowbit(x) ((x) & (-(x)))
#define sqr(a) ((a) * (a))
#define clr(a) (memset((a), 0, sizeof(a)))
#define ls ((x) << 1)
#define rs (((x) << 1) | 1)
#define mid (((l) + (r)) >> 1)
#define pb push_back
#define w1 first
#define w2 second
inline void read(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
inline void judge() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
/*******************************head*******************************/
const int G = 5, mod = 924844033;
const int maxn = 500005;
int len, lim, wm[maxn], rev[maxn];
inline int powmod(int a, int b) {
  int res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = 1ll * res * a % mod;
    a = 1ll * a * a % mod;
  }
  return res;
}
inline void init(int n, int m) {
  len = 1;
  lim = 0;
  while (len <= n + m)
    len <<= 1, lim++;
  int w = powmod(G, (mod - 1) / len);
  wm[0] = 1;
  rep(i, 1, len) wm[i] = 1ll * wm[i - 1] * w % mod;
  rep(i, 0, len - 1) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
}
int n;
inline void NTT(int a[], int on) {
  rep(i, 0, len - 1) if (i < rev[i]) swap(a[i], a[rev[i]]);
  for (int h = 2; h <= len; h <<= 1) {
    int wn = wm[len / h];
    for (int k = 0; k < len; k += h) {
      int w = 1;
      rep(j, 0, h / 2 - 1) {
        int u = a[j + k], t = 1ll * a[j + k + h / 2] * w % mod;
        a[j + k] = (u + t) % mod;
        a[j + k + h / 2] = (u - t + mod) % mod;
        w = 1ll * w * wn % mod;
      }
    }
  }
  if (on == -1) {
    int inv = powmod(len, mod - 2);
    rep(i, 0, len - 1) a[i] = 1ll * a[i] * inv % mod;
  }
}
namespace tree {
int num, size[maxn], head[maxn], t[maxn << 1], ne[maxn << 1];
inline void addedge(int x, int y) {
  ne[++num] = head[x];
  head[x] = num;
  t[num] = y;
  ne[++num] = head[y];
  head[y] = num;
  t[num] = x;
}
inline void dfs(int x, int f) {
  size[x] = 1;
  forE(i, x) if (t[i] != f) {
    dfs(t[i], x);
    size[x] += size[t[i]];
  }
}
inline void solve() {
  rep(i, 1, n) head[i] = -1;
  rep(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    addedge(x, y);
  }
  dfs(1, -1);
}
} // namespace tree
int A[maxn], B[maxn], fac[maxn], facinv[maxn];
inline void prprpr() {
  fac[0] = 1;
  rep(i, 1, 400000) fac[i] = 1ll * fac[i - 1] * i % mod;
  facinv[400000] = powmod(fac[400000], mod - 2);
  per(i, 399999, 0) facinv[i] = 1ll * facinv[i + 1] * (i + 1) % mod;
}
inline int C(int n, int m) {
  if (n < 0 || m < 0 || m > n)
    return 0;
  return 1ll * fac[n] * facinv[m] % mod * facinv[n - m] % mod;
}
int main() {
  prprpr();
  read(n);
  tree::solve();
  rep(i, 2, n) A[tree::size[i]]++, A[n - tree::size[i]]++;
  rep(i, 0, n) A[i] = 1ll * A[i] * fac[i] % mod;
  rep(i, 0, n) B[i] = facinv[n - i];
  init(n, n);
  NTT(A, 1);
  NTT(B, 1);
  reverse(wm + 1, wm + len);
  rep(i, 0, len - 1) A[i] = 1ll * A[i] * B[i] % mod;
  NTT(A, -1);
  rep(K, 1, n) {
    int nowans = 1ll * (n - 1) * C(n, K) % mod;
    nowans = (nowans - 1ll * A[K + n] * facinv[K] % mod + mod) % mod;
    nowans = (nowans + C(n, K)) % mod;
    printf("%d\n", nowans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define forE(i, x) for (int i = head[x]; i != -1; i = ne[i])
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned u32;
typedef pair<int, int> pin;
#define mk(a, b) make_pair(a, b)
#define lowbit(x) ((x) & (-(x)))
#define sqr(a) ((a) * (a))
#define clr(a) (memset((a), 0, sizeof(a)))
#define ls ((x) << 1)
#define rs (((x) << 1) | 1)
#define mid (((l) + (r)) >> 1)
#define pb push_back
#define w1 first
#define w2 second
inline void read(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
inline void judge() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
/*******************************head*******************************/
const int G = 5, mod = 924844033;
const int maxn = 530005;
int len, lim, wm[maxn], rev[maxn];
inline int powmod(int a, int b) {
  int res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = 1ll * res * a % mod;
    a = 1ll * a * a % mod;
  }
  return res;
}
inline void init(int n, int m) {
  len = 1;
  lim = 0;
  while (len <= n + m)
    len <<= 1, lim++;
  int w = powmod(G, (mod - 1) / len);
  wm[0] = 1;
  rep(i, 1, len) wm[i] = 1ll * wm[i - 1] * w % mod;
  rep(i, 0, len - 1) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
}
int n;
inline void NTT(int a[], int on) {
  rep(i, 0, len - 1) if (i < rev[i]) swap(a[i], a[rev[i]]);
  for (int h = 2; h <= len; h <<= 1) {
    int wn = wm[len / h];
    for (int k = 0; k < len; k += h) {
      int w = 1;
      rep(j, 0, h / 2 - 1) {
        int u = a[j + k], t = 1ll * a[j + k + h / 2] * w % mod;
        a[j + k] = (u + t) % mod;
        a[j + k + h / 2] = (u - t + mod) % mod;
        w = 1ll * w * wn % mod;
      }
    }
  }
  if (on == -1) {
    int inv = powmod(len, mod - 2);
    rep(i, 0, len - 1) a[i] = 1ll * a[i] * inv % mod;
  }
}
namespace tree {
int num, size[maxn], head[maxn], t[maxn << 1], ne[maxn << 1];
inline void addedge(int x, int y) {
  ne[++num] = head[x];
  head[x] = num;
  t[num] = y;
  ne[++num] = head[y];
  head[y] = num;
  t[num] = x;
}
inline void dfs(int x, int f) {
  size[x] = 1;
  forE(i, x) if (t[i] != f) {
    dfs(t[i], x);
    size[x] += size[t[i]];
  }
}
inline void solve() {
  rep(i, 1, n) head[i] = -1;
  rep(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    addedge(x, y);
  }
  dfs(1, -1);
}
} // namespace tree
int A[maxn], B[maxn], fac[maxn], facinv[maxn];
inline void prprpr() {
  fac[0] = 1;
  rep(i, 1, 400000) fac[i] = 1ll * fac[i - 1] * i % mod;
  facinv[400000] = powmod(fac[400000], mod - 2);
  per(i, 399999, 0) facinv[i] = 1ll * facinv[i + 1] * (i + 1) % mod;
}
inline int C(int n, int m) {
  if (n < 0 || m < 0 || m > n)
    return 0;
  return 1ll * fac[n] * facinv[m] % mod * facinv[n - m] % mod;
}
int main() {
  prprpr();
  read(n);
  tree::solve();
  rep(i, 2, n) A[tree::size[i]]++, A[n - tree::size[i]]++;
  rep(i, 0, n) A[i] = 1ll * A[i] * fac[i] % mod;
  rep(i, 0, n) B[i] = facinv[n - i];
  init(n, n);
  NTT(A, 1);
  NTT(B, 1);
  reverse(wm + 1, wm + len);
  rep(i, 0, len - 1) A[i] = 1ll * A[i] * B[i] % mod;
  NTT(A, -1);
  rep(K, 1, n) {
    int nowans = 1ll * (n - 1) * C(n, K) % mod;
    nowans = (nowans - 1ll * A[K + n] * facinv[K] % mod + mod) % mod;
    nowans = (nowans + C(n, K)) % mod;
    printf("%d\n", nowans);
  }
  return 0;
}