题解 P7986 [USACO21DEC] HILO P
serene_analysis · · 题解
先考虑暴力 DP。设
上面那个 DP 状态是
实际上把式子写出来化简是可以做到
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
const int maxn=5e3+5;
const int mod=1e9+7;
int inv[maxn],fact[maxn],finv[maxn];
void init(){
inv[0]=inv[1]=fact[0]=fact[1]=finv[0]=finv[1]=1;
for(int i=2;i<=maxn-2;i++){
inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
fact[i]=1ll*fact[i-1]*i%mod;
finv[i]=1ll*finv[i-1]*inv[i]%mod;
}
return;
}
int biga(int n,int k){return 1ll*fact[n]*finv[n-k]%mod;}
int n,x;
signed main(){
init();
scanf("%d%d",&n,&x);
int ans=0;
for(int j=x+1;j<=n;j++)for(int i=0;i<=x-1;i++){
int ba=n-j,fr=std::max(0,i-1),p=x-i,q=j-x-1;
(ans+=1ll*biga(n,ba+fr)*fact[p+q-1]%mod*p%mod)%=mod;
}
printf("%d",ans);
return 0;
}
//namespace burningContract
感谢你的阅读。