zoukankan      html  css  js  c++  java
  • HDU5322 Hope (前缀和优化dp/CDQ+NTT)

    HDU-5332(CDQ+NTT/前缀和优化dp)

    考虑依次求出\(i\)个点的答案

    假设当前有\(i-1\)个点,枚举第\(i\)个点前面的点数\(j\),则\(dp_i=dp_{i-j-1}\cdot (j+1)^2\cdot C(i-1,i-j-1)\cdot j!\)

    直接转移是\(O(n^2)\)的,可以看到是一个\(dp\)转移与差值有关,所以可以用\(CDQ\)分治+\(NTT\)解决

    关于这种简单粗暴的做法,模板题HDU-5730 题解

    以下是暴力的代码

    #include<bits/stdc++.h>
    using namespace std;
    
    #define reg register
    typedef long long ll;
    #define rep(i,a,b) for(reg int i=a,i##end=b;i<=i##end;++i)
    #define drep(i,a,b) for(reg int i=a,i##end=b;i>=i##end;--i)
    
    template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } 
    template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); } 
    
    char IO;
    int rd(){
    	int s=0,f=0;
    	while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    	do s=(s<<1)+(s<<3)+(IO^'0');
    	while(isdigit(IO=getchar()));
    	return f?-s:s;
    }
    
    const int N=(1<<18)+4,P=998244353;
    
    int n=1e5;
    ll dp[N];
    ll A[N],B[N];
    ll Inv[N],Fac[N];
    int rev[N];
    ll qpow(ll x,ll k) {
    	ll res=1;
    	for(;k;k>>=1,x=x*x%P) if(k&1) res=res*x%P;
    	return res;
    }
    
    void NTT(int n,ll *a,int f){
    	rep(i,1,n-1) if(rev[i]>i) swap(a[i],a[rev[i]]);
    	for(reg int i=1;i<n;i<<=1) {
    		int len=i*2;
    		ll w=qpow(f==1?3:(P+1)/3,(P-1)/i/2);
    		for(reg int l=0;l<n;l+=len) {
    			ll e=1;
    			for(reg int j=l;j<l+i;++j,e=e*w%P) {
    				ll t=a[j+i]*e;
    				a[j+i]=(a[j]-t)%P;
    				a[j]=(a[j]+t)%P;
    			}
    		}
    	}
    	if(f==-1) {
    		ll base=qpow(n,P-2);
    		rep(i,0,n-1) A[i]=(A[i]*base%P+P)%P;
    	}
    }
    
    void Solve(int l,int r){ 
    	if(l==r) return;
    	int mid=(l+r)>>1;
    	Solve(l,mid);
    	int R=1,c=-1;
    	while(R<=r-l+1) R<<=1,c++;
    	rep(i,1,R) rev[i]=(rev[i>>1]>>1)|((i&1)<<c);
    	rep(i,l,mid) A[i-l]=dp[i]*Inv[i]%P;
    	rep(i,1,r-l+1) B[i]=1ll*i*i%P;
    	NTT(R,A,1),NTT(R,B,1);
    	rep(i,0,R) A[i]=A[i]*B[i]%P;
    	NTT(R,A,-1);
    	rep(i,mid+1,r) dp[i]=(dp[i]+A[i-l]*Fac[i-1])%P;
    	rep(i,0,R) A[i]=B[i]=0;
    	Solve(mid+1,r);
    }
    
    int main(){
    	Fac[0]=Fac[1]=Inv[0]=Inv[1]=1;
    	rep(i,2,n) {
    		Fac[i]=Fac[i-1]*i%P;
    		Inv[i]=(P-P/i)*Inv[P%i]%P;
    	}
    	rep(i,2,n) Inv[i]=Inv[i-1]*Inv[i]%P;
    	dp[0]=1;
    	Solve(0,n);
    	while(~scanf("%d",&n)) printf("%lld\n",dp[n]);
    }
    
    
    
    

    \[\ \]

    下面是\(O(n)\)做法

    观察转移\(dp_i=dp_{i-j-1}\cdot (j+1)^2\cdot C(i-1,i-j-1)\cdot j!\)

    变形一下\(dp_i=dp_j\cdot (i-j)^2\cdot \frac{(i-1)!}{j!}\)

    考虑前缀和转移

    可以看到阶乘的问题可以通过参数分离很好地解决,就是\((i-j)^2\)的问题

    \(i\rightarrow i+1\)时,\((i-j)^2\rightarrow (i+1-j)^2\)

    提出来看一下,就是\(x^2\rightarrow(x+1)^2=x^2+2x+1\)

    所以我们可以记录\(a=\sum x^2dp_j,b=\sum xdp_j,c=\sum dp_j\)

    所以每次\(i\)增加,\(a=a+2b+c,b=b+c\)即可

    #include<bits/stdc++.h>
    using namespace std;
    
    #define reg register
    typedef long long ll;
    #define rep(i,a,b) for(reg int i=a,i##end=b;i<=i##end;++i)
    #define drep(i,a,b) for(reg int i=a,i##end=b;i>=i##end;--i)
    
    template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } 
    template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); } 
    
    char IO;
    int rd(){
    	int s=0,f=0;
    	while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    	do s=(s<<1)+(s<<3)+(IO^'0');
    	while(isdigit(IO=getchar()));
    	return f?-s:s;
    }
    
    const double PI=acos(-1);
    
    const int N=1e5+10,P=998244353;
    
    
    int n=1e5;
    ll Fac[N],Inv[N];
    ll dp[N];
    int main(){
    	Fac[0]=Fac[1]=Inv[0]=Inv[1]=1;
    	rep(i,2,n) {
    		Fac[i]=Fac[i-1]*i%P;
    		Inv[i]=(P-P/i)*Inv[P%i]%P;
    	}
    	rep(i,2,n) Inv[i]=Inv[i-1]*Inv[i]%P;
    	dp[0]=1;
    	ll x=1,y=1,z=1;
    	rep(i,1,n) {
    		dp[i]=x*Fac[i-1]%P; // 累上(i-1)!
    		x=(x+2*y+z)%P; // x
    		y=(y+z)%P;     // y
    		ll t=dp[i]*Inv[i]%P; // 考虑i的贡献
    		x=(x+t)%P;
    		y=(y+t)%P;
    		z=(z+t)%P;
    	}
    	while(~scanf("%d",&n)) printf("%lld\n",dp[n]);
    }
    
    
    
  • 相关阅读:
    端口
    IDEA 快捷建
    Correct the classpath of your application so that it contains a single, compatible version of javax.
    Consider renaming one of the beans or enabling overriding by setting
    Idea 关于SpringBoot的@AutoWired 注入问题--无法自动装配Could not autowire. No beans of 'UserMapper' type found. more...
    数据库相关问题
    Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings. E
    爬虫
    CMS总结
    rust 打印当前时间
  • 原文地址:https://www.cnblogs.com/chasedeath/p/12101410.html
Copyright © 2011-2022 走看看