【CodeForces】554C - CodeForces 554C(组合数学)
2016-04-22 08:56
393 查看
C. 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 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.
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 ci,
the number of balls of the i-th color (1 ≤ ci ≤ 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
output
input
output
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的时候,先在队尾放1~num[ 2 ] 个,然后前面的用了分苹果问题的组合公式
(传送门:http://blog.csdn.net/wyg1997/article/details/50810768)
这里的组合数很大啊,用暴力的方法投机取巧出来了,这个代码是可以被hack的。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
__int64 c[1011][1011];
__int64 MOD = 1e9 + 7;
void C()
{
for (int i = 1 ; i <= 1000 ; i++)
c[i][0] = c[i][i] = 1;
for (int i = 1 ; i <= 1000 ; i++)
{
for (int j = 1 ; j < i ; j++)
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % MOD;
}
}
__int64 CC(__int64 nn,__int64 mm)
{
if (nn<1000&&mm<1000)
return c[nn][mm];
if (mm<(nn/2))
return (CC(nn-1,mm)+CC(nn-1,mm-1))%MOD;
else
return (CC(nn-1,nn-1-mm)+CC(nn-1,nn-mm))%MOD;
}
int main()
{
int num[1011];
__int64 sum[1011];
int n;
__int64 dp[1011];
C();
while (~scanf ("%d",&n))
{
sum[0] = 0;
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d",&num[i]);
sum[i] = sum[i-1] + num[i];
}
dp[1] = 1;
for (int i = 2 ; i <= n ; i++)
{
dp[i] = 1;
__int64 m = sum[i-1];
for (int j = 1 ; j < num[i] ; j++)
{
dp[i]=(dp[i] + CC(j+m-1,j)) % MOD;
}
dp[i] = dp[i] * dp[i-1] % MOD;
}
printf ("%I64d\n",dp
);
}
return 0;
}
下面是宇神的AC代码(传送门:http://blog.csdn.net/chenzhenyu123456/article/details/51211622):
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <iostream>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
LL dp[1010];
int sum[1010], c[1010];
LL fac[2000000+10];
LL pow_mod(LL a, LL n) {
LL ans = 1LL;
while(n) {
if(n & 1LL) {
ans = ans * a % MOD;
}
a = a * a % MOD;
n >>= 1LL;
}
return ans;
}
LL C(int n, int m) {
return fac
* pow_mod(fac[m], MOD-2) % MOD * pow_mod(fac[n-m], MOD-2) % MOD;
}
void getfac() {
fac[0] = 1LL;
for(int i = 1; i <= 2000008; i++) {
fac[i] = fac[i-1] * i % MOD;
}
}
int main()
{
getfac(); int n;
while(scanf("%d", &n) != EOF) {
sum[0] = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
sum[i] = sum[i-1] + c[i];
}
dp[1] = 1LL;
for(int i = 2; i <= n; i++) {
dp[i] = dp[i-1];
for(int j = 1; j < c[i]; j++) {
add(dp[i], dp[i-1] * C(sum[i-1] + c[i] - j - 1, c[i] - j) % MOD);
}
//cout << dp[i] << endl;
}
printf("%lld\n", dp
);
}
return 0;
}
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 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.
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 ci,
the number of balls of the i-th color (1 ≤ ci ≤ 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
3 2 2 1
output
3
input
4
1
2
34
output
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 31 1 2 2 32 1 1 2 3
有了宇神的提示,勉勉强强做出来了(虽然完了宇神说数据太水,我们运气好通过的)
题解:先放1,再放2,依次往下。放1就一种方案,放2的时候,先在队尾放1~num[ 2 ] 个,然后前面的用了分苹果问题的组合公式
(传送门:http://blog.csdn.net/wyg1997/article/details/50810768)
这里的组合数很大啊,用暴力的方法投机取巧出来了,这个代码是可以被hack的。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
__int64 c[1011][1011];
__int64 MOD = 1e9 + 7;
void C()
{
for (int i = 1 ; i <= 1000 ; i++)
c[i][0] = c[i][i] = 1;
for (int i = 1 ; i <= 1000 ; i++)
{
for (int j = 1 ; j < i ; j++)
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % MOD;
}
}
__int64 CC(__int64 nn,__int64 mm)
{
if (nn<1000&&mm<1000)
return c[nn][mm];
if (mm<(nn/2))
return (CC(nn-1,mm)+CC(nn-1,mm-1))%MOD;
else
return (CC(nn-1,nn-1-mm)+CC(nn-1,nn-mm))%MOD;
}
int main()
{
int num[1011];
__int64 sum[1011];
int n;
__int64 dp[1011];
C();
while (~scanf ("%d",&n))
{
sum[0] = 0;
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d",&num[i]);
sum[i] = sum[i-1] + num[i];
}
dp[1] = 1;
for (int i = 2 ; i <= n ; i++)
{
dp[i] = 1;
__int64 m = sum[i-1];
for (int j = 1 ; j < num[i] ; j++)
{
dp[i]=(dp[i] + CC(j+m-1,j)) % MOD;
}
dp[i] = dp[i] * dp[i-1] % MOD;
}
printf ("%I64d\n",dp
);
}
return 0;
}
下面是宇神的AC代码(传送门:http://blog.csdn.net/chenzhenyu123456/article/details/51211622):
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <iostream>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
LL dp[1010];
int sum[1010], c[1010];
LL fac[2000000+10];
LL pow_mod(LL a, LL n) {
LL ans = 1LL;
while(n) {
if(n & 1LL) {
ans = ans * a % MOD;
}
a = a * a % MOD;
n >>= 1LL;
}
return ans;
}
LL C(int n, int m) {
return fac
* pow_mod(fac[m], MOD-2) % MOD * pow_mod(fac[n-m], MOD-2) % MOD;
}
void getfac() {
fac[0] = 1LL;
for(int i = 1; i <= 2000008; i++) {
fac[i] = fac[i-1] * i % MOD;
}
}
int main()
{
getfac(); int n;
while(scanf("%d", &n) != EOF) {
sum[0] = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
sum[i] = sum[i-1] + c[i];
}
dp[1] = 1LL;
for(int i = 2; i <= n; i++) {
dp[i] = dp[i-1];
for(int j = 1; j < c[i]; j++) {
add(dp[i], dp[i-1] * C(sum[i-1] + c[i] - j - 1, c[i] - j) % MOD);
}
//cout << dp[i] << endl;
}
printf("%lld\n", dp
);
}
return 0;
}
相关文章推荐
- Python 请用sorted对上述列表按名字和分数排序
- 4.22 站立会议(四)
- 百度编辑器Ueditor自动换行,添加<p>的问题
- 获取Access数据库中的用户表名
- Git版本控制使用方法入门教程
- CSS3魔法堂:说说Multi-column Layout
- Java 程序员们值得一看的好书推荐
- 团队项目个人工作总结(4月22日)
- 网页简介及个人理解
- mysql性能优化
- javascript计算两个时间差
- LLDB调试器的使用
- 连连看-BFS
- java中的String类常量池详解
- ubuntu 安装ncurses(转)
- 基于android的Socket通信
- struts2中的constant配置详解
- 笔试题33. LeetCode OJ (20)
- hibernate---一对一单向外键关联--annotation (重要!!!)
- Android:Activity的跳转