传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5305
【题解】
1 # include <bits/stdc++.h> 2 using namespace std; 3 4 const int M = 2010; 5 6 int n, mod; 7 int C[M][M], fac[M]; 8 9 int main() { 10 cin >> n >> mod; 11 fac[0] = 1; 12 for (int i=1; i<=n; ++i) fac[i] = 1ll * fac[i-1] * i % mod; 13 C[0][0] = 1; 14 for (int i=1; i<=n; ++i) { 15 C[i][0] = 1; 16 for (int j=1; j<=i; ++j) { 17 C[i][j] = C[i-1][j] + C[i-1][j-1]; 18 if(C[i][j] >= mod) C[i][j] -= mod; 19 } 20 } 21 int ans = 0; 22 for (int i=2; i<=n; ++i) 23 for (int sz=1; sz<=n-i+1; ++sz) { 24 int tot = 1ll * fac[n-sz-1] * i % mod * (i-1) % mod * fac[sz] % mod * C[n-i][sz-1] % mod; 25 ans += 1ll * tot * sz % mod * (n-sz) % mod; 26 if(ans >= mod) ans -= mod; 27 } 28 29 cout << ans % mod << endl; 30 return 0; 31 }