洛谷P4233 射命丸文的笔记 【多项式求逆】
题目链接
题解
我们只需求出总的哈密顿回路个数和总的强联通竞赛图个数
对于每条哈密顿回路,我们统计其贡献
一条哈密顿回路就是一个圆排列,有\(\frac{n!}{n}\)种,剩余边随便连
所以总的贡献为
\[(n - 1)!2^{{n \choose 2} - n}
\]
我们只需求出总的强联通竞赛图的个数
设\(g[n]\)表示\(n\)个点竞赛图个数,\(f[n]\)表示强联通竞赛图个数
那么有
\[g[n] = \sum\limits_{i = 1}^{n}{n \choose i}f[i]g[n - i]
\]
即
\[\frac{g[n]}{n!} = \sum\limits_{i = 1}^{n}\frac{f[i]}{i!}\frac{g[n - i]}{(n - i)!}
\]
设\(G(x)\)和\(F(x)\)分别为其指数型生成函数
那么有
\[G(x) = F(x)G(x) + 1
\]
即
\[F(x) = \frac{G(x) - 1}{G(x)}
\]
多项式求逆即可
复杂度\(O(nlogn)\)
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 0x3f3f3f3f;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
return flag ? out : -out;
}
const int P = 998244353,G = 3;
inline int qpow(int a,LL b){
int re = 1;
for (; b; b >>= 1,a = 1ll * a * a % P)
if (b & 1) re = 1ll * re * a % P;
return re;
}
inline LL C(int n){return 1ll * n * (n - 1) / 2;}
int R[maxn];
void NTT(int* a,int n,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 gn = qpow(G,(P - 1) / (i << 1));
for (int j = 0; j < n; j += (i << 1)){
int g = 1,x,y;
for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
x = a[j + k],y = 1ll * g * a[j + k + i] % P;
a[j + k] = (x + y) % P,a[j + k + i] = ((x - y) % P + P) % P;
}
}
}
if (f == 1) return;
int nv = qpow(n,P - 2); reverse(a + 1,a + n);
for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
int A[maxn],B[maxn],c[maxn],ans,N,fac[maxn],inv[maxn],fv[maxn];
void init(){
fac[0] = fac[1] = inv[0] = inv[1] = fv[0] = fv[1] = 1;
for (int i = 2; i <= N; i++){
fac[i] = 1ll * fac[i - 1] * i % P;
inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
fv[i] = 1ll * fv[i - 1] * inv[i] % P;
}
}
void Inv(int deg,int* a,int* b){
if (deg == 1){b[0] = qpow(a[0],P - 2); return;}
Inv((deg + 1) >> 1,a,b);
int L = 0,n = 1;
while (n < (deg << 1)) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
for (int i = 0; i < deg; i++) c[i] = a[i];
for (int i = deg; i < n; i++) c[i] = 0;
NTT(c,n,1); NTT(b,n,1);
for (int i = 0; i < n; i++)
b[i] = 1ll * ((2ll - 1ll * c[i] * b[i] % P) + P) % P * b[i] % P;
NTT(b,n,-1);
for (int i = deg; i < n; i++) b[i] = 0;
}
int main(){
N = read(); init();
for (int i = 0; i <= N; i++) A[i] = 1ll * qpow(2,C(i)) * fv[i] % P;
Inv(N + 1,A,B);
A[0] = 0;
int n = 1,L = 0;
while (n <= (N << 1)) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
NTT(A,n,1); NTT(B,n,1);
for (int i = 0; i < n; i++) A[i] = 1ll * A[i] * B[i] % P;
NTT(A,n,-1);
REP(i,N){
if (i == 1) puts("1");
else if (i == 2) puts("-1");
else printf("%lld\n",1ll * fac[i - 1] * qpow(2,C(i) - i) % P * qpow(1ll * A[i] * fac[i] % P,P - 2) % P);
}
return 0;
}