zoukankan      html  css  js  c++  java
  • 【BZOJ4228】—Tibbar的后花园(生成函数+NTT)

    传送门

    一个简单的结论是满足的要么是链要么是长度不为3的倍数的环

    链的EGFEGF显然是f(x)=x+i=2ni!2xii!=x+i=2nxi2f(x)=x+sum_{i=2}^{n}frac{i!}{2}frac{x^i}{i!}=x+sum_{i=2}^{n}frac{x^i}{2}

    环的EGFEGFg(x)=i>3and i%3!=0ni!2ixii!=i>3and i%3!=0nxi2ig(x)=sum_{i>3and i\%3!=0}^{n}frac{i!}{2i}frac{x^i}{i!}=sum_{i>3and i\%3!=0}^{n}frac{x^i} {2i}

    由于是要么链,要么环
    ans=ef+gans=e^{f+g}

    求个expexp即可

    由于模数2的最高次幂只有2121,所以预处理单位根只能处理到2212^{21}

    #include<bits/stdc++.h>
    using namespace std;
    #define gc getchar
    inline int read(){
    	char ch=gc();
    	int res=0,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;
    }
    #define re register
    #define pb push_back
    #define cs const
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define ll long long
    cs int mod=1004535809,G=3;
    inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
    inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
    inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
    inline void Dec(int &a,int b){(a-=b)<0?(a+=mod):0;}
    inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
    inline void Mul(int &a,int b){a=mul(a,b);}
    inline int ksm(int a,int b,int res=1){
    	for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));return res;
    }
    inline void chemx(ll &a,ll b){a<b?a=b:0;}
    inline void chemn(int &a,int b){a>b?a=b:0;}
    #define poly vector<int>
    #define bg begin
    cs int N=1050005,C=21;
    poly w[C+1];
    int rev[N<<2],inv[N<<2],fac[N<<2];
    inline void init_w(){
    	for(int i=1;i<=C;i++)
    	w[i].resize(1<<(i-1));
    	int wn=ksm(G,(mod-1)/(1<<C));
    	w[C][0]=1;
    	for(int i=1;i<(1<<(C-1));i++)w[C][i]=mul(w[C][i-1],wn);
    	for(int i=C-1;i;i--)
    	for(int j=0;j<(1<<(i-1));j++)
    	w[i][j]=w[i+1][j<<1];
    }
    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 ntt(poly &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.bg()+1,f.bg()+lim);
    		for(int i=0;i<lim;i++)Mul(f[i],inv[lim]);
    	}
    }
    inline poly operator *(poly a,poly b){
    	int deg=a.size()+b.size()-1,lim=1;
    	if(deg<=128){
    		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;
    	}
    	while(lim<deg)lim<<=1;
    	init_rev(lim);
    	a.resize(lim),ntt(a,lim,1);
    	b.resize(lim),ntt(b,lim,1);
    	for(int i=0;i<lim;i++)Mul(a[i],b[i]);
    	ntt(a,lim,-1),a.resize(deg);
    	return a;
    }
    inline poly deriv(poly a){
    	for(int i=0;i<a.size()-1;i++)a[i]=mul(a[i+1],i+1);
    	a.pop_back();return a;
    }
    inline poly integ(poly a){
    	a.pb(0);
    	for(int i=a.size()-1;i;i--)a[i]=mul(a[i-1],inv[i]);
    	a[0]=0;return a;
    }
    inline poly Inv(poly a,int deg){
    	poly b,c(1,ksm(a[0],mod-2));
    	for(int lim=4;lim<(deg<<2);lim<<=1){
    		b=a,b.resize(lim>>1);
    		init_rev(lim);
    		c.resize(lim),ntt(c,lim,1);
    		b.resize(lim),ntt(b,lim,1);
    		for(int i=0;i<lim;i++)c[i]=mul(c[i],dec(2,mul(b[i],c[i])));
    		ntt(c,lim,-1),c.resize(lim>>1);
    	}c.resize(deg);return c;
    }
    inline poly Ln(poly a,int deg){
    	a=integ(deriv(a)*Inv(a,deg)),a.resize(deg);return a;
    }
    inline poly exp(poly a,int deg){
    	poly b(1,1),c;a.resize(deg<<1);
    	for(int lim=2;lim<(deg<<1);lim<<=1){
    		c=Ln(b,lim);
    		for(int i=0;i<lim;i++)c[i]=dec(a[i],c[i]);
    		Add(c[0],1),b=b*c,b.resize(lim);
    	}
    	b.resize(deg);return b;
    }
    poly f;
    int n;
    inline void init(){
    	fac[0]=1,inv[0]=inv[1]=1;
    	for(int i=1;i<n*4;i++)fac[i]=mul(fac[i-1],i);
    	for(int i=2;i<n*4;i++)inv[i]=mul(mod-mod/i,inv[mod%i]);
    }
    int main(){
    	init_w();
    	n=read();
    	init(),f.resize(n+1);
    	f[1]=1;
    	for(int i=2;i<=n;i++)f[i]=inv[2];
    	for(int i=4;i<=n;i++)if(i%3!=0)Add(f[i],inv[2*i]);
    	f=exp(f,n+1);
    	cout<<mul(fac[n],f[n]);
    }
    
  • 相关阅读:
    string.Join()用法
    从List分组后重新组织数据
    Java学习笔记05--字符串
    Java学习笔记03--判断与循环语句
    Java学习笔记04--数组
    Java学习笔记02--运算符、表达式
    Java学习笔记01--数据类型
    Spring面试问答
    115个Java面试题和答案——终极列表(下)
    115个Java面试题和答案——终极列表(上)
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328696.html
Copyright © 2011-2022 走看看