CodeForces #554 C Kyoya and Colored Balls
2017-03-10 23:15
274 查看
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 ibefore
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
output
input
output
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:
这是codeforces上的一道题,在一次训练赛里出现,赛时没有思路,没有做出来。
题意:
有很多球,颜色不同,其他无差别,把它按一定顺序排列,使得颜色编号小的球比颜色编号大的球先取完,是一道排列组合题目。
思路:
既然要颜色从小到大结束,那么假如我们有三种颜色 1,2,3; 分别有 2,2,1个;
我们从2号开始,最后的那个2号,它前面还有2个1号和一个2号,所以是C(3,1) (ps:前面共有三个球,2个一号一个2号,所以只要在三个位置中找一个给2号) 然后接下来是3号球,它前面有2个2号和2个一号 所以是C(4,0) (ps:前面有4个球,还剩下0个3号球,所以不需要排) 情况遍历结束 answer = C(3,1) * C(4,0) = 3;
我先把 C(n,m) 运算的表打了出来,然后后面再遍历各种球。
下面是AC代码:
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 ibefore
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
34
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
这是codeforces上的一道题,在一次训练赛里出现,赛时没有思路,没有做出来。
题意:
有很多球,颜色不同,其他无差别,把它按一定顺序排列,使得颜色编号小的球比颜色编号大的球先取完,是一道排列组合题目。
思路:
既然要颜色从小到大结束,那么假如我们有三种颜色 1,2,3; 分别有 2,2,1个;
我们从2号开始,最后的那个2号,它前面还有2个1号和一个2号,所以是C(3,1) (ps:前面共有三个球,2个一号一个2号,所以只要在三个位置中找一个给2号) 然后接下来是3号球,它前面有2个2号和2个一号 所以是C(4,0) (ps:前面有4个球,还剩下0个3号球,所以不需要排) 情况遍历结束 answer = C(3,1) * C(4,0) = 3;
我先把 C(n,m) 运算的表打了出来,然后后面再遍历各种球。
下面是AC代码:
#include<stdio.h> #define maxn 1000000007 int a[1600]; __int64 c[1050][1060]; //存C(n,m)的表 __int64 ans; int main(){ int n,m,i,j,total; for(i = 1;i <= 1000;i++) c[i][0] = 1; for(i = 1;i <= 1000;i++){ for(j = 1;j <= i;j++){ if(i == j) c[i][j] = 1; else if(i > j) c[i][j] = (c[i-1][j] + c[i-1][j-1]) % maxn; } } //上面的代码是打表 C(n,m) 的运算结果 while(scanf("%d",&n)!=EOF){ for(i = 1;i <= n;i++){ scanf("%d",&a[i]); } total = a[1]; ans = 1; for(i = 2;i <= n;i++){ // 因为 1号球总是最先取完,所以我们从2号球开始排列组合 total += a[i]; ans = (ans * c[total - 1][a[i] - 1]) % maxn; //把各个球的排列组合乘起来 } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- codeforces Kyoya and Colored Balls
- Codeforces Round#309 C Kyoya and Colored Balls
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
- Kyoya and Colored Balls CodeForces 553A(组合数学)
- Educational Codeforces Round 31 D. Boxes And Balls
- A. Bear and Three Balls CodeForces 653A
- Educational Codeforces Round 31 - D. Boxes And Balls (哈夫曼树)
- codeforces 553 A Kyoya and Colored Balls
- Codeforces Gym 101174 K. Balls and Needles (DFS)
- Educational Codeforces Round 31 D. Boxes And Balls
- AC日记——Red and Blue Balls codeforces 399b
- Educational Codeforces Round 31 D. Boxes And Balls
- codeforces#233_div2_B Red and Blue Balls
- 【47.95%】【codeforces 554C】Kyoya and Colored Balls
- Codeforces554C:Kyoya and Colored Balls(组合数学计算+费马小定理)
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
- Boxes And Balls CodeForces - 884D
- Educational Codeforces Round 31 D. Boxes And Balls
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
- CodeForces #309 Div.2 C. Kyoya and Colored Balls