【LOJ #6069】「2017 山东一轮集训 Day4」塔(DP)

传送门

好像和魔法的碰撞那道题差不多

最后空的位置插入所有人之间的方案数就是一个组合数

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|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++;
}
inline int read(){
    char ch=gc();
    int res=0;bool 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;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
int mod;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
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 int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=105;
int f[2][N][N*N];
int n,l,vis[N],pr[N],cnt[N],tp[N],tot;
inline void init(){
	for(int i=2;i<=n;i++){
		if(!vis[i])pr[++tot]=i;
		for(int j=1;j<=tot&&i*pr[j]<=n;j++){
			vis[i*pr[j]]=1;
			if(i%pr[j]==0)break;
		}
	}
	for(int i=2;i<=n;i++){
		int x=i;
		for(int j=1;j<=tot;j++){
			while(x%pr[j]==0){
				cnt[j]++,x/=pr[j];
			}
		}
	}
}
inline int C(int x){
	memcpy(tp,cnt,sizeof(tp));
	int res=1;
	for(int i=x-n+1;i<=x;i++){
		ll tmp=i;
		for(int j=1;j<=tot;j++)
		while(tp[j]&&tmp%pr[j]==0)tmp/=pr[j],tp[j]--;
		Mul(res,tmp);
	}
	return res;
}
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read(),l=read()-n,mod=read();
	init();
	f[0][1][0]=1;
	int cur=0,s=0;
	for(int i=n;i;i--){
		cur^=1;
		memset(f[cur],0,sizeof(f[cur]));
		s+=i-1;
		for(int j=0;j<=i+1;j++)
		for(int k=0;k<=2*s;k++){
			Add(f[cur][j][k],mul(f[cur^1][j+1][k],j+1));
			if(k>=i-1)Add(f[cur][j][k],mul(f[cur^1][j][k-i+1],2*j));
			if(j&&k>=2*(i-1))Add(f[cur][j][k],mul(f[cur^1][j-1][k-2*(i-1)],j-1));
		}
	}
	int res=0;
	for(int i=0;i<=s*2&&i<=l;i++)Add(res,mul(f[cur][0][i],C(l-i+n)));
	cout<<res;
}
posted @ 2020-02-17 14:52  Stargazer_cykoi  阅读(223)  评论(0编辑  收藏  举报