1. 程式人生 > >【AGC 002F】Leftmost Ball

【AGC 002F】Leftmost Ball

LG 技術 ive pri ros 可能 出現 none splay

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