1. 程式人生 > >BZOJ 3326: [Scoi2013]數數

BZOJ 3326: [Scoi2013]數數

brush calc cpp clu highlight pre 數位 ring pri

數位DP,然而式子真的復雜

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int mod=20130427;
int ans,B,N,A[100005],Suf[100005][2],Szsuf[100005][2],Sum[100005][2],a[100005];
int calc(){
	int suf=0,sum=0;
	for (int i=1; i<=N; i++){
		suf=(1ll*suf*B%mod+1ll*A[i]*i%mod)%mod;
		(sum+=suf)%=mod;
	}
	return sum;
}
int solve(){
	for (int i=1; i<=N; i++){
		int C=B;
		if (i==1) C=0;
		a[i]=1ll*(C-1)+1ll*a[i-1]*B%mod+A[i];
		a[i]%=mod;
		Szsuf[i][0]=Szsuf[i-1][0]+1;
		Szsuf[i][0]%=mod;
		Szsuf[i][1]=1ll*C-1+1ll*(Szsuf[i-1][1]+a[i-1])*B%mod+1ll*(Szsuf[i-1][0]+1)*A[i]%mod;
		Szsuf[i][1]%=mod;
		Suf[i][1]=1ll*Suf[i-1][1]*B%mod+1ll*A[i]*Szsuf[i][0]%mod;
		Suf[i][1]%=mod;
		Suf[i][0]=1ll*C*(C-1)/2%mod+1ll*Suf[i-1][0]*B%mod*B%mod+1ll*B*(B-1)/2%mod*(Szsuf[i-1][1]+a[i-1])%mod;
		Suf[i][0]%=mod;
		Suf[i][0]+=1ll*Suf[i-1][1]*B%mod*A[i]%mod+1ll*A[i]*(A[i]-1)/2%mod*Szsuf[i][0]%mod;
		Suf[i][0]%=mod;
		Sum[i][1]=Sum[i-1][1]+Suf[i][1];
		Sum[i][1]%=mod;
		Sum[i][0]=1ll*Sum[i-1][0]*B%mod+1ll*Sum[i-1][1]*A[i]%mod+Suf[i][0];
		Sum[i][0]%=mod;
	}
	return (Sum[N][0]+Sum[N][1])%mod;
}
int main(){
	scanf("%d",&B);
	scanf("%d",&N);
	for (int i=1; i<=N; i++) scanf("%d",&A[i]);
	ans-=solve();
	ans+=calc();
	scanf("%d",&N);
	for (int i=1; i<=N; i++) scanf("%d",&A[i]);
	ans%=mod;
	ans+=solve();
	(ans+=mod)%=mod;
	printf("%d\n",ans);
	return 0;
}

  


BZOJ 3326: [Scoi2013]數數