codeforces 554C 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 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.

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

 

/*
 * zw.cpp
 *
 *  Created on: 2016-7-25
 *      Author: PC-100
 */
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<ctype.h>
#include<time.h>
#include<stack>
#include<queue>
#include<bitset>
#include<set>
#include<map>
#include<vector>
#include<sstream>
using namespace std;
typedef long long ll;
void fre(){freopen("in.txt","r",stdin);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define zero(a) fabs(a)<eps
#define equal(a,b) zero(a-b)
const int pi=acos(-1);
double e=2.718281828;

const ll mod=1000000007;
ll n;
ll a[1005];
ll fac[1000005];


ll qpow(ll a,ll b)
{
    ll c=1;
    while(b)
    {
        if(b&1)
            c=c*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return c;
}


ll work(ll m,ll i)
{
    return ((fac[m]%mod)*(qpow((fac[i]*fac[m-i])%mod,mod-2)%mod))%mod;
}

void init()
{
    for(int i=1;i<1000005;i++)
        fac[i]=(fac[i-1]*i)%mod;
}
int main()
{
    fac[0]=1;
    init();
    ll ans=1,sum=0;
    scanf("%I64d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
        sum+=a[i];
    }
    for(int i=n;i>=1;i--)
    {
        ans*=work(sum-1,a[i]-1);
        ans%=mod;
        sum-=a[i];
    }
    printf("%I64d\n",ans);
    return 0;
}

题意:把n个小球涂成k种颜色,然后排成一列,要求第i种颜色的最后一个小球要排在第i+1种颜色的最后一个小球的后面,问有多少种排法。( 1n,k1000  )

分析:设总共n个球,每种颜色 a i   个,最后一种颜色的最后一个球一定放最后一位,剩下的组合C( a n   -1,n-1),再考虑倒数第二种颜色,它的最后一个球放在最后的一个空位,剩下的组合C( a n1   -1,n- a n   -1),以此类推。总数就等于C( a n   -1,n-1)C( a n1   -1,n- a n   -1)…;求组合数时用费马小定理求分母逆元即可。

:西 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值