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 ...
随机推荐
- day 007 深浅拷贝
今日内容: 1.字符串操作补充: join # 遍历列表 例: lst = ['汪峰','吴君如','章子怡'] s = '*-/@'.join(lst) prints(s) 结果为汪峰*-/@吴君如 ...
- Java对象排序两种方法
转载:https://blog.csdn.net/wangtaocsdn/article/details/71500500 有时候需要对对象列表或数组进行排序,下面提供两种简单方式: 方法一:将要排序 ...
- C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
由于最近经常使用到c语言中的读写文件,所以在此总结以下,方便以后查找. 在c中,文件操作都是由库函数来实现的,主要是分为读和写两种操作,以下详细讲解以下所有有关文件操作的邯郸乎的用法: //C++写入 ...
- robotframe 学习笔记1
在robot framework中,通过 Set variable关键字来定义变量 连接对象: 通过Catenate关键字可以连接多个信息 加上"SEPARATOR=",可以对多个 ...
- centos7安装并配置postgresql
安装并配置postgresql,参考以下两篇文章即可 https://www.postgresql.org/download/linux/redhat/ http://www.jianshu.com/ ...
- [转]Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC - Part 4
本文转自:http://www.asp.net/mvc/overview/older-versions/using-the-html5-and-jquery-ui-datepicker-popup-c ...
- (转)DNS解析过程详解
DNS解析过程详解 原文:http://blog.csdn.net/crazw/article/details/8986504 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的 ...
- JavaScript运算符优先级——"++,--,&&,||“
上篇文章比较了"?,="三者的优先级:"?">"=">"," 今天继续学习"++,--,& ...
- lftp 快速使用
登录 lftp username:password@ip:port 设置字符集 set ftp:charset 'gbk' set ftp:charset 'utf-8' 下载文件 mget *.tx ...
- NLog学习笔记二:深入学习
配置文件 NLog所有的配置信息都可以写到一个单独的xml文件中,也可以在程序代码中进行配置. 配置文件位置 启动的时候,NLog会试图查找配置文件完成自动配置,查找的文件依次如下(找到配置信息则结束 ...