Codeforces Round #309 (Div. 2) C 排列组合+费马小

来源:互联网 发布:淘宝客服图标素材 编辑:程序博客网 时间:2024/06/06 01:37



链接:戳这里


C. Kyoya and Colored Balls
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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


题意:

箱子里面装了n个球,总共k个颜色。现在从里面取出球。当取出最后一个颜色为i的球,一定在最后一个颜色为i+1的球之前取出。也就是说最后一个颜色为i+1的球在最后一个i球之后取出

问颜色取出的排列有多少种


思路:

从后往前取,这样可以确保至少有一个i+1的球在i之后

当前取得球颜色是i,还剩下n个位置没有放球,那么先拿一个颜色为i的球放在当前所有能放的位置的最后面

剩下的num[i]-1个球去放剩下的n-1个位置,直到不能放为止,所以每次的i贡献是C(n-1,a[i]-1)

取模用下费马小


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1#define mod 1000000007using namespace std;ll fac[1000100];ll ppow(ll k,ll n) {    ll c=1;    while(n) {        if(n%2) c=(c*k)%mod;        k=(k*k)%mod;        n>>=1;    }    return c;}ll C(ll a,ll b) {    return (((fac[b]*ppow((fac[a]*fac[b-a])%mod,mod-2)))%mod)%mod;}int n;int a[10010];int main(){    fac[0]=1;    for(int i=1;i<=200000;i++) fac[i]=(fac[i-1]*i)%mod;    int sum=0;    scanf("%d",&n);    for(int i=1;i<=n;i++) {        scanf("%d",&a[i]);        sum+=a[i];    }    ll ans=1;    for(int i=n;i>=1;i--){        ans=ans*C(a[i]-1,sum-1)%mod;        sum-=a[i];    }    printf("%I64d\n",ans);    return 0;}


0 0