【47.95%】【codeforces 554C】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
【题目链接】:http://codeforces.com/contest/554/problem/C
【题解】
题意:
设第i种颜色的球出现的最后一个位置为x
第i+1种颜色的球出现的最后一个位置为y;
则严格要求x< y;
问这样的序列有多少个.
解法:
先设所有的颜色的球总共有n个;即n=c1+c2..ck;
可以从最后一种颜色即颜色为k的球开始考虑;
现在有n个位置让你摆这第k种颜色的球;
先考虑第k种颜色的最后一个球要放在哪里?
肯定是第n个位置.
因为如果不放在第n个位置,则可能会有其他的球在后面的过程中放在第n个位置,而不管这个球的颜色是什么,它的颜色编号都比k小;这就不满足上上述要求了,因为那种颜色的球最后一次出现的位置肯定比k大..
因此把第k种颜色的球的最后一个放在第n个位置;
接下来第k种颜色的球还有ck-1个;剩下的位置还有n-1个;
显然剩下的ck-1个球放在哪里都行即C(n-1,ck-1);
放完第k个球再考虑第k-1种颜色的球.
还是一样先考虑第k-1种颜色的球的最后出现的球应该放在哪里??
肯定是第k中颜色的球放剩下的位置里面的最后一个位置.道理还是一样,如果不先占据那个位置则其他编号比k-1小的球可以放在那里(设为位置x),则编号为k-1的球的最后一次出现的位置肯定小于x,而位置为x的球的编号小于k-1????显然就不合适了。。
所以先把编号为k-1的球中的一个拿出来放在位置x(即第k中颜色的球放剩下的位置里面的最后一个位置);
然后剩下n-c[k]-c[k-1]-1个位置,从中选出c[k-1]-1位置放剩下的c[k-1]-1个球….
即C(n-c[k]-c[k-1]-1,c[k-1]-1);
以此类推..
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 1e3+10;
const int MOD = 1e9+7;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int k,n = 0;
int c[MAXN][MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
c[0][0] = 1;
rep1(i,1,1000)
c[i][i] = 1,c[i][0] = 1;
rep1(i,1,1000)
rep1(j,1,1000)
{
c[i][j] =c[i-1][j]+c[i-1][j-1];
if (c[i][j] >= MOD) c[i][j]-=MOD;
}
LL ans = 1;
rei(k);
rep1(i,1,k)
{
int x;
rei(x);
n+=x;
ans = (ans*c[n-1][x-1])%MOD;
}
cout << ans << endl;
return 0;
}
【47.95%】【codeforces 554C】Kyoya and Colored Balls的更多相关文章
- codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)
题目链接: A. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces 553 A Kyoya and Colored Balls
这个题.比赛的时候一直在往dp的方向想,可是总有一个组合数学的部分没办法求, 纯粹组合数学撸,也想不到办法-- 事实上,非常显然.. 从后往前推,把第k种颜色放在最后一个,剩下的k球.还有C(剩余的位 ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- 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 diffe ...
- 554C - Kyoya and Colored Balls
554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...
- Codeforces A. Kyoya and Colored Balls(分步组合)
题目描述: Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 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 ...
- Codeforces Round#309 C Kyoya and Colored Balls
给定一个k表示颜色的种类从1到k 然后接下来k行, 每行一个数字, 代表该颜色的球有多少个 这些球都放在一个包中,然后依次拿出. 要求颜色i的最后一个球, 必须要排在颜色i+1的最后一个球前面, ...
随机推荐
- BZOJ3530: [Sdoi2014]数数(Trie图,数位Dp)
Description 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3 ...
- ORA-01078错误举例:SID的大写和小写错误
案例重演: dbca建库.SID:metro --手工建库时实例名小写的metro ...... [oracle@org54 ~]$ export ORACLE_SID=METRO ...
- Oracle实现数据不存在则插入,数据存在则更新(insert or update)
思路是写一个函数,先按条件查询数据,假设查询到数据则更新.假设没有查询到数据则插入: create or replace function fn_merge_index(statdate in dat ...
- VA对于开发QT是神器,VA自动补全QT
我怎么就忘了,VA也可以适用于VS下开发QT程序.其中QT的头文件自己增加,主要是: C:\Qt\4.8.6_2008\include 但还有一些特殊类不认识,所以还得继续增加: C:\Qt\4.8. ...
- actionbar spinner-用法实例
今天需要更改一个actionbar上的spinner的字体颜色,结果试了好长时间都没有解决,最后才发现,原来他是在代码下增加的一个textview,然后使用adapter加载的,并不是直接用frame ...
- 阿里一道Java并发面试题 (详细分析篇)
说明 前天分享了一篇关于阿里的"Java常见疑惑和陷阱"的文章,有人说这个很早就有了,可能我才注意到,看完之后发现内容非常不错,有几个我也是需要停顿下想想,如果后续有机会我录制一个 ...
- 104.tcp多线程读写实现群聊
客户端: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <w ...
- Spring MVC基础了解
参考网址:https://www.yiibai.com/spring_mvc/springmvc_overview.html Spring框架相关 Spring Security 一个灵活强大的身份验 ...
- "C:\Program Files\Internet Explorer\iexplore.exe" -extoff 无加载项启动IE 浏览器打开时全屏模式
"C:\Program Files\Internet Explorer\iexplore.exe" -extoff 无加载项启动IE浏览器打开时全屏模式
- serialport串口通讯
在.NET Framework 2.0中提供了SerialPort类,该类主要实现串口数据通信 = System.IO.Ports.SerialPort.GetPortNames();获取电脑有哪几个 ...