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的更多相关文章

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

  2. codeforces 553 A Kyoya and Colored Balls

    这个题.比赛的时候一直在往dp的方向想,可是总有一个组合数学的部分没办法求, 纯粹组合数学撸,也想不到办法-- 事实上,非常显然.. 从后往前推,把第k种颜色放在最后一个,剩下的k球.还有C(剩余的位 ...

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

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

  5. 554C - Kyoya and Colored Balls

    554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...

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

    题目描述: Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  7. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

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

  9. Codeforces Round#309 C Kyoya and Colored Balls

    给定一个k表示颜色的种类从1到k 然后接下来k行, 每行一个数字, 代表该颜色的球有多少个 这些球都放在一个包中,然后依次拿出.  要求颜色i的最后一个球, 必须要排在颜色i+1的最后一个球前面,   ...

随机推荐

  1. 3/21 Django框架 模板路径及模板过滤器 1.模板路径查找

    3/21 Django框架 模板路径及模板过滤器 1.模板路径查找 先找settings.py里的TEMPLATES列表下的DIRS路径.如果APP_DIRS为True,还会到注册了的APP文件夹下依 ...

  2. 理解宏的使用 extern

    如何定义一个全局变量在一个文件中,然后在其它文件中调用就行,而不需要多次extern外部声明. 由于之前的公司的程序中全局的变量使用得很多,在多个.C文件中会调用,不这样处理做的话就会多处进行exte ...

  3. MySQL 汉字拼音

    http://blog.csdn.net/u012076316/article/details/54951365 http://www.cnblogs.com/diony/p/5483108.html ...

  4. Activity学习

    http://www.360doc.com/content/13/1106/11/203871_327110236.shtml http://www.jianshu.com/p/e6971e8a8da ...

  5. android:一个Open键引发的问题!!

    1.问题简单介绍 首先描写叙述一下问题.当我们安装完APP的时候,界面会显示两个button,一个完毕键,一个Open键,点击Open键之后.进入应用.此时.我们点击HOME键.程序将会后台.然后再点 ...

  6. 【iOS开发-29】解决方式:TabBar的图片不显示,仅仅显示灰色的正方形

    (1)现象 tabbar上的图片变成一块正方形的灰色块块,原先的图片没有了. (2)原因 tabbar上的图片本质上不是一个图片.而是一个形状图片.系统对我们使用的图片也仅仅是把当中的形状" ...

  7. 转换PHP脚本成为windows的执行程序

    转换PHP脚本成为windows的执行程序 Convert a PHP script into a stand-alone windows executable I want to automate ...

  8. “-bash: !”: event not found"、echo > sudo permission denied

    1. "-bash: !": event not found" 比如当我们在 linux 命令行输入echo "Reboot your instance!&qu ...

  9. 每日技术总结:jquery datetimepicker,微博QQ好友QQ空间微信等分享接口

    前言: 1.jquery datetimepicker 今天遇到一个日期控件格式的问题,默认选中显示的并不是设定的值,而是当天的日期.于是去查了一遍文档. 参考文章:jquery datetimepi ...

  10. ECMAScript5和ECMAScript6_浏览器支持情况

    ECMAScript5浏览器支持情况: Opera 11.60 Internet Explorer 9* Firefox 4 Safari 5.1** Chrome 13 * IE9不支持严格模式 - ...