码迷,mamicode.com
首页 > 其他好文 > 详细

【AGC 002F】Leftmost Ball

时间:2018-04-21 10:13:22      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:lov   family   find   bsp   AC   while   long   编号   ble   

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)$
 
技术分享图片
 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];
 9 int read()
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 {
30     n=read();m=read();
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

 

【AGC 002F】Leftmost Ball

标签:lov   family   find   bsp   AC   while   long   编号   ble   

原文地址:https://www.cnblogs.com/zsnuo/p/8898199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!