Codeforces Round #309 (Div. 2) C 排列组合+费马小
2016-07-18 12:08
344 查看
链接:戳这里
C. Kyoya and Colored Balls
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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
3
4
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 3
1 1 2 2 3
2 1 1 2 3
题意:
箱子里面装了n个球,总共k个颜色。现在从里面取出球。当取出最后一个颜色为i的球,一定在最后一个颜色为i+1的球之前取出。也就是说最后一个颜色为i+1的球在最后一个i球之后取出
问颜色取出的排列有多少种
思路:
从后往前取,这样可以确保至少有一个i+1的球在i之后
当前取得球颜色是i,还剩下n个位置没有放球,那么先拿一个颜色为i的球放在当前所有能放的位置的最后面
剩下的num[i]-1个球去放剩下的n-1个位置,直到不能放为止,所以每次的i贡献是C(n-1,a[i]-1)
取模用下费马小
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
#define mod 1000000007
using namespace std;
ll fac[1000100];
ll ppow(ll k,ll n) {
ll c=1;
while(n) {
if(n%2) c=(c*k)%mod;
k=(k*k)%mod;
n>>=1;
}
return c;
}
ll C(ll a,ll b) {
return (((fac[b]*ppow((fac[a]*fac[b-a])%mod,mod-2)))%mod)%mod;
}
int n;
int a[10010];
int main(){
fac[0]=1;
for(int i=1;i<=200000;i++) fac[i]=(fac[i-1]*i)%mod;
int sum=0;
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
sum+=a[i];
}
ll ans=1;
for(int i=n;i>=1;i--){
ans=ans*C(a[i]-1,sum-1)%mod;
sum-=a[i];
}
printf("%I64d\n",ans);
return 0;
}
C. Kyoya and Colored Balls
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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
3
4
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 3
1 1 2 2 3
2 1 1 2 3
题意:
箱子里面装了n个球,总共k个颜色。现在从里面取出球。当取出最后一个颜色为i的球,一定在最后一个颜色为i+1的球之前取出。也就是说最后一个颜色为i+1的球在最后一个i球之后取出
问颜色取出的排列有多少种
思路:
从后往前取,这样可以确保至少有一个i+1的球在i之后
当前取得球颜色是i,还剩下n个位置没有放球,那么先拿一个颜色为i的球放在当前所有能放的位置的最后面
剩下的num[i]-1个球去放剩下的n-1个位置,直到不能放为止,所以每次的i贡献是C(n-1,a[i]-1)
取模用下费马小
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
#define mod 1000000007
using namespace std;
ll fac[1000100];
ll ppow(ll k,ll n) {
ll c=1;
while(n) {
if(n%2) c=(c*k)%mod;
k=(k*k)%mod;
n>>=1;
}
return c;
}
ll C(ll a,ll b) {
return (((fac[b]*ppow((fac[a]*fac[b-a])%mod,mod-2)))%mod)%mod;
}
int n;
int a[10010];
int main(){
fac[0]=1;
for(int i=1;i<=200000;i++) fac[i]=(fac[i-1]*i)%mod;
int sum=0;
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
sum+=a[i];
}
ll ans=1;
for(int i=n;i>=1;i--){
ans=ans*C(a[i]-1,sum-1)%mod;
sum-=a[i];
}
printf("%I64d\n",ans);
return 0;
}
相关文章推荐
- 虚拟机,开发板,主机 相互ping通
- NYOJ35表达式求值
- NGUI拖动摄像机实现面板上下拖动
- NODE.JS的简单的例子
- 入门级大数据解决方案
- eclipse+padWalker+perl 环境搭建
- 通过继承实现多态(小学生日常)
- 【杭电】[1865]1sting
- Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable to create native thread)
- codeforces 696C PLEASE 概率dp+公式递推+费马小定理
- Android中的基础----android:gravity和android:layout_gravity的使用原则和区别
- GP学习(七)—Accessing raster workspaces
- 通达OA列表控件添加js代码,实现js的获取和赋值
- Android AllView使用效果
- i3 i5 i7的区别
- Android Studio修改项目编译版本
- BestCoder 2nd Anniversary/HDU 5719 姿势
- 真机测试的时候出现 data parameter is nil
- .NET类库Newtonsoft.Json的各json与对之应的Model记录
- [从头读历史] 第307节 星球战争 BC2699 至 BC2600(公元前27世纪)