1. 程式人生 > >「AGC005F」Many Easy Problems-NTT

「AGC005F」Many Easy Problems-NTT

Description

連結

Solution

考慮每個點 u u 的貢獻對 f ( k ) f(k)

。即 C n k v
s o n    o f    u
C s i z v k C_{n}^k-\sum_{v \in son \ \ of \ \ u} C_{siz_v}^k
。意思是在它的子樹中選取 k k 個的總方案數減去只在它子樹中選 k k 的方案數。

那麼答案的貢獻就是 n C n k i = 0 n c n t i C i k nC_{n}^k-\sum_{i=0}^ncnt_iC_{i}^k ,其中 c n t i cnt_i 表示大小為 i i 的子樹的數量,dfs求即可。可以發現後面的部分是一個卷積的形式,NTT即可。

#include <bits/stdc++.h>
using namespace std;

typedef long long lint;
const int mod = 924844033, Phi = mod - 1, G = 5;
const int maxn = 1000005;

int n;
int fac[maxn], ifac[maxn];
int a[maxn], b[maxn], c[maxn];

struct edge {
    int to, next;
} e[maxn * 2];
int h[maxn], tot;

int cnt[maxn], siz[maxn];

inline int gi()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int sum = 0;
    while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
    return sum;
}

inline int Pow(int x, int k)
{
    int res = 1;
    while (k) {
        if (k & 1) res = (lint)res * x % mod;
        x = (lint)x * x % mod; k >>= 1;
    }
    return res;
}

inline void add(int u, int v)
{
    e[++tot] = (edge) {v, h[u]}; h[u] = tot;
    e[++tot] = (edge) {u, h[v]}; h[v] = tot;
}

void dfs(int u, int fa)
{
    siz[u] = 1;
    for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
        if (v != fa) {
            dfs(v, u);
            siz[u] += siz[v];
            ++cnt[siz[v]]; ++cnt[n - siz[v]];
        }
}

namespace NTT
{

    int n, L, R[maxn], A[maxn], B[maxn];

    void NTT(int *a, int f)
    {
        for (int i = 0; i < n; ++i)
            if (i < R[i]) swap(a[i], a[R[i]]);
        for (int i = 1; i < n; i <<= 1) {
            int wn = Pow(G, Phi / (i << 1)), t;
            if (f == -1) wn = Pow(wn, mod - 2);
            for (int j = 0; j < n; j += (i << 1)) {
                int w = 1;
                for (int k = 0; k < i; ++k, w = (lint)w * wn % mod) {
                    t = (lint)a[j + i + k] * w % mod;
                    a[j + i + k] = a[j + k] - t;
                    if (a[j + i + k] < 0) a[j + i + k] += mod;
                    a[j + k] = a[j + k] + t;
                    if (a[j + k] >= mod) a[j + k] -= mod;
                }
            }
        }
    }
    
    void mul(int *a, int *b, int len1, int len2, int *c)
    {
        for (n = 1, len1 += len2 - 1; n < len1; n <<= 1) ++L;
        --L;
        for (int i = 0; i < n; ++i) R[i] = (R[i >> 1] >> 1) | ((i & 1) << L);

        for (int i = 0; i < n; ++i) A[i] = a[i], B[i] = b[i];
        NTT(A, 1); NTT(B, 1);
        for (int i = 0; i < n; ++i) A[i] = (lint)A[i] * B[i] % mod;
        NTT(A, -1);

        int inv = Pow(n, mod - 2);
        for (int i = 0; i < n; ++i) c[i] = (lint)A[i] * inv % mod;
    }

}
    
int main()
{
    n = gi();
    for (int i = 1; i < n; ++i) add(gi(), gi());

    dfs(1, 0);

    fac[0] = ifac[0] = ifac[1] = 1;
    for (int i = 1; i <= n; ++i) fac[i] = (lint)fac[i - 1] * i % mod;
    for (int i = 2; i <= n; ++i) ifac[i] = (lint)(mod - mod / i) * ifac[mod % i] % mod;
    for (int i = 1; i <= n; ++i) ifac[i] = (lint)ifac[i - 1] * ifac[i] % mod;

    for (int i = 1; i <= n; ++i) a[i] = (lint)cnt[i] * fac[i] % mod;
    for (int i = 1; i <= n; ++i) b[i] = ifac[n - i];

    NTT::mul(a, b, n + 1, n + 1, c);

    for (int i = 1; i <= n; ++i)
        printf("%lld\n", ((lint)n * fac[n] % mod * ifac[i] % mod * ifac[n - i] % mod - (lint)ifac[i] * c[n + i] % mod + mod) % mod);
    
    return 0;
}