【BZOJ2655】—calc(拉格朗日插值+生成函数+dp)

传送门


考虑每一个数ii的贡献
不选就相当于乘11
选的话就是乘ii

最后就相当于求i=1A(1+ix)\prod_{i=1}^{A}(1+ix)的第nn项系数

考虑dpdp
先强制a1a2ana_1\le a_2……\le a_n
最后乘n!n!
f[i][j]f[i][j]表示A=iA=i时第jj项系数
显然f[i][j]=f[i1][j1]i+f[i1][j]f[i][j]=f[i-1][j-1]*i+f[i-1][j]

再展开f[i1][j]f[i-1][j]下去
f[i][j]=f[i1][j1]i+f[i2][j1](i1)=k=1i1(k+1)f[k][j1]f[i][j]=f[i-1][j-1]*i+f[i-2][j-1]*(i-1)…=\sum_{k=1}^{i-1}(k+1)f[k][j-1]
由于f[i][i]=i!,f[i][j](ij)=0f[i][i]=i!,f[i][j](i\le j)=0
可以把f[i][j]f[i][j]看做关于ii2j2j次多项式(因为dp柿子jj每加一多一个前缀和乘一个东西)

于是dpdp2n+12n+1项拉格朗日插值求出f[A][n]f[A][n]

#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;
}
posted @ 2019-07-02 19:51  Stargazer_cykoi  阅读(239)  评论(0编辑  收藏  举报