【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.

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #define LL long long
5 using namespace std;
6 const int N=2e3+5;
7 const int mod=1e9+7;
8 int n,m,fac[N*N],inv[N*N],f[N][N];
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 void Mod(int& a,int b){a+=b;if(a>=mod)a-=mod;}
17 int power(int a,int b)
18 {
19     int ans=1;
20     while(b)
21     {
22         if(b&1)ans=1ll*ans*a%mod;
23         a=1ll*a*a%mod;b>>=1;
24     }
25     return ans;
26 }
27 int C(int n,int m){return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;}
28 int main()
29 {
31     if(m==1){printf("1");return 0;}
32     fac[0]=1;
33     for(int i=1;i<=n*m;i++)fac[i]=1ll*fac[i-1]*i%mod;
34     inv[n*m]=power(fac[n*m],mod-2);
35     for(int i=n*m;i>=1;i--)inv[i-1]=1ll*inv[i]*i%mod;
36     f[0][0]=1;
37     for(int i=1;i<=n;i++)
38         for(int j=0;j<=i;j++)
39         {
40             f[i][j]=f[i-1][j];
41             if(!j)continue;
42             Mod(f[i][j],1ll*f[i][j-1]*(n-j+1)%mod*C(n-i+(n-j+1)*(m-1)-1,m-2)%mod);
43         }
44     printf("%d",f[n][n]);
45     return 0;
46 }
View Code

posted @ 2018-04-21 09:51  Zsnuo  阅读(392)  评论(0编辑  收藏  举报