P4463 [國家集訓隊] calc(拉格朗日插值)
阿新 • • 發佈:2019-01-04
設\(dp[i][j]\)為考慮\(i\)個數,其中最大值不超過\(j\)的答案,那麼轉移為\[dp[i][j]=dp[i-1][j-1]\times i\times j+dp[i][j-1]\]
即最大值不超過\(j-1\)的答案加上最大值剛好為\(j\)的答案,乘上\(i\)是因為\(j\)可以放在\(i\)個數裡隨便哪個位置
考慮把轉移拆開\[dp[i][j]=\sum_{k=0}^{j-1}dp[i-1][k]\times i\times (k+1)\]
如果把\(i\)看成列,\(j\)看成行,那麼這個轉移就是對第\(i-1\)列把第\(k\)行乘上一個係數然後再求和之後加到第\(i+1\)
拉格朗日插值插一插就好了
//minamoto #include<bits/stdc++.h> #define R register #define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i) #define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i) #define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v) using namespace std; char buf[1<<21],*p1=buf,*p2=buf; inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;} int read(){ R int res,f=1;R char ch; while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1); for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0'); return res*f; } char sr[1<<21],z[20];int C=-1,Z=0; inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;} void print(R int x){ if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x; while(z[++Z]=x%10+48,x/=10); while(sr[++C]=z[Z],--Z);sr[++C]='\n'; } const int N=1005; int A,n,P,ans,dp[N][N],lim; inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;} inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;} inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;} int ksm(R int x,R int y){ R int res=1; for(;y;y>>=1,x=mul(x,x))if(y&1)res=mul(res,x); return res; } int Large(int *f,int n,int k){ if(k<=n)return f[k]; int res=0,tmp=1,ty=n&1?P-1:1; fp(i,1,n)tmp=1ll*tmp*(k-i)%P*ksm(i,P-2)%P; fp(i,0,n){ res=add(res,1ll*ty*tmp%P*f[i]%P); tmp=1ll*tmp*(k-i)%P*ksm(k-i-1,P-2)%P*(n-i)%P*ksm(i+1,P-2)%P; ty=P-ty; }return res; } int main(){ // freopen("testdata.in","r",stdin); A=read(),n=read(),P=read(),lim=(n<<1); fp(i,0,lim)dp[0][i]=1; fp(i,1,n)fp(j,1,lim)dp[i][j]=add(1ll*dp[i-1][j-1]*i%P*j%P,dp[i][j-1]); printf("%d\n",Large(dp[n],lim,A)); return 0; }