【AGC005F】Many Easy Problems

题面

洛谷

题解

这当前处理的点集大小为\(k\),那么考虑将每个点的贡献拆开来算,那么如果这\(K\)个点都在以\(x\)为根的一棵子树内,这个点就没有贡献

\(size_x\)表示\(x\)子树的大小,有

\[ f(k)={N \choose k} - \sum_{x=1}^N\sum_{(x,v)}{ size_v \choose K} \]

\(cnt_i\)表示无根树大小为\(i\)的子树个数,那么有,

\[ f(k)=\sum_{i=k}^N cnt_i\times \frac{i!}{k!(i-k)!} \]

其中\(\frac {1}{k!}\)可以最后再算。

\(g(i)=cnt_i\times i!\)\(h(i)=\frac {1}{i!}\),注意这个式子两字母之差相等,考虑将\(g\)翻转,

\[ \begin{aligned} f(k)&=\sum_{i=k}^{N}g(i)h(i-k)\\ &=\sum_{i=0}^{N-k}g(i+k)h(i)\\ &=\sum_{i=0}^{N-k}g_r(N-i-k)h_i \end{aligned} \]

然后\(\mathcal{NTT}\)就可以了。

代码

#include  
#include  
#include  
#include  
#include  
#include  
using namespace std; 
inline int gi() {
    register int data = 0, w = 1;
    register char ch = 0;
    while (!isdigit(ch) && ch != '-') ch = getchar(); 
    if (ch == '-') w = -1, ch = getchar(); 
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); 
    return w * data; 
} 
const int Mod = 924844033, G = 5; 
int fpow(int x, int y) { 
    int res = 1; 
    while (y) { 
        if (y & 1) res = 1ll * res * x % Mod; 
        x = 1ll * x * x % Mod; 
        y >>= 1; 
    } 
    return res; 
} 
const int iG = fpow(G, Mod - 2); 
const int MAX_N = 2e5 + 5; 
int Limit, rev[MAX_N << 2]; 
void FFT_prepare(int len) { 
    int p = 0; 
    for (Limit = 1; Limit <= len; Limit <<= 1) ++p; 
    for (int i = 1; i < Limit; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (p - 1)); 
} 
void NTT(int *p, int op) { 
    for (int i = 1; i < Limit; i++) if (i < rev[i]) swap(p[i], p[rev[i]]); 
    for (int i = 1; i < Limit; i <<= 1) { 
        int rot = fpow(op == 1 ? G : iG, (Mod - 1) / (i << 1)); 
        for (int j = 0; j < Limit; j += i << 1) 
            for (int k = 0, w = 1; k < i; k++, w = 1ll * w * rot % Mod) { 
                int x = p[j + k], y = 1ll * w * p[i + j + k] % Mod; 
                p[j + k] = (x + y) % Mod, p[i + j + k] = (x - y + Mod) % Mod; 
            } 
    } 
    if (!op) { 
        int inv = fpow(Limit, Mod - 2); 
        for (int i = 0; i < Limit; i++) p[i] = 1ll * p[i] * inv % Mod; 
    } 
} 
struct Graph { int to, next; } e[MAX_N << 1]; 
int fir[MAX_N], e_cnt; 
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; } 
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; } 
int N, f[MAX_N << 2], g[MAX_N << 2], h[MAX_N << 2];
int siz[MAX_N], cnt[MAX_N]; 
void dfs(int x, int fa) { 
    siz[x] = 1; 
    for (int i = fir[x]; ~i; i = e[i].next) {
        int v = e[i].to; if (v == fa) continue; 
        dfs(v, x), siz[x] += siz[v]; 
        cnt[siz[v]]++; 
    } 
    cnt[N - siz[x]]++; 
} 
int fac[MAX_N], ifc[MAX_N]; 
int C(int n, int m) { 
    if (n < 0 || m < 0 || n < m) return 0; 
    else return 1ll * fac[n] * ifc[n - m] % Mod * ifc[m] % Mod; 
} 
int main () { 
    clearGraph(); 
    N = gi(); 
    for (int i = 1; i < N; i++) { 
        int u = gi(), v = gi(); 
        Add_Edge(u, v), Add_Edge(v, u); 
    } 
    fac[0] = 1; for (int i = 1; i <= N; i++) fac[i] = 1ll * fac[i - 1] * i % Mod; 
    ifc[N] = fpow(fac[N], Mod - 2); 
    for (int i = N - 1; ~i; i--) ifc[i] = 1ll * ifc[i + 1] * (i + 1) % Mod;
    dfs(1, 0); 
    for (int i = 1; i <= N; i++) g[i] = 1ll * cnt[i] * fac[i] % Mod; 
    for (int i = 0; i <= N; i++) h[i] = ifc[i]; 
    reverse(&g[0], &g[N + 1]); 
    FFT_prepare(N << 1); 
    NTT(g, 1), NTT(h, 1); 
    for (int i = 0; i < Limit; i++) f[i] = 1ll * g[i] * h[i] % Mod; 
    NTT(f, 0); 
    for (int i = 1; i <= N; i++) { 
        int ans = 1ll * N * C(N, i) % Mod; 
        ans = (ans - 1ll * ifc[i] * f[N - i] % Mod + Mod) % Mod; 
        printf("%d\n", ans); 
    } 
    return 0; 
} 

你可能感兴趣的:(【AGC005F】Many Easy Problems)