【AGC 002F】Leftmost Ball

时间:2023-03-09 16:57:49
【AGC 002F】Leftmost Ball

Description

Snuke loves colorful balls. He has a total of N*K balls, K in each of his favorite N colors. The colors are numbered 1 through N.He will arrange all of the balls in a row from left to right, in arbitrary order. Then, for each of the N colors, he will paint the leftmost ball of that color into color 0, a color different from any of the N original colors.After painting, how many sequences of the colors of the balls are possible? Find this number modulo 109+7. 1≤N,K≤2000

Input

The input is given from Standard Input in the following format: N K

Output

Print the number of the possible sequences of the colors of the balls after painting, modulo 109+7.

题意:有$n$种颜色的球,标号$1$到$n$,每种颜色有$k$个。将$nk$个球随机排列后,将每种颜色的第一个球涂成颜色$0$,求最终可能得到的颜色序列的方案数。
分析:
令$f(i,j)~(i\leq j)$表示已经放置了i个编号为0的球与j种第一次出现的位置最靠前的颜色的方案数。每次在当前的第一个空位放置一个颜色为$0$的球或是一种未出现的颜色的球。可得转移方程:
$$f(i,j)=f(i-1,j)+\binom{n-i+(n-j+1)\cdot(k-1)-1}{k-2}\cdot(n-j+1)\cdot f(i,j-1)$$
时间复杂度$O(nk)$。
 #include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int N=2e3+;
const int mod=1e9+;
int n,m,fac[N*N],inv[N*N],f[N][N];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void Mod(int& a,int b){a+=b;if(a>=mod)a-=mod;}
int power(int a,int b)
{
int ans=;
while(b)
{
if(b&)ans=1ll*ans*a%mod;
a=1ll*a*a%mod;b>>=;
}
return ans;
}
int C(int n,int m){return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;}
int main()
{
n=read();m=read();
if(m==){printf("");return ;}
fac[]=;
for(int i=;i<=n*m;i++)fac[i]=1ll*fac[i-]*i%mod;
inv[n*m]=power(fac[n*m],mod-);
for(int i=n*m;i>=;i--)inv[i-]=1ll*inv[i]*i%mod;
f[][]=;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
{
f[i][j]=f[i-][j];
if(!j)continue;
Mod(f[i][j],1ll*f[i][j-]*(n-j+)%mod*C(n-i+(n-j+)*(m-)-,m-)%mod);
}
printf("%d",f[n][n]);
return ;
}