【BZOJ2655】—calc(拉格朗日插值+生成函数+dp)
考虑每一个数的贡献
不选就相当于乘
选的话就是乘
最后就相当于求的第项系数
考虑
先强制
最后乘
表示时第项系数
显然
再展开下去
由于
可以把看做关于的次多项式(因为dp柿子每加一多一个前缀和乘一个东西)
于是出项拉格朗日插值求出
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<21|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++;
}
#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 ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define bg begin
#define re register
int mod;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=a+b>=mod?a+b-mod:a+b;}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=a>=b?a-b:a-b+mod;}
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,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline void chemx(int &a,int b){a>b?0:a=b;}
inline void chemn(int &a,int b){a<b?a=b:0;}
const int N=1015;
int f[N][N],fac[N],y[N];
inline int calc(int n,int m){
if(m<=n+1)return y[m];
int res=0;
for(int i=1;i<=n+1;i++){
int tmp=mul(mul(fac[i-1],fac[n+1-i]),dec(m,i));
tmp=ksm(tmp,mod-2);if((n+1-i)&1)Mul(tmp,dec(0,1));
Mul(tmp,y[i]),Add(res,tmp);
}for(int i=1;i<=n+1;i++)Mul(res,dec(m,i));return res;
}
int A,n;
int main(){
A=read(),n=read(),mod=read();
fac[0]=1;for(int i=1;i<N;i++)fac[i]=mul(fac[i-1],i);
f[0][0]=1;
for(int i=1;i<=n*2+1;i++){
f[i][0]=1;
for(int j=1;j<=n;j++)
f[i][j]=add(mul(f[i-1][j-1],i),f[i-1][j]);
}
for(int i=1;i<=2*n+1;i++)y[i]=f[i][n];
int res=calc(2*n,A);
Mul(res,fac[n]);
cout<<res;
}