Codeforces A. Kyoya and Colored Balls(分步组合)
题目描述:
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(分步组合)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- A. Kyoya and Colored Balls_排列组合,组合数
Codeforces Round #309 (Div. 1) A. Kyoya and Colored Balls time limit per test 2 seconds memory limit ...
- 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 ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- Kyoya and Colored Balls(组合数)
Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 554C - Kyoya and Colored Balls
554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...
随机推荐
- [Gamma]Scrum Meeting#8
github 本次会议项目由PM召开,时间为6月3日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客,组织例会 撰写博客,组织例会 swoip 前端显示屏幕,翻译坐 ...
- Qt应用开发常见问题
Qt判断当前操作系统? 可使用宏判断,例如: #ifdef Q_OS_MAC //mac ... #endif #ifdef Q_OS_LINUX //linux ... #endif #ifdef ...
- python数据分析4之自动采集数据
1 数据采集的重要性 数据采集是数据挖掘的基础,没有数据,挖掘也没有意义.很多时候,我们拥有多少数据源,多少数据量,以及数据质量如何,将决定我们挖掘产出的成果会怎样 2 四类采集方式 3 如何使用开放 ...
- 045 用户登录功能01----JWT和后台代码
(1)有状态登录概述 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session. 例如登录:用户登录后,我们把登录者 ...
- 图论 --- 骑士周游问题,DFS
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28630 Accepted: ...
- Docker 搭建简单 LVS
LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,是由章文嵩博士主导的开源负载均衡项目,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基 ...
- Django 定义视图函数
Django 定义视图函数 一.接收内容及文件处理 1.接收分类 # 获取数据 request.GET # 提交数据 request.POST # 获取文件 request.FILES 2.check ...
- Java学习:面向对象的三大特征:封装性、继承性、多态性之继承性
面向对象的三大特征:封装性.继承性.多态性. 继承 继承是多态的前提 ,如果没有继承,就没有多态. 继承主要解决的问题就是:共性抽取. 继承关系当中的特点: 子类可以拥有父类的“内容” 子类还可以拥有 ...
- 动态代理(一)——JDK中的动态代理
在开始动态代理的描述之前,让我们认识下代理.代理:即代替担任执行职务.在面向对象世界中,即寻找另一个对象代理目标对象与调用者交互.Java中分为静态代理和动态代理.这里对于静态代理不做详述.它们之间的 ...
- html 显示 pdf
html 显示 pdf文件四种方式: 1. <embed src="pdf/wobu.pdf" type="application/pdf" width= ...