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; ...
随机推荐
- 解析XML数据
在 Java 程序中读取 XML 文件的过程称为解析 XML 解析 XML 文件的方式 1) DOM 解析 (java 官方提供) 2) SAX 解析(java 官方提供) 3) JDOM 解析(第三 ...
- Linux中文件权限查看和修改
权限定义 linux文件权限分为:r读权限(4).w写权限(2).x执行权限(1) linux权限对象分为:拥有者.组用户.其他用户 权限修改: chown user:group /usr/local ...
- 【神经网络与深度学习】DCGAN及其TensorFlow源码
上一节我们提到G和D由多层感知机定义.深度学习中对图像处理应用最好的模型是CNN,那么如何把CNN与GAN结合?DCGAN是这方面最好的尝试之一.源码:https://github.com/Newmu ...
- Hibernate的Hql语句使用in关键字
原文地址:https://blog.csdn.net/u013410747/article/details/50954867
- 用Powershell强制同步Windows主机与Internet time server的时间
第一步,判断Windows Time服务是否正在运行,如果没有,则开启它. 第二步,强制同步,不知为何,往往第一次会失败,那么就多运行几次好了. Get-Service w32time | Where ...
- ZYNQ笔记(4):PL触发中断
一.ZYNQ中断框图 PL到PS部分的中断经过ICD控制器分发器后同时进入CPU1 和CPU0.从下面的表格中可以看到中断向量的具体值.PL到PS部分一共有20个中断可以使用.其中4个是快速中断.剩余 ...
- MyEclipse部署外部引用的jar到web-inf的lib目录下
在用MyEclipse开发java web项目的时候,引入了外部jar,都是以library的形式存在左边的explore中的,调试没有问题,但是部署之后呢,经常遇到个非常头疼的问题就是,这些jar不 ...
- Rdlc Mail Label
1.创建报表服务器项目RDLML. 2.新建共享数据源DataMailLabel,设置到数据库AdventureWorks的连接,并为报表指定相应的访问凭据. 3.选择Name.Color.Thumb ...
- sqlserver获得数据库非聚集索引的代码
创建Index DECLARE @zindex_sql NVARCHAR(max); SET @zindex_sql = N''; SELECT @zindex_sql = @zindex_sql + ...
- c# .netcore oracle连接工具类
1.先右键->添加NeGet包->引入Oracle.ManagedDataAccess.dll 2.将该类加入项目中 工具类: using System; using System.Col ...