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]个球可以看做是同一种颜色的球

那么这个随便插就相当于有多少种不同的排列。  可知是

这是在每个排列中随便插的结果, 总共有个排列, k=3时,总的取法是两个式子相乘

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <string>
12 #include <math.h>
13 using namespace std;
14 #pragma warning(disable:4996)
15 typedef long long LL;
16 const int INF = 1<<30;
17 /*
18
19 */
20 const int MOD = 1000000007;
21 int a[1000 + 10];
22 LL fact[1000001];
23
24 LL MyPow(LL a, LL b)
25 {
26     LL ret = 1;
27     while (b)
28     {
29         if (b & 1)
30             ret = ret * a % MOD;
31         a = a * a % MOD;
32         b >>= 1;
33     }
34     return ret;
35 }
36 LL C(int n, int m)
37 {
38     if (m > n || m < 0) return 0;
39     LL a = fact[n], b = fact[n - m] * fact[m] % MOD;
40     return a * MyPow(b, MOD - 2) % MOD;//除以一个数,等于乘以这个数的乘法逆元, 然后是在MOD的情况下
41 }
42
43 int main()
44 {
45     fact[0] = 1;
46     for (int i = 1; i < 1000001; ++i)
47         fact[i] = fact[i - 1] * i %MOD;
48     int n;
49     scanf("%d", &n);
50     for (int i = 0; i < n; ++i)
51         scanf("%d", &a[i]);
52     int sum = a[0];
53     LL ans = 1;
54     for (int i = 1; i < n; ++i)
55     {
56         sum += a[i];
57         ans = ans * C(sum - 1, sum - a[i] - 1) % MOD;
58     }
59     printf("%I64d\n", ans);
60     return 0;
61 }
时间: 2024-11-06 08:05:48

Codeforces Round#309 C Kyoya and Colored Balls的相关文章

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

找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

题目传送门 1 /* 2 找规律,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e4 + 10; 12 const int INF = 0x3f3f3f3f; 13 char s

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

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 Round #309 (Div. 2)

A. Kyoya and Colored Balls 一个背包有K种颜色一共N个球,颜色分别为1,2,3...K,颜色相同的球是不区别的.现在从背包里一个一个拿球直到所有球拿光,在拿光第I+1种颜色球之前一定要拿光第I种颜色的球,求有多少种拿法 如 果只有一种颜色的球,拿的方法只有一种.如果有两种,留下一个颜色为2的放在最后一个,剩下的N1个颜色1和N2-1个颜色2的可以随意摆,方法数为 1*\(N1+N2-1 \choose N2-1\):如果有三种相当于先排好颜色为1,2的,然后剩下N3-1

贪心 Codeforces Round #309 (Div. 2) B. Ohana Cleans Up

题目传送门 1 /* 2 题意:某几列的数字翻转,使得某些行全为1,求出最多能有几行 3 想了好久都没有思路,看了代码才知道不用蠢办法,匹配初始相同的行最多能有几对就好了,不必翻转 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <string> 8 #include <cmath> 9 #include <iostream> 10 using namespace std; 1

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 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 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