Codeforces Round #309 (Div. 2)D

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.

Examples
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种不同颜色的球,然后给出不同颜色的球的个数,把球排成一列,要求第i+1种颜色的球的最后一个一定要在第i种颜色的球的最后面一个的后面,求摆放的方案数mod1e9+7

题解:下标大的先放,对与每一种球它的最后一个球的位置是确定的,然后就是在剩下的位置中挑剩下的球的个数个位置C(sum-1,a[i]-1),然后相乘。关建是求通过乘法逆元组合数。

#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define PI 3.14159265
using namespace std;
const int maxn=1e3+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
int n,sum;
int a[maxn];
ll b[(int)1e6+5]; 
ll ans=1;
ll poww(ll x,ll k)
{
    ll t=1;
    while(k)
    {
        if(k%2)
        {
            t=(t*x)%mod;
        }
        x=(x*x)%mod;
        k/=2;
    }
    return t;
 } 
ll c(ll x,ll y)//组合数公式
{
    if(x<y)return 0;
    if(y==0)return 1;
    ll t=1;
    ll tmp=(b[x-y]*b[y])%mod; 
    t=(b[x]*poww(tmp,mod-2))%mod;//费马小定理求乘法逆元 
    return t;
} 
int main()
{    
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    b[1]=1;b[0]=1;
    for(int i=2;i<=sum;i++)
    {
        b[i]=(b[i-1]*i)%mod; 
    }
    for(int i=n;i>=1;i--)
    {
        ll tmp=c(sum-1,a[i]-1);
        sum-=a[i];
        ans=(ans*tmp)%mod;
    }
    cout<<ans<<'\n';
    return 0;
}

 

posted @ 2017-10-06 15:33  lhclqslove  阅读(145)  评论(0编辑  收藏  举报