設\(f[i][j]\)表示當前有\(i\)個白球,一共放完了\(j\)種球
顯然有\(j <= i\)
對於每一個狀態目前已經放下去的球是固定了的,那麼考慮轉移c++
第二種的C是欽定第一個球放在已經構造好了的合法序列的後面第一個空位,而後剩下的\(k-2\)個球放在剩下的\(n * k - i - (j - 1) * (k - 1) - 1\)空位上。spa
#include <bits/stdc++.h> using namespace std; const int N = 2020; const int mod = 1e9 + 7; int inv[N * N], fac[N * N]; int f[N][N]; int n, k; int power(int a, int b) { a %= mod; int ans = 1; while(b) { if(b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ans; } int C(int n, int m) { return 1ll * fac[m] * inv[n] % mod * inv[m - n] % mod; } int main() { scanf("%d%d", &n, &k); if(k == 1) return printf("%d\n", 1), 0; fac[0] = 1; for(int i = 1; i < N * N; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; for(int i = 0; i < N * N; ++i) inv[i] = power(fac[i], mod - 2); for(int i = 0; i <= n; ++i) f[i][0] = 1; for(int i = 1; i <= n; ++i) { for(int j = 1; j <= i; ++j) { f[i][j] = f[i - 1][j]; (f[i][j] += 1ll * (n - j + 1) * f[i][j - 1] % mod * C(k - 2, n * k - i - (j - 1) * (k - 1) - 1) % mod) %= mod; (f[i][j] += mod) %= mod; } } printf("%lld\n", (f[n][n] % mod + mod) % mod); return 0; }