Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

C. Kyoya and Colored Balls

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/554/problem/C

Description

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 Input

3
2
2
1

Sample Output

3

HINT

 

题意

一个袋子里有n种颜色,然后分别告诉你,某个颜色最后拿出来的编号是什么,然后要求这些颜色的最后一个球拿出来的顺序和给定的顺序一样

问你一共有多少种拿法

题解:

排列组合,答案是PI(C(a[i]-1,sum-1)

这个排列组合是我凭感觉猜的,然后试了试,发现是对的……

然后就A了,唔,用数学解释的话,就倒着理解,就比较好懂,倒着放球

(其实这是一道数学题

代码

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

ll fac[maxn];
ll qpow(ll a,ll b)
{
    ll ans=1;a%=mod;
    for(ll i=b;i;i>>=1,a=a*a%mod)
        if(i&1)ans=ans*a%mod;
    return ans;
}
ll C(ll n,ll m)
{
    if(m>n||m<0)return 0;
    ll s1=fac[n],s2=fac[n-m]*fac[m]%mod;
    return s1*qpow(s2,mod-2)%mod;
}
int n;
int a[1005];
int sum;
int main()
{
    fac[0]=1;

    for(int i=1;i<maxn;i++)
    fac[i]=fac[i-1]*i%mod;
    int n=read();
    for(int i=0;i<n;i++)
        a[i]=read();
    ll ans=1;
    sum=a[0];
    for(int i=1;i<n;i++)
    {
        sum+=a[i];
        ans=(ans*C(sum-1,a[i]-1))%mod;
    }
    cout<<ans<<endl;
}

 

你可能感兴趣的:(codeforces)