【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的最后一个球前面, ...
随机推荐
- codevs 5960 信使
codevs 5960 信使 题目描述 Description 战争时期,前线有n个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单 ...
- 洛谷 P1724 东风早谷苗
洛谷 P1724 东风早谷苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走, ...
- ubuntu-安装中文拼音输入法
一下内容转载自http://blog.chinaunix.net/uid-24410388-id-3501873.html 自己验证了可用.转载了,已留做日后使用 步骤: step1:安装ibus所需 ...
- windows下配置mycat与常见问题解决
mycat官网:http://www.mycat.org.cn/ wiki:https://github.com/MyCATApache/Mycat-Server/wiki MyCat使用Mysql的 ...
- asp.net mvc 中的自定义验证(Custom Validation Attribute)
前言
- Node知识总结
一. 伪装URL-SEO 伪URL重写 把一个动态页面的地址重写为静态页面的地址,为了方便网站的SEO优化 真实地址:http://item.jd.com/detail.php?id=12261336 ...
- 飞镖忍者 quick-cocos2d-x3.2
经典的入门小游戏.这里用quick-cocos2d-x3.2又一次写一遍,以便熟悉下quick 首先,创建project,假设不会自行百度啊. 1.编译效果例如以下: watermark/2/text ...
- js闭包中的this(匿名函数中的this指向的是windows)
js闭包中的this(匿名函数中的this指向的是windows) 一.总结 1.普通函数中的this指向的是对象,匿名函数中的this指向的是windows,和全局变量一样 2.让匿名函数中的thi ...
- 动态规划求解序列问题(LIS、JLIS)
1. 最长递增子序列 不要求位置连续:要求大小严格递增(strictly increasing) 穷举法解题 首先以每个数字为单位分割寻找最长递增子序列: int lis(const vector&l ...
- iOS开发UI篇--一个侧滑菜单SlidingMenu
一.简介 侧滑菜单已经成为app一个极常用的设计,不管是事务类,效率类还是生活类app.侧滑菜单因Path 2.0和Facebook为开发者熟知,国内目前也有很多流行app用到了侧滑菜单,比如QQ.网 ...