codeforces 553A . Kyoya and Colored Balls 组合数学

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, modulo1 000 000 007.

Sample test(s)

input

3221

output

3

input

41234

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 31 1 2 2 32 1 1 2 3

有n个小球,有k种颜色,编号为1~k,每一个小球都被染了一种颜色,numi表示颜色为i的颜色的球有numi个。num之和=n现在问你这n个小球有多少种排列方式,满足第i种颜色的最后一个球后面的球的颜色一定是i+1(1<=i<n)

分析,先考虑第k种颜色的球,一定有一个第k种颜色的球是放在最后的位置了

 1 #include<cstdio>
 2 #include<cstring>
 3
 4 using namespace std;
 5
 6 #define ll long long
 7
 8 const int maxn=1000000+5;
 9 const int mod=1e9+7;
10
11 ll fac[maxn];
12 ll num[1005];
13
14 inline ll quick_pow(ll x)
15 {
16     ll y=mod-2;
17     ll ret=1;
18     while(y){
19         if(y&1){
20             ret=ret*x%mod;
21         }
22         x=x*x%mod;
23         y>>=1;
24     }
25     return ret;
26 }
27
28 int main()
29 {
30     fac[0]=1;
31     for(int i=1;i<maxn;i++){
32         fac[i]=(fac[i-1]*i)%mod;
33     }
34     int k;
35     scanf("%d",&k);
36     ll sum=0;
37     for(int i=1;i<=k;i++){
38         scanf("%I64d",&num[i]);
39         sum+=num[i];
40     }
41     ll ans=1;
42
43     for(int i=k;i>=1;i--){
44         ans*=fac[sum-1]*quick_pow(((fac[num[i]-1]%mod)*(fac[sum-num[i]]%mod))%mod)%mod;
45         ans%=mod;
46         sum-=num[i];
47     }
48     printf("%I64d\n",ans);
49
50     return 0;
51 }

				
时间: 2024-12-22 00:57:36

codeforces 553A . Kyoya and Colored Balls 组合数学的相关文章

Codeforces 553A. Kyoya and Colored Balls

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

Codeforces A. Kyoya and Colored Balls(分步组合)

题目描述: 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 f

Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)

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

C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))

C. Kyoya and Colored Balls 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 i

CF R309C 554C Kyoya and Colored Balls

554C Kyoya and Colored Balls 即求 复合条件的序列数量 序列最后一位必定是K. 那么对于c[k]-1个剩下的K,有n-1个位置去放置. 同理,对于颜色为k-1的球 在剩下的n-c[k]个位置中,必有一个在最靠右的空位置上. 剩下的c[k-1]-1个球放在剩下的位置上 类推 core code: while(k--){ res *= C[n-1][c[k]-1]%MOD; n -= c[k]; }

codeforces 553 A Kyoya and Colored Balls

这个题,比赛的时候一直在往dp的方向想,但是总有一个组合数学的部分没办法求, 纯粹组合数学撸,也想不到办法-- 其实,很显然.. 从后往前推,把第k种颜色放在最后一个,剩下的k球,还有C(剩余的位置,k球的总数目-1)种放法 然后讨论第k-1种...推下去就好了 但是当时没想到-- 这里要求组合数,由于比较大,用乘法逆元... 当然直接套lucas也是可以的.... time limit per test 2 seconds memory limit per test 256 megabytes

Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls

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

Codeforces Round#309 C Kyoya and Colored Balls

给定一个k表示颜色的种类从1到k 然后接下来k行, 每行一个数字, 代表该颜色的球有多少个 这些球都放在一个包中,然后依次拿出.  要求颜色i的最后一个球, 必须要排在颜色i+1的最后一个球前面,    1<=i<=k-1 我们先从小规模判断起来, 当k=2时, 当k=3时, a[2]-1个球可以在已经排好的 每个排列中的 a[0]+a[1] 个球中随便插, 因为已经排好(即位置不能变了),所以a[0]+a[1]个球可以看做是同一种颜色的球 那么这个随便插就相当于有多少种不同的排列.  可知是

A Game with Colored Balls

题目链接 题意: 给一个长度为n的字符串,每次删除字母相同切连续的串,如果有多个,删除最左边的.最长的串.每次删除输出串的字母,每个字母的下标(1-n) N (1 ≤ N ≤ 106),串只包括red ('R'), green ('G') or blue ('B') 分析: 题目比较麻烦,分析一下需要的任务: 1.每次找到最长串--优先队列 2.删除最长串后,需要合并两侧的串(如果字母相同)--加Hash的链表,set(超时) 3.每次删除需要知道这一个区间的下标都是谁--加Hash的链表,se