codeforces 553A . Kyoya and Colored Balls 组合数学

Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are 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 will have one integer k (1 ≤ k ≤ 1000) the number of colors.

Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≤ ci ≤ 1000).

The total number of balls doesn't exceed 1000.

Output

A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo1 000 000 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 have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are:


1 2 1 2 3
1 1 2 2 3
2 1 1 2 3




有n个小球,有k种颜色,编号为1~k,每一个小球都被染了一种颜色,
numi表示颜色为i的颜色的球有numi个。
num之和=n
现在问你这n个小球有多少种排列方式,满足第i种颜色的最后一个球后面的球的颜色一定是i+1(1<=i<n)


分析,
先考虑第k种颜色的球,一定有一个第k种颜色的球是放在最后的位置了



 1 #include<cstdio>
 2 #include<cstring>
 3 
 4 using namespace std;
 5 
 6 #define ll long long
 7 
 8 const int maxn=1000000+5;
 9 const int mod=1e9+7;
10 
11 ll fac[maxn];
12 ll num[1005];
13 
14 inline ll quick_pow(ll x)
15 {
16     ll y=mod-2;
17     ll ret=1;
18     while(y){
19         if(y&1){
20             ret=ret*x%mod;
21         }
22         x=x*x%mod;
23         y>>=1;
24     }
25     return ret;
26 }
27 
28 int main()
29 {
30     fac[0]=1;
31     for(int i=1;i<maxn;i++){
32         fac[i]=(fac[i-1]*i)%mod;
33     }
34     int k;
35     scanf("%d",&k);
36     ll sum=0;
37     for(int i=1;i<=k;i++){
38         scanf("%I64d",&num[i]);
39         sum+=num[i];
40     }
41     ll ans=1;
42 
43     for(int i=k;i>=1;i--){
44         ans*=fac[sum-1]*quick_pow(((fac[num[i]-1]%mod)*(fac[sum-num[i]]%mod))%mod)%mod;
45         ans%=mod;
46         sum-=num[i];
47     }
48     printf("%I64d
",ans);
49 
50     return 0;
51 }
View Code




原文地址:https://www.cnblogs.com/-maybe/p/4796386.html