# AT2000 Leftmost Ball

$f[i][j]$表示当前有$i$个白球，一共放完了$j$种球

• 放白球 从$f[i - 1][j]$转移
• 放没有出现过的球 $(n - j + 1) * f[i][j - 1] * C(k - 2, n * k - i - (j - 1) * (k - 1) - 1)$

#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;
}

posted @ 2019-10-25 15:55  henry_y  阅读(...)  评论(...编辑  收藏