一定要注意要乘阶乘,细节很多.
code:
#include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; const int N=2007; int n,mod; int C[N][N],fac[N],g[N],f[N]; void Init() { fac[0]=C[0][0]=1; for(int i=1;i<=n;++i) { C[i][0]=C[i][i]=1; fac[i]=(ll)fac[i-1]*i%mod; for(int j=1;j<=i-1;++j) C[i][j]=(ll)(C[i-1][j]+C[i-1][j-1])%mod; } } int main() { // setIO("input"); scanf("%d%d",&n,&mod); Init(); f[1]=1; for(int i=2;i<=n;++i) { for(int L=0;L<=i-1;++L) { int R=i-1-L,F=0,G=0; F=(ll)((ll)f[L]*fac[R]%mod+(ll)f[R]*fac[L]%mod)%mod; G=(1ll*f[L]*fac[R]%mod*(R+1)%mod)%mod; G=(G+1ll*f[R]*fac[L]%mod*(L+1)%mod)%mod; G=(G+1ll*g[L]*fac[R]%mod)%mod; G=(G+1ll*g[R]*fac[L]%mod)%mod; f[i]=(f[i]+1ll*C[i-1][L]*F%mod)%mod; g[i]=(g[i]+1ll*C[i-1][L]*G%mod)%mod; } f[i]=(ll)(f[i]+(ll)i*fac[i]%mod)%mod; } printf("%d ",g[n]); return 0; }