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种颜色的最后一个小球的后面,问有多少种排法。( 1≤n,k≤1000 )
分析:设总共n个球,每种颜色 a i 个,最后一种颜色的最后一个球一定放最后一位,剩下的组合C( a n -1,n-1),再考虑倒数第二种颜色,它的最后一个球放在最后的一个空位,剩下的组合C( a n−1 -1,n- a n -1),以此类推。总数就等于C( a n -1,n-1)C( a n−1 -1,n- a n -1)…;求组合数时用费马小定理求分母逆元即可。
吐槽:虽然是水题,让自己想起来很久没用过费马小定理,学过的东西要多用,要复习,说到底还是题目做的少了,快滚去啃题!