A. Kyoya and Colored Balls_排列组合,组合数
Codeforces Round #309 (Div. 1)
2 seconds
256 megabytes
standard input
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.
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
解题报告:
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_排列组合,组合数的更多相关文章
- 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 ...
- 排列组合+组合数取模 HDU 5894
// 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...
- 554C - Kyoya and Colored Balls
554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...
- Codeforces A. Kyoya and Colored Balls(分步组合)
题目描述: Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Kyoya and Colored Balls(组合数)
Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- HDU 1521 排列组合 指数型母函数
排列组合 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- UVaLive 7360 Run Step (排列组合,枚举)
题意:给定一个数 n ,表示一共有 n 步,然后你可以迈一步也可以迈两步,但是左腿和右腿的一步和两步数要一样,并且两步数不小于一步数,问你有多少种方式. 析:虽然是排列组合,但还是不会做.....水啊 ...
- 【CF521C】【排列组合】Pluses everywhere
Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote ...
随机推荐
- tp5分组查询
$data=DB::name('goods_common')->alias('a')->join('all580_goods_attractions w','a.common_id = w ...
- KS光盘制作 for rhel6.5 and rhel7.2
############################## RHEL6.5 KS光盘制作--1.复制光盘到本地mkdir -p /opt/rhel6mount /dev/cdrom /mediacp ...
- php数组·的方法3-数组和变量之间的转换
/* * 数组和变量之间的转换 * */ //extract() 使用数组定义一组变量 // 键名为变量名 键值为变量值(类似于js的解构赋值) // 返回值是数组的长度 echo '<hr&g ...
- 更新jdk
1.从官网下载jdk-8u191-linux-x64.tar.gz,然后放在ubuntu里的Downloads文件夹里.然后解压到/usr/lib/jvm文件夹中. sudo tar -zxvf Do ...
- io基础(字节流、字符流、转换流、缓冲字符流)
首先需要明确的一点是输入流输出流的输入输出是站在内存的角度看的,读取文件,把文件内容写到内存中,是输入流:写文件,把内存中的数据写到文件中,是输出流. IO操作主要有4个抽象类: 字节输入输出流:In ...
- C++中遇到的各种小问题
lpcwstr类型问题 在使用VS2010开发C++程序时,由于系统默认字符集是unicode字符集,造成与早期的字符串格式不兼容问题 ①Properties — Configuration Prop ...
- javascript基础语法备忘录-变量和数据类型
//javascript基础语法备忘录-变量和数据类型 // 定义变量使用var关键字 后面跟变量名,不要使用eval 和arguments为变量名 var message = "hi&qu ...
- 用spring的 InitializingBean 的 afterPropertiesSet 来初始化
void afterPropertiesSet() throws Exception; 这个方法将在所有的属性被初始化后调用. 但是会在init前调用. 但是主要的是如果是延迟加载的话,则马上执行. ...
- 35、XPath的使用示例
使用Xpath获取页面元素 [参见W3C官网说明] http://www.w3school.com.cn/xpath/xpath_syntax.asp 以下Xpath路径都是获取下面地址的元素 ...
- (初学)wpf仿QQ界面-整体布局
跟一个小学弟一起学习wpf,小学弟是刚初中毕业,对编程刚刚接触,我挺怕自己带的不好,影响小学弟以后在编程方向的学习兴趣.我承认自己水平不高,但是在努力去学习新知识!一起加油吧!在此以博客,记录学习进度 ...