Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls

C. Kyoya and Colored Balls
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ibefore 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, modulo 1 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
题意是,给点一系列的球,要保证每个下标为k的最后一个球后没有比k号小的球,排列组合的个数!
这是一个排列组合的问题,首先考虑前k个球,任意一个组合,对于k+1而言没有任何区别,所以,可以从前住后推!
对于第k号球,由于,其总是要有一个球放在最后一个,所以有m-1个球会插入到前面序列的间隔空里,前面间隔空有sum-1个空,sum是前面总的球的
个数!则列等式x1 + x2 + ... + xsum = m-1;可用组合公式解出c[sum-1][m-1];递推即可得出最后的答案!
#define N 1005
#define MOD 1000000007
int n,pri[N];
long long C[N][N];
void GetC(){
    C[0][0] =C[1][0] = C[1][1] = 1;
    for(int i=2;i<N;i++){
        C[i][0] = 1;C[i][i] = 1;
        for(int j=1;j<i;j++){
            C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD;
        }
    }
}
int main()
{
    int m;
    GetC();
    while (S(n) != EOF)
    {
        long long ans = 1;
        int sum = 0;
        FI(n){
            S(m);
            sum +=m;
            ans *= C[sum -1][m - 1];
            ans %= MOD;
        }
        cout<<ans<<endl;
    }
    return 0;
}


你可能感兴趣的:(Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls)