【Codeforces 891 E】 Lust(生成函数)

传送门

考虑每次贡献可以差分成 a j ∏ i ≠ j a i − ( a j − 1 ) ∏ i ≠ j a i a_j\prod_{i\not=j}a_i-(a_j-1)\prod_{i\not=j}a_i aji=jai(aj1)i=jai
那么可以发现最后答案就是 ∏ i a i − E ( ∏ i d i ) , d i 表 示 操 作 完 后 的 a i \prod_{i}a_i-E(\prod_{i}d_i),d_i表示操作完后的a_i iaiE(idi)diai
考虑求后面一块
显然可以构造生成函数
f t ( x ) = ∑ i = 0 ∞ ( a t − i ) x i i ! f_t(x)=\sum_{i=0}^{\infty}\frac{(a_t-i)x^i}{i!} ft(x)=i=0i!(ati)xi
求得就是就是 k ! n k [ x k ] ∏ i f i ( x ) \frac{k!}{n^k}[x^k]\prod_if_i(x) nkk![xk]ifi(x)
考虑 f t ( x ) = a t ∑ i x i i ! − ∑ i x i + 1 i ! = ( a t − x ) e x f_t(x)=a_t\sum_{i}\frac{x^i}{i!}-\sum_i\frac{x^{i+1}}{i!}=(a_t-x)e^x ft(x)=atii!xiii!xi+1=(atx)ex
那么考虑最后乘出来的多项式为 e n x ( ∏ t ( a t − x ) ) = e n x ∑ i g i x i e^{nx}(\prod_{t}(a_t-x))=e^{nx}\sum_{i}g_ix^i enx(t(atx))=enxigixi
那么就是 k ! n k ∑ i = 0 n f i ∗ n k − i ( k − i ) ! = k i ‾ f i / n i \frac{k!}{n^k}\sum_{i=0}^nf_i*\frac{n^{k-i}}{(k-i)!}=k^{\underline i}f_i/n^i nkk!i=0nfi(ki)!nki=kifi/ni

暴力做多项式乘法是 O ( n 2 ) O(n^2) O(n2)
如果写 m t t mtt mtt可以做到 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)

#include
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair
#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;
}
inline ll readll(){
    char ch=gc();
    ll 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;
}
inline int readstring(char *s){
	int top=0;char ch=gc();
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
	return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
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;}
cs int N=5005;
int f[N],n,k;
int a[N];
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read(),k=read();
	f[0]=1;int res=1;
	for(int i=1;i<=n;i++){
		int x=read();Mul(res,x);
		for(int j=i;~j;j--){
			Mul(f[j],x);
			if(j)Dec(f[j],f[j-1]);
		}
	}
	for(int pw1=1,pw2=1,mt=Inv(n),i=0;i<=n;i++){
		Dec(res,mul(pw1,mul(pw2,f[i])));
		Mul(pw1,k-i),Mul(pw2,mt);
	}
	cout<<res<<'\n';
	return 0;
}

你可能感兴趣的:(生成函数,多项式)