A. Kyoya and Colored Balls_排列组合,组合数
2016-04-01 22:00
260 查看
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
output
input
output
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、可以从后往前思考,先把第n种颜色的,最后一个球放到最后,然后将这个颜色的其余的球随便放,然后将第(n-1)种颜色的球放到,之前放的球的最前一个的前面,递推下去。
2、递推公式:
3、组合数递推公式:
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>=1;i--) { if(cnt[i]==0) continue; ans=(ans*C[c-1][cnt[i]-])% mod; c-=cnt[i]; }
3、组合数递推公式:
void init() { memset(C, 0, sizeof(C)); C[0][0] = 1; C[1][0] = C[1][1] = 1; for(int i = 2; i <= 1000; ++i) { C[i][0] = C[i][i] = 1; for(int j = 1; j < i; ++j) { C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod; } } }
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
const ll mod = 1000000007;
ll C[maxn][maxn];
void init() { memset(C, 0, sizeof(C)); C[0][0] = 1; C[1][0] = C[1][1] = 1; for(int i = 2; i <= 1000; ++i) { C[i][0] = C[i][i] = 1; for(int j = 1; j < i; ++j) { C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod; } } }
int cnt[maxn];
int main()
{
init();
int n;
int c = 0;
ll ans = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &cnt[i]);
c += cnt[i];
}
for(int i = n; i >= 1; i--)
{
if(cnt[i] == 0) continue;
ans = (ans * C[c-1][cnt[i]-1]) % mod;
c -= cnt[i];
}
cout << ans << endl;
return 0;
}
相关文章推荐
- Android:Date、String、Long三种日期类型之间的相互转换
- Android:Date、String、Long三种日期类型之间的相互转换
- U盘安装Windows 10和Ubuntu Linux双系统经验分享
- A2P模式下的e租宝是否值得投资
- M2_REPO
- 非对称技术栈实现AES加密解密
- 尽量使用append()追加
- 《算法(第四版)》排序-----选择排序
- 静态初始化代码块启动新线程执行初始化
- CodeForces 19D Points
- 27 API-反射(类的加载器,反射的使用,动态代理)&设计模式(装饰设计模式,模版设计模式)&JDK新特性(JDK5,JDK6,JDK7,DK8)
- 进程和线程
- 读《九败一胜》有感
- 笔记:C语言文件处理
- JS中BOM相关知识点总结
- 图像常用特征
- 2016.4.1
- Struts2中的Action
- 开发Flex for Android第一个ANE(ActionScript Native Extensions)本地扩展
- Linux例行性工作(at/cron)