Codeforces Round #309 (Div. 1)

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

解题报告:

1、可以从后往前思考,先把第n种颜色的,最后一个球放到最后,然后将这个颜色的其余的球随便放,然后将第(n-1)种颜色的球放到,之前放的球的最前一个的前面,递推下去。

2、递推公式:

for(int i=n;i>=;i--)
{
if(cnt[i]==) continue;
ans=(ans*C[c-][cnt[i]-])% mod;
c-=cnt[i];
}

3、组合数递推公式:

void init()
{
memset(C, , sizeof(C));
C[][] = ;
C[][] = C[][] = ;
for(int i = ; i <= ; ++i)
{
C[i][] = C[i][i] = ;
for(int j = ; j < i; ++j)
{
C[i][j] = (C[i-][j-] + C[i-][j]) % mod;
}
}
}
#include <bits/stdc++.h>

using namespace std;
typedef long long ll; const int maxn = ;
const ll mod = ;
ll C[maxn][maxn]; void init()
{
memset(C, , sizeof(C));
C[][] = ;
C[][] = C[][] = ;
for(int i = ; i <= ; ++i)
{
C[i][] = C[i][i] = ;
for(int j = ; j < i; ++j)
{
C[i][j] = (C[i-][j-] + C[i-][j]) % mod;
}
}
} int cnt[maxn]; int main()
{ init();
int n;
int c = ;
ll ans = ;
scanf("%d", &n);
for(int i = ; i <= n; i++)
{
scanf("%d", &cnt[i]);
c += cnt[i];
} for(int i = n; i >= ; i--)
{
if(cnt[i] == ) continue;
ans = (ans * C[c-][cnt[i]-]) % mod;
c -= cnt[i];
} cout << ans << endl; return ;
}

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. 排列组合+组合数取模 HDU 5894

    // 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...

  3. 554C - Kyoya and Colored Balls

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

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

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

  5. Kyoya and Colored Balls(组合数)

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

  6. Codeforces554 C Kyoya and Colored Balls

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

  7. HDU 1521 排列组合 指数型母函数

    排列组合 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  8. UVaLive 7360 Run Step (排列组合,枚举)

    题意:给定一个数 n ,表示一共有 n 步,然后你可以迈一步也可以迈两步,但是左腿和右腿的一步和两步数要一样,并且两步数不小于一步数,问你有多少种方式. 析:虽然是排列组合,但还是不会做.....水啊 ...

  9. 【CF521C】【排列组合】Pluses everywhere

    Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote ...

随机推荐

  1. sqlserver 事务日志

    预写式日志(Write-Ahead Logging (WAL)) --在数据写入到数据库之前,先写入到日志. 1.”Begin Tran”记录  -> 缓冲区 2. 日志             ...

  2. css3 box-shadow 用法

    第1个值水平偏移值 第2个值垂直偏移值 第3个值设置对象的阴影模糊值 第4个值设置对象的阴影外延值 外阴影常规效果box-shadow:5px 5px rgba(0,0,0,.6); 外阴影模糊效果b ...

  3. shell脚本学习(1)

    格式: #!/bin/bash                              :标志一个shell脚本 #Filename:first_shell.sh          :文件名称 #a ...

  4. 在新项目下使用rbc权限

    例子说明: 8 个步骤 1. 拷贝rabc这个app到项目中 2. 把迁移过来的rbac下migrations记录删除 3. 把rabc注册到项目中 4. 创建数据库迁移 5. 注册admin,录入数 ...

  5. VUE验证器哪家强? VeeValidate absolutely!

    VUE验证器哪家强? VeeValidate absolutely! vee-validate表单验证用法 github地址:https://github.com/baianat/vee-valida ...

  6. leetcode 925. 长按键入

    题目描述: 你的朋友正在使用键盘输入他的名字 name.偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次. 你将会检查键盘输入的字符 typed.如果它对应的可能是你的朋友的 ...

  7. Python + Selenium 基础篇 - 打开和关闭浏览器

    1.首先要下载浏览器对应的driver,并放到你的python安装目录 Chrome浏览器(chromedriver): http://npm.taobao.org/mirrors/chromedri ...

  8. input类型为number去除上下按钮浏览器兼容方法

    <input type="number" ...> input::-webkit-outer-spin-button, input::-webkit-inner-spi ...

  9. leetcode 620. Not Boring Movies 用where语句判断

    https://leetcode.com/problems/not-boring-movies/description/ 不管题目简不简单,现在先熟悉语法. 直接用where语句判断即可,判断奇偶可以 ...

  10. eclipse启动的时候报错An internal error occurred during: "Initializing Java Tooling"

    eclipse ->windows ->Perspactive -> Reset perspactive 重置视图可以解决