CodeForces 553A Kyoya and Colored Balls (排列组合)

http://codeforces.com/problemset/problem/553/A

A. 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 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, 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种颜色给N个球涂色,每种颜色表上序号1~K,涂色规则为,涂 i 颜色球的最后一个球时,它的后面一个球为 i+1颜色的球。


分析:这是一个组合排列问题。假设K颜色球有m个,那么一个球放在最右边就好,剩下的 m-1个球排列情况有C(n-1,m-1),假设K-1颜色的球有z个,那么这是后选出一个放在最右侧的空位就好,剩余的 z-1 个排列情况有 C(n-m-1,z-1).......就这样不断地类推下去,直到第一种颜色结束。



<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int mod=1000000007;
const int maxn=1010;
int a[1010],sum[1010],b[maxn][maxn];
int k;
void combination()//预处理,计算出C(n,m)组合情况,且n,m<maxn;
{
    memset(b,0,sizeof(b));
    for(int i=1;i<maxn;i++)
    {
        b[i][0]=b[i][i]=1;
        for(int j=1;j<i;++)
        {
            b[i][j]=(b[i-1][j-1]+b[i-1][j])%mod;
        }
    }
}
int main()
{
    combination();
    while(scanf("%d",&k) != EOF)
    {
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=k;i++){
            scanf("%d",&a[i]);
            sum[i] +=sum[i-1]+a[i];//计算出前i种颜色球的总个数
        }
        LL ans=1;
        for(int i=k;i>=1;i--)
        {
            if(a[i]==1) continue;
            ans =(ans*b[sum[i]-1][a[i]-1])%mod;//从第k种开始直到第一种颜色,将所有组合乘起来
        }
        printf("%lld\n",ans);
    }
    return 0;
}
</span>


你可能感兴趣的:(CodeForces 553A Kyoya and Colored Balls (排列组合))