C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))
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.
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.
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.
3
2
2
1
3
4
1
2
3
4
1680
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
题意:n种不同颜色的球。有k[n]个,要求每种颜色的球的最后一个的相对初始状态(球的种类)的位置不变;问有多少种组合
思路:定最后一个球,其它的球(同样的颜色)在前面任选,之后再定最后另外一种颜色的球<放在剩下空中离最后一个位置近期的地方>,然后剩下的任选。。 。以此类推。
题目链接:http://codeforces.com/contest/554/problem/C
转载请注明出处:寻找&星空の孩子
用了个费马小定理优化了下。也不可不优化。(a=1 mod (p-1),gcd(a,p)=1)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
const LL mod = 1000000007;
LL n;
LL a[1005];
LL fac[1000005]; LL ppow(LL a,LL b)
{
LL c=1;
while(b)
{
if(b&1) c=c*a%mod;
b>>=1;
a=a*a%mod;
}
return c;
} LL work(LL m,LL i)
{
return ((fac[m]%mod)*(ppow((fac[i]*fac[m-i])%mod,mod-2)%mod))%mod;
} int main()
{
LL i,j,k;
fac[0] = 1;
for(i = 1; i<1000005; i++)
fac[i]=(fac[i-1]*i)%mod;
LL ans = 1,sum = 0;
scanf("%I64d",&n);
for(i = 1; i<=n; i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
for(i = n; i>=1; i--)
{
ans*=work(sum-1,a[i]-1);
ans%=mod;
sum-=a[i];
}
printf("%I64d\n",ans); return 0;
}
C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))的更多相关文章
- A. Kyoya and Photobooks(Codeforces Round #309 (Div. 2))
A. Kyoya and Photobooks Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He ...
- 找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks
题目传送门 /* 找规律,水 */ #include <cstdio> #include <iostream> #include <algorithm> #incl ...
- 贪心 Codeforces Round #309 (Div. 2) B. Ohana Cleans Up
题目传送门 /* 题意:某几列的数字翻转,使得某些行全为1,求出最多能有几行 想了好久都没有思路,看了代码才知道不用蠢办法,匹配初始相同的行最多能有几对就好了,不必翻转 */ #include < ...
- 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 Round #309 (Div. 2)
A. Kyoya and Photobooks Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He ha ...
- Codeforces Round #309 (Div. 2)D
C. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #309 (Div. 1)
A. Kyoya and Colored Balls 大意: 给定$k$种颜色的球, 第$i$种颜色有$c_i$个, 一个合法的排列方案满足最后一个第$i$种球的下一个球为第$i+1$种球, 求合法方 ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造
B. Kyoya and Permutation Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
随机推荐
- 关于Linux CentOS 7 时区时间修改问题
原文:http://blog.csdn.net/yin138/article/details/52765089 今天遇到时区的问题,操作系统为CentOS 7 1. 首先进入终端,使用su root ...
- Mac Os系统设置
显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults wri ...
- MySQL锁学习之UPDATE
##==============================================================================## 学MySQL也蛮长时间了,可一直停 ...
- php 文档操作
ftp_mkdir() 函数在 FTP 服务器上建立新目录. 语法 ftp_mkdir(ftp_connection,dir) 参数 描述 ftp_connection 必需.规定要使用的 FTP 连 ...
- C# 读写文本文件乱码解决方案
在使用C#对文本文件读取的时候,如果其中包含了中文,经常会出现乱码.一般解决是在StreamReader加一个编码,我使用的是Encoding.UTF8,一般情况下使用这个参数就可以.但是,在这次我使 ...
- 结合java.util.TreeMap源码理解红黑树
前言 本篇将结合JDK1.6的TreeMap源码,来一起探索红-黑树的奥秘.红黑树是解决二叉搜索树的非平衡问题. 当插入(或者删除)一个新节点时,为了使树保持平衡,必须遵循一定的规则,这个规则就是红- ...
- Python之re正则模块
正则表达式可以帮助我们更好的描述复制的文本格式,可以更好地利用它们对文本数据进行检索.替换.提取和修改操作. http://www.cnblogs.com/huxi/archive/2010/07/0 ...
- C#操作防火墙控制电脑某些软件联网
问题: 目前公司软件刚由单机软件更改为联网软件,许多客户反映希望能够有一个功能来控制电脑上某些必用软件,如qq,公司软件联网,而其他不必要的如网页,游戏等软件不允许联网,于是向公司反映希望可以有一个功 ...
- 记PHP面向对象编程
访问控制 public(公开的):可以在类中.子类中.类外访问. protected(受保护的):只能在类本身及子类中访问. private(私有的):只能在声明他们的类中进行访问,私有的类成员不能被 ...
- Openning
In order to imporve my english writing skill and enhance my understanding of programming ,I'm setti ...