Codeforces Round #309 (Div. 2) C 排列组合+费马小
来源:互联网 发布:淘宝客服图标素材 编辑:程序博客网 时间:2024/06/06 01:37
链接:戳这里
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 1000000007using 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;}
0 0
- Codeforces Round #309 (Div. 2) C 排列组合+费马小
- Codeforces Round #345 (Div. 2)(C)排列组合,思维
- Codeforces Round #309 (Div. 2) C
- Codeforces Round #309 (Div. 2)C
- [排列组合]Codeforces Round #324 (Div. 2)E - Anton and Ira
- Codeforces Round #273 (Div. 2)(B)排列组合
- 【codeforces】Codeforces Round #370 (Div. 2) C
- Codeforces Round #324 (Div. 2) (B排列组合)(C贪心)(D哥德巴赫猜想 数论+暴力)
- Codeforces Round #309 (Div. 2)
- C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
- Codeforces Round #105 (Div. 2) C
- Codeforces Round 134 div 2 C题
- Codeforces Round #137 (Div. 2), problem: (C)
- Codeforces Round #153 (Div. 2) C题
- Codeforces Round #158 (Div. 2) C题
- Codeforces Round #162 (Div. 2) C
- 已知点的经纬度 与另一个点的距离 以及方向角 求另一点的经纬度
- 递归和迭代(学习笔记)
- 机器学习中特征选择和特征提取区别
- 丑数(Ugly Number)
- jquery.validate.js 在ie中丢失焦点的时候不触发验证
- Codeforces Round #309 (Div. 2) C 排列组合+费马小
- 真机测试的时候出现 data parameter is nil
- 人月神话读书笔记(10)----提纲挈领
- 开源中国源码解析之Splash页面
- 理解RESTful架构
- C++STL之string详讲
- 百度地图导航功能 希望对大家有用吧
- Robotium
- Linux 使用 ip route , ip rule , iptables 配置策略路由