【题目链接】
【算法】
动态规划
f[i][j][k]表示前i行,有j列放了1个,有k列放了两个
分六种情况讨论即可
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 110 const long long MOD = 9999973; long long i,j,k,n,m,ans; long long dp[MAXN][MAXN][MAXN]; template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x/10); putchar(x%10+'0'); } template <typename T> inline void writeln(T x) { write(x); puts(""); } int main() { read(n); read(m); dp[0][0][0] = 1; for (i = 1; i <= n; i++) { for (j = 0; j <= m; j++) { for (k = 0; k <= m - j; k++) { dp[i][j][k] = dp[i-1][j][k]; if (j) dp[i][j][k] += dp[i-1][j-1][k] * (m - j - k + 1); if (j < m && k) dp[i][j][k] += dp[i-1][j+1][k-1] * (j + 1); if (j && k) dp[i][j][k] += dp[i-1][j][k-1] * j * (m - j - k + 1); if (j >= 2) dp[i][j][k] += dp[i-1][j-2][k] * (m - j - k + 2) * (m - j - k + 1) >> 1; if (j + 2 <= m && k >= 2) dp[i][j][k] += dp[i-1][j+2][k-2] * (j + 2) * (j + 1) >> 1; dp[i][j][k] %= MOD; } } } for (i = 0; i <= m; i++) { for (j = 0; j <= m - i; j++) { ans = (ans + dp[n][i][j]) % MOD; } } writeln(ans); return 0; }