Kyoya Ootori have a bag with n colored balls that is colored with k different colors. The colors is labeled from 1 to K. Balls of the same color is indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color I + 1 For all i from 1 to K -1. Now he wonders how many different ways this can happen.
Input
The first line of input would have one integer k (1≤ k ≤1000) the number of colors.
Then, K lines would follow. The i-th line would contain ci, the number of balls of the I-t H Color (1≤ ci ≤1000).
The total number of balls doesn ' t exceed 1000.
Output
A single Integer, the number of ways this Kyoya can draw the balls from the bag as described in the statement, modulo1 007.
Sample Test (s)input
3
2
2
1
Output
3
input
4
1
2
3
4
Output
1680
Note
In the first sample, we had 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya is:
1 2 1) 2 3
1 1 2) 2 3
2 1 1) 2 3
There are n balls, K colors, numbered 1~k, each ball is dyed a color,
Numi that the color of I is a ball with Numi.
Num's sum =n
Now ask you how many of these n balls are arranged, the color of the ball that satisfies the last ball of the first I color must be i+1 (1<=i<n)
Analysis
First consider the k color of the ball, there must be a k color ball is placed in the last position
1#include <cstdio>2#include <cstring>3 4 using namespacestd;5 6 #definell Long Long7 8 Const intmaxn=1000000+5;9 Const intmod=1e9+7;Ten One ll FAC[MAXN]; All num[1005]; - - Inline ll Quick_pow (ll x) the { -ll y=mod-2; -ll ret=1; - while(y) { + if(y&1){ -ret=ret*x%MoD; + } Ax=x*x%MoD; aty>>=1; - } - returnret; - } - - intMain () in { -fac[0]=1; to for(intI=1; i<maxn;i++){ +Fac[i]= (fac[i-1]*i)%MoD; - } the intK; *scanf"%d",&k); $ll sum=0;Panax Notoginseng for(intI=1; i<=k;i++){ -scanf"%i64d",&num[i]); thesum+=Num[i]; + } All ans=1; the + for(inti=k;i>=1; i--){ -ans*=fac[sum-1]*quick_pow (((fac[num[i]-1]%MOD) * (fac[sum-num[i]]%mod))%mod)%MoD; $ans%=MoD; $sum-=Num[i]; - } -printf"%i64d\n", ans); the - return 0;Wuyi}
View Code
Codeforces 553A. Kyoya and colored Balls combinatorial mathematics