码迷,mamicode.com
首页 > 其他好文 > 详细

[luogu P4705] 玩游戏

时间:2021-05-23 23:13:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:rev   erase   利用   return   bin   char   insert   --   ++   

\(\text{Problem}:\)玩游戏

\(\text{Solution}:\)

要对 \(\forall k\in[1,t]\),求出:

\[f_{k}=\frac{1}{nm}\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}(a_{i}+b_{j})^{k} \]

\((a_{i}+b_{j})^{k}\) 用二项式定理展开,有:

\[\begin{aligned} f_{k}&=\frac{1}{nm}\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\sum\limits_{x=0}^{k}\binom{k}{x}a_{i}^{x}b_{j}^{k-x}\&=\frac{k!}{nm}\sum\limits_{x=0}^{k}\sum\limits_{i=1}^{n}\frac{a_{i}^{x}}{x!}\sum\limits_{j=1}^{m}\frac{b_{i}^{k-x}}{(k-x)!}\&=\frac{k!}{nm}\sum\limits_{x=0}^{k}A_{x}B_{k-x} \end{aligned} \]

\(F(x)\)\(A_{i}\times i!\) 的生成函数,\(G(x)\)\(B_{i}\times i!\) 的生成函数,有:

\[F(x)=\sum\limits_{i=0}^{\infty}x^{i}\sum\limits_{j=1}^{n}a_{j}^{i}=\sum\limits_{j=1}^{n}\sum\limits_{i=0}^{\infty}a_{j}^{i}x^{i}=\sum\limits_{j=1}^{n}\frac{1}{1-a_{j}x}\G(x)=\sum\limits_{i=0}^{\infty}x^{i}\sum\limits_{j=1}^{m}b_{j}^{i}=\sum\limits_{j=1}^{m}\sum\limits_{i=0}^{\infty}b_{j}^{i}x^{i}=\sum\limits_{j=1}^{m}\frac{1}{1-b_{j}x} \]

发现是一个加和的形式,难以处理。考虑用 \(\ln\) 去转化。易知 \((\ln(1-ax))‘=\frac{-a}{1-ax}\)。设 \(F_{1}(x)=\sum\limits_{i=1}^{n}\frac{-a_{i}}{1-a_{i}x}\)\(G_{1}(x)=\sum\limits_{i=1}^{m}\frac{-b_{i}}{1-b_{i}x}\),有:

\[1-x\frac{-a}{1-ax}=\frac{1}{1-ax}\F(x)=-xF_{1}(x)+n\G(x)=-xG_{1}(x)+m \]

现在问题转化为求出 \(F_{1}(x),G_{1}(x)\),有:

\[\begin{aligned} F_{1}(x)&=\sum\limits_{i=1}^{n}\frac{-a_{i}}{1-a_{i}x}\&=\sum\limits_{i=1}^{n}(\ln(1-a_{i}x))‘\&=\left(\sum\limits_{i=1}^{n}\ln(1-a_{i}x)\right)‘\&=\left(\ln\left(\prod\limits_{i=1}^{n}(1-a_{i}x)\right)\right)‘ \end{aligned} \]

利用分治乘法即可在 \(O(n\log^2 n)\) 的时间复杂度内求出 \(F_{1}(x)\),对于 \(G_{1}(x)\) 也是类似。带回到原来的式子即可求出 \(A_{i},B_{i}\)。总时间复杂度 \(O(n\log^2 n)\)

\(\text{Code}:\)

#include <bits/stdc++.h>
#pragma GCC optimize(3)
//#define int long long
#define ri register
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define is insert
#define es erase
#define vi vector<int>
#define vpi vector<pair<int,int>>
using namespace std; const int N=265010, Mod=998244353;
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘) w=-1; ch=getchar(); }
	while(ch>=‘0‘&&ch<=‘9‘) s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
	return s*w;
}
int n,m,K,fac[N+5],inv[N+5];
vector<int> a,b;
int rev[N],r[24][2];
inline int ksc(int x,int p) { int res=1; for(;p;p>>=1, x=1ll*x*x%Mod) if(p&1) res=1ll*res*x%Mod; return res; }
inline void Get_Rev(int T) { for(ri int i=0;i<T;i++) rev[i]=(rev[i>>1]>>1)|((i&1)?(T>>1):0); }
inline void DFT(int T,vector<int> &s,int type)
{
	for(ri int i=0;i<T;i++) if(rev[i]<i) swap(s[i],s[rev[i]]);
	for(ri int i=2,cnt=1;i<=T;i<<=1,cnt++)
	{
		int wn=r[cnt][type];
		for(ri int j=0,mid=(i>>1);j<T;j+=i)
		{
			for(ri int k=0,w=1;k<mid;k++,w=1ll*w*wn%Mod)
			{
				int x=s[j+k], y=1ll*w*s[j+mid+k]%Mod;
				s[j+k]=x+y;
				if(s[j+k]>=Mod) s[j+k]-=Mod;
				s[j+mid+k]=x-y;
				if(s[j+mid+k]<0) s[j+mid+k]+=Mod;
			}
		}
	}
	if(!type) for(ri int i=0,inv=ksc(T,Mod-2);i<T;i++) s[i]=1ll*s[i]*inv%Mod;
}
inline void NTT(int n,int m,vector<int> &A,vector<int> B)
{
	int len=n+m;
	int T=1;
	while(T<=len) T<<=1;
	Get_Rev(T);
	A.resize(T), B.resize(T);
	for(ri int i=n+1;i<T;i++) A[i]=0;
	for(ri int i=m+1;i<T;i++) B[i]=0;
	DFT(T,A,1), DFT(T,B,1);
	for(ri int i=0;i<T;i++) A[i]=1ll*A[i]*B[i]%Mod;
	DFT(T,A,0);
	A.erase(A.begin()+len+1,A.end());
}
void GetInv(int n,vector<int> &F,vector<int> G)
{
	if(n==1) { F[0]=ksc(G[0],Mod-2); return; }
	GetInv((n+1)/2,F,G);
	vector<int> A,B;
	int T=1;
	while(T<=n+n) T<<=1;
	Get_Rev(T);
	A.resize(T), B.resize(T);
	for(ri int i=0;i<n;i++) A[i]=F[i], B[i]=G[i];
	DFT(T,A,1), DFT(T,B,1);
	for(ri int i=0;i<T;i++) A[i]=(2ll*A[i]%Mod-1ll*B[i]*A[i]%Mod*A[i]%Mod+Mod)%Mod;
	DFT(T,A,0);
	for(ri int i=0;i<n;i++) F[i]=A[i];
}
inline void GetDao(int n,vector<int> &A,vector<int> B)
{
	for(ri int i=0;i<n-1;i++) A[i]=1ll*B[i+1]*(i+1)%Mod;
	A[n-1]=0;
}
inline void GetJi(int n,vector<int> &A,vector<int> B)
{
	for(ri int i=1;i<n;i++) A[i]=1ll*B[i-1]*inv[i]%Mod*fac[i-1]%Mod;
	A[0]=0;
}
inline void GetLn(int n,vector<int> &F,vector<int> G)
{
	vector<int> A,B;
	A.resize(n), B.resize(n);
	GetDao(n,A,G);
	GetInv(n,B,G);
	NTT(n,n,A,B);
	GetJi(n,F,A);
}
void Solve(int l,int r,vector<int> &F,vector<int> &G)
{
	if(l==r)
	{
		F.resize(2);
		F[0]=1, F[1]=Mod-G[l];
		return;
	}
	int mid=(l+r)/2;
	Solve(l,mid,F,G);
	vector<int> C;
	Solve(mid+1,r,C,G);
	NTT(mid-l+1,r-mid,F,C);
}
signed main()
{
	r[23][1]=ksc(3,119), r[23][0]=ksc(ksc(3,Mod-2),119);
	for(ri int i=22;~i;i--) r[i][0]=1ll*r[i+1][0]*r[i+1][0]%Mod, r[i][1]=1ll*r[i+1][1]*r[i+1][1]%Mod;
	fac[0]=1;
	for(ri int i=1;i<=N;i++) fac[i]=1ll*fac[i-1]*i%Mod;
	inv[N]=ksc(fac[N],Mod-2);
	for(ri int i=N;i;i--) inv[i-1]=1ll*inv[i]*i%Mod;
	n=read(), m=read();
	a.resize(n+1), b.resize(m+1);
	for(ri int i=1;i<=n;i++) a[i]=read();
	for(ri int i=1;i<=m;i++) b[i]=read();
	K=read(), K++;
	vector<int> A,B;
	A.resize(n+1), B.resize(m+1);
	Solve(1,n,A,a), Solve(1,m,B,b);
	if((int)A.size()>=K) A.erase(A.begin()+K,A.end());
	if((int)B.size()>=K) B.erase(B.begin()+K,B.end());
	A.resize(K), B.resize(K);
	GetLn(K,A,A), GetLn(K,B,B);
	GetDao(K,A,A), GetDao(K,B,B);
	for(ri int i=K-1;i;i--) A[i]=Mod-A[i-1]; A[0]=n;
	for(ri int i=K-1;i;i--) B[i]=Mod-B[i-1]; B[0]=m;
	for(ri int i=0;i<K;i++) A[i]=1ll*A[i]*inv[i]%Mod, B[i]=1ll*B[i]*inv[i]%Mod;
	NTT(K,K,A,B);
	for(ri int i=1,iv=ksc(1ll*n*m%Mod,Mod-2);i<K;i++)
	{
		printf("%d\n",1ll*fac[i]*iv%Mod*A[i]%Mod)%Mod;
	}
	return 0;
}

[luogu P4705] 玩游戏

标签:rev   erase   利用   return   bin   char   insert   --   ++   

原文地址:https://www.cnblogs.com/zkdxl/p/14727838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!