【洛谷 P5825】 排列计数(二项式反演 / 多项式 / 生成函数)

传送门

题解区的Eulerian numberEulerian\ number什么的完全看不懂啊

显然能得到一个n2,dpn^2,dp
f[i][j]f[i][j]表示前ii个有jj个的方案
f[i][j]=f[i1][j](j+1)+f[i1][j1](ij)f[i][j]=f[i-1][j]*(j+1)+f[i-1][j-1](i-j)

然后好像具体数学里面有,这个叫Eulerian numberEulerian\ number,
f[i][j]=k=0j(i+1k)(1)k(j+1k)if[i][j]=\sum_{k=0}^j{i+1\choose k}(-1)^k(j+1-k)^i
然后好像可以证明这个玩意儿是符合那个dpdp的式子的
然后卷积即可
当然我看不懂关我啥事


考虑容斥,记g[i]g[i]表示至少ii<<的方案
考虑连续一段<<一定是有序的

所以整个序列可以看做若干组有序的拼接在一起
于是构建EGFEGFg[i]=n![xn](ex1)nig[i]=n![x^n](e^x-1)^{n-i}
拆系数后发现
[xn](ex1)ni=(ni)!n!j=0nijnj!(1)nij(nij)![x^n](e^x-1)^{n-i}=\frac{(n-i)!}{n!}\sum_{j=0}^{n-i}\frac{j^n}{j!}\frac{(-1)^{n-i-j}}{(n-i-j)!}

于是可以nttntt搞出gg

再考虑二项式反演
ff为答案
g[i]=j=in(ji)f[i]g[i]=\sum_{j=i}^n{j\choose i}f[i]
f[i]=j=in(ji)(1)jig[j]f[i]=\sum_{j=i}^n{j\choose i}(-1)^{j-i}g[j]
然后这个玩意拆开有有
f[i]=1i!j=0ni(1)jj!gi+j(i+j)f[i]=\frac{1}{i!}\sum_{j=0}^{n-i}\frac{(-1)^j}{j!}\frac{g_{i+j}}{(i+j)!}
然后翻转一下卷积即可

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
typedef vector<int> poly;
namespace Poly{
	cs int C=19,M=(1<<C)|1,G=3;
	int *w[C+1],rev[M];
	inline void init_rev(int lim){
		for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
	}
	inline void init_w(){
		for(int i=1;i<=C;i++)w[i]=new int[(1<<(i-1))+1];
		int wn=ksm(G,(mod-1)/(1<<C));w[C][0]=1;
		for(int i=1,l=1<<(C-1);i<l;i++)w[C][i]=mul(w[C][i-1],wn);
		for(int j=C-1;j;j--)
		for(int i=0;i<(1<<(j-1));i++)w[j][i]=w[j+1][i<<1];
	}
	inline void ntt(int *f,int lim,int kd){
		for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
		for(int mid=1,l=1,a0,a1;mid<lim;mid<<=1,l++)
		for(int i=0;i<lim;i+=mid<<1)
		for(int j=0;j<mid;j++)
		a0=f[i+j],a1=mul(f[i+j+mid],w[l][j]),f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
		if(kd==-1){
			reverse(f+1,f+lim);
			for(int i=0,iv=Inv(lim);i<lim;i++)Mul(f[i],iv);
		}
	}
	inline poly operator +(poly a,cs poly &b){
		a.resize(max(a.size(),b.size()));
		for(int i=0;i<b.size();i++)Add(a[i],b[i]);
		return a;
	}
	inline poly operator *(poly a,poly b){
		int deg=a.size()+b.size()-1;
		if(deg<=32){
			poly c(deg,0);
			for(int i=0;i<a.size();i++)
			for(int j=0;j<b.size();j++)
			Add(c[i+j],mul(a[i],b[j]));
			return c;
		}
		int lim=1;
		while(lim<deg)lim<<=1;
		init_rev(lim);
		a.resize(lim),ntt(&a[0],lim,1);
		b.resize(lim),ntt(&b[0],lim,1);
		for(int i=0;i<lim;i++)Mul(a[i],b[i]);
		ntt(&a[0],lim,-1),a.resize(deg);
		return a;
	}
	inline poly mul(poly a,poly b){
		return a*b;
	}
}
using Poly::mul;
cs int N=200005;
int fac[N],ifac[N];
inline void init_inv(cs int len=N-5){
	fac[0]=ifac[0]=1;
	for(int i=1;i<=len;i++)fac[i]=mul(i,fac[i-1]);
	ifac[len]=Inv(fac[len]);
	for(int i=len-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
inline int C(int n,int m){return n<m?0:mul(fac[n],mul(ifac[m],ifac[n-m]));}
int n,g[N];
poly a,b;
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read();init_inv();
	Poly::init_w();
	a.resize(n+1),b.resize(n+1);
	for(int i=0;i<=n;i++)a[i]=mul(ksm(i,n),ifac[i]),b[i]=(i&1)?mod-ifac[i]:ifac[i];
	a=mul(a,b);
	for(int i=0;i<=n;i++)g[i]=mul(a[n-i],fac[n-i]);
	a.resize(n+1);
	for(int i=0;i<=n;i++)a[i]=mul(g[i],fac[i]);
	reverse(b.bg(),b.end());
	a=mul(a,b);
	for(int i=0;i<=n;i++)cout<<mul(a[n+i],ifac[i])<<" ";
	return 0;
}
posted @ 2020-02-12 19:20  Stargazer_cykoi  阅读(235)  评论(0编辑  收藏  举报