题目描述:

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 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 \(c_i\), the number of balls of the i-th color (1 ≤ \(c_i\) ≤ 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

Copy

3
2
2
1

Output

Copy

3

Input

Copy

4
1
2
3
4

Output

Copy

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

思路:

题目是说给一组有颜色的球,从袋子中去出球要求第i种颜色的求必须在第i+1种颜色的求取完之前取完,问这种取球方法有多少种。大致可以看出这是一道排列组合题,而且方案会很多(因为要取模)。一开始想的是整体怎么放,就是说我一下子就要先扣下每种颜色的一个球,固定住他们的顺序,然后在看其他的球的放法。但情况实际上十分复杂。然后想的是这是一种有重复元素的定序排列问题,但直接套公式好像又不可行。应该要分步考虑而不是全局考虑。考虑最后一个位子,肯定放最后一种颜色的球,之前的位置有\(sum-1\)个,剩余的最后颜色球放在这些位子上有\(C_{sum-1}^{a[last]-1}\)种放法(同种颜色的球无差别)。然后考虑倒数第二种颜色的最后一个球,这是忽略掉前面放好的球,只看空位,最后一个空位放一个球,其它空位放剩余倒数第二种颜色的球,有\(C_{sum-a[last]-1}^{a[last-1]-1}\)种放法。以此类推直到第一种颜色的球。

注意在实现组合数时用到了费马小定理求逆元来算组合数取模。

代码

#include <iostream>
#define max_n 1005
#define mod 1000000007
using namespace std;
int n;
long long a[max_n];
long long ans = 1;
long long sum = 0;
long long q_mod(long long a,long long b)
{
long long res = 1;
while(b)
{
if(b&1)
{
res = ((res%mod)*a)%mod;
}
a = (a*a)%mod;
b >>= 1;
}
return res;
}
long long fac[max_n];
void ini()
{
fac[0] = 1;
for(int i = 1;i<max_n;i++)
{
fac[i] = ((fac[i-1]%mod)*i)%mod;
}
}
long long inv(long long a)
{
return q_mod(a,mod-2);
}
long long comb(int n,int k)
{
if(k>n) return 0;
return (fac[n]*inv(fac[k])%mod*inv(fac[n-k])%mod)%mod;
}
int main()
{
ini();
//cout << comb(3,1) << endl;
cin >> n;
for(int i = 0;i<n;i++)
{
cin >> a[i];
sum += a[i];
}
for(int i = n-1;i>=0;i--)
{
ans = (ans%mod*(comb(sum-1,a[i]-1)%mod))%mod;
sum -= a[i];
}
cout << ans << endl;
return 0;
}

参考文章:

hellohelloC,CodeForces 553A Kyoya and Colored Balls (排列组合),https://blog.csdn.net/hellohelloc/article/details/47811913

Codeforces A. Kyoya and Colored Balls(分步组合)的更多相关文章

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

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

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

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

  5. A. Kyoya and Colored Balls_排列组合,组合数

    Codeforces Round #309 (Div. 1) A. Kyoya and Colored Balls time limit per test 2 seconds memory limit ...

  6. CF-weekly4 F. Kyoya and Colored Balls

    https://codeforces.com/gym/253910/problem/F F. Kyoya and Colored Balls time limit per test 2 seconds ...

  7. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  8. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. 554C - Kyoya and Colored Balls

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

随机推荐

  1. SUSE12.2 添加ISO为源

    152 2019-05-16 16:40:13 mkdir /mnt/DVD1 153 2019-05-16 16:40:55 mount -o loop /root/SLE-12-SP2-Serve ...

  2. Spring Cloud 升级最新 Greenwich 版本,舒服了~

    去年将 Spring Cloud 升级到了 Finchley 版本: Spring Cloud 升级最新 Finchley 版本,踩了所有的坑! 这个大版本栈长是踩了非常多的坑啊,帮助了不少小伙伴. ...

  3. 冰多多团队Gamma阶段发布说明

    Bingduoduo 语音Coding(Gamma):项目Github地址 Gamma版本新功能介绍 在gamma阶段我们推出了一个更加完整的IDE,完善了部分编辑器功能,并且优化了UI,增添了新的s ...

  4. 安装gerrit服务器

    一.环境准备 1.Java环境 gerrit依赖,用于安装gerrit环境. 下载:jdk-7u79-linux-x64.tar.gz http://www.oracle.com/technetwor ...

  5. windows server 2016 安装网卡驱动

    首先,联网分解为两个问题,一.WLAN(无线网).二.以太网(有线网) 一 .WLAN问题解决方案 1.打开服务器管理器 2.添加角色和功能 3.一直点下一步到“功能”,勾选 DirectPlay 和 ...

  6. flume到flume消息传递

    环境:两台虚拟机( 每台都有flume) 第一台slave作为消息的产生者 第二台master作为消息的接收者    IP(192.168.83.133) 原理:通过监听slave中文件的变化,获取变 ...

  7. python之路——阅读目录

    阅读目录 希望大家多多交流,有错误的地方请随时指正,笔记记得可能有点杂 一.python入门 计算机基础 编程语言发展史和python安装  二.数据类型.字符编码.文件处理 python基础数据类型 ...

  8. APUE之第5章——标准I/O库

    一.知识回顾:文件I/O 文件 I/O 是不带缓冲的 I/O(unbuffered I/O),指每个 read 和 write 都调用内核中的一个系统调用. 对于内核而言,所有打开的文件都通过文件描述 ...

  9. Linux学习笔记之vim操作指令大全

    0x00 关于Vim Vim是款强大的文本编辑器,但是众多指令需要学习,这次记录了指令大全方便以后翻阅. Vim的几种模式 正常模式:可以使用快捷键命令,或按:输入命令行. 插入模式:可以输入文本,在 ...

  10. ASP.NET Core MVC的Razor视图中,使用Html.Raw方法输出原生的html

    我们在ASP.NET Core MVC项目中,有一个Razor视图文件Index.cshtml,如下: @{ Layout = null; } <!DOCTYPE html> <ht ...