Problem p03991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, a) for (int i = start[(a)]; i; i = e[i].next)
template <class T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
inline int read() {
  int sum = 0, p = 1;
  char ch = getchar();
  while (!(('0' <= ch && ch <= '9') || ch == '-'))
    ch = getchar();
  if (ch == '-')
    p = -1, ch = getchar();
  while ('0' <= ch && ch <= '9')
    sum = sum * 10 + ch - 48, ch = getchar();
  return sum * p;
}

const int maxn = 5.3e5 + 20;
const int mod = 924844033;

struct node {
  int v, next;
};
node e[maxn << 1];
int cnt, start[maxn];

inline void addedge(int u, int v) {
  e[++cnt] = (node){v, start[u]};
  start[u] = cnt;
}
int n, sz[maxn], num[maxn], jc[maxn], jcn[maxn], inv[maxn];

inline void pls(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

inline int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ll)ans * a % mod;
    b >>= 1;
    a = (ll)a * a % mod;
  }
  return ans;
}

void dfs(int u, int fa) {
  sz[u] = 1;
  EREP(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    dfs(v, u);
    sz[u] += sz[v];
    pls(num[sz[v]], mod - 1);
  }
  pls(num[n - sz[u]], mod - 1);
}

inline void init() {
  n = read();
  jc[0] = jc[1] = jcn[0] = jcn[1] = inv[1] = 1;
  REP(i, 2, n)
  jc[i] = (ll)i * jc[i - 1] % mod,
  inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod,
  jcn[i] = (ll)inv[i] * jcn[i - 1] % mod;
  REP(i, 1, n - 1) {
    int u = read(), v = read();
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 0);
  num[n] = n;
}

int st[maxn], rev[maxn];

inline void FFT(int *p, int n, int op) {
  int l = 0;
  while ((1 << l) < n)
    l++;
  REP(i, 1, n - 1) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << l - 1);
  REP(i, 1, n - 1) if (i < rev[i]) swap(p[i], p[rev[i]]);
  for (int i = 1; i < n; i <<= 1) {
    int w = power(5, (mod - 1) / (i << 1));
    st[0] = 1;
    REP(j, 1, i - 1) st[j] = (ll)st[j - 1] * w % mod;
    for (int j = 0; j < n; j += i << 1)
      for (int k = 0; k < i; k++) {
        int x = p[j + k], y = (ll)st[k] * p[i + j + k] % mod;
        p[j + k] = x + y;
        if (p[j + k] >= mod)
          p[j + k] -= mod;
        p[i + j + k] = x - y;
        if (p[i + j + k] < 0)
          p[i + j + k] += mod;
      }
  }
  if (op == -1) {
    int inv = power(n, mod - 2);
    REP(i, 0, n - 1) p[i] = (ll)inv * p[i] % mod;
    reverse(p + 1, p + n);
  }
}

int f[maxn], g[maxn];

inline void doing() {
  int N = 1;
  while (N <= 2 * n)
    N <<= 1;
  REP(i, 0, n) f[i] = (ll)num[i] * jc[i] % mod, g[i] = jcn[i];
  f[0] = 0;
  reverse(g, g + n + 1);
  FFT(f, N, 1);
  FFT(g, N, 1);
  REP(i, 0, N - 1) f[i] = (ll)f[i] * g[i] % mod;
  FFT(f, N, -1);
  REP(i, 1, n) printf("%lld\n", (ll)f[i + n] * jcn[i] % mod);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("easy.in", "r", stdin);
  freopen("easy.out", "w", stdout);
#endif
  init();
  doing();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, a) for (int i = start[(a)]; i; i = e[i].next)
template <class T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
inline int read() {
  int sum = 0, p = 1;
  char ch = getchar();
  while (!(('0' <= ch && ch <= '9') || ch == '-'))
    ch = getchar();
  if (ch == '-')
    p = -1, ch = getchar();
  while ('0' <= ch && ch <= '9')
    sum = sum * 10 + ch - 48, ch = getchar();
  return sum * p;
}

const int maxn = 5.3e5 + 20;
const int mod = 924844033;

struct node {
  int v, next;
};
node e[maxn << 1];
int cnt, start[maxn];

inline void addedge(int u, int v) {
  e[++cnt] = (node){v, start[u]};
  start[u] = cnt;
}
int n, sz[maxn], num[maxn], jc[maxn], jcn[maxn], inv[maxn];

inline void pls(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

inline int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ll)ans * a % mod;
    b >>= 1;
    a = (ll)a * a % mod;
  }
  return ans;
}

void dfs(int u, int fa) {
  sz[u] = 1;
  EREP(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    dfs(v, u);
    sz[u] += sz[v];
    pls(num[sz[v]], mod - 1);
  }
  pls(num[n - sz[u]], mod - 1);
}

inline void init() {
  n = read();
  jc[0] = jc[1] = jcn[0] = jcn[1] = inv[1] = 1;
  REP(i, 2, n)
  jc[i] = (ll)i * jc[i - 1] % mod,
  inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod,
  jcn[i] = (ll)inv[i] * jcn[i - 1] % mod;
  REP(i, 1, n - 1) {
    int u = read(), v = read();
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 0);
  num[n] = n;
}

int st[maxn], rev[maxn];

inline void FFT(int *p, int n, int op) {
  int l = 0;
  while ((1 << l) < n)
    l++;
  REP(i, 1, n - 1) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << l - 1);
  REP(i, 1, n - 1) if (i < rev[i]) swap(p[i], p[rev[i]]);
  for (int i = 1; i < n; i <<= 1) {
    int w = power(5, (mod - 1) / (i << 1));
    st[0] = 1;
    REP(j, 1, i - 1) st[j] = (ll)st[j - 1] * w % mod;
    for (int j = 0; j < n; j += i << 1)
      for (int k = 0; k < i; k++) {
        int x = p[j + k], y = (ll)st[k] * p[i + j + k] % mod;
        p[j + k] = x + y;
        if (p[j + k] >= mod)
          p[j + k] -= mod;
        p[i + j + k] = x - y;
        if (p[i + j + k] < 0)
          p[i + j + k] += mod;
      }
  }
  if (op == -1) {
    int inv = power(n, mod - 2);
    REP(i, 0, n - 1) p[i] = (ll)inv * p[i] % mod;
    reverse(p + 1, p + n);
  }
}

int f[maxn], g[maxn];

inline void doing() {
  int N = 1;
  while (N <= 2 * n)
    N <<= 1;
  REP(i, 0, n) f[i] = (ll)num[i] * jc[i] % mod, g[i] = jcn[i];
  f[0] = 0;
  reverse(g, g + n + 1);
  FFT(f, N, 1);
  FFT(g, N, 1);
  REP(i, 0, N - 1) f[i] = (ll)f[i] * g[i] % mod;
  FFT(f, N, -1);
  REP(i, 1, n) printf("%lld\n", (ll)f[i + n] * jcn[i] % mod);
}

int main() {
  init();
  doing();
  return 0;
}