【CodeForces】554C - CodeForces 554C(组合数学)
来源:互联网 发布:吴鉴鹰单片机视频网盘 编辑:程序博客网 时间:2024/04/28 14:31
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.
3221
3
41234
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 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;elsereturn (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[n]);}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[n] * 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[n]); } return 0;}
- CodeForces 554C 组合数学
- 【CodeForces】554C - CodeForces 554C(组合数学)
- Codeforces--554C--Kyoya and Colored Balls(组合数学)
- CodeForces 57 C.Array(组合数学)
- codeforces 294C组合数学)
- Codeforces 554C Kyoya and Colored Balls 【dp + 组合数学】
- CodeForces - 554C Kyoya and Colored Balls (组合数学&逆元模板)
- Codeforces 300C Beautiful Numbers 【组合数学】
- CodeForces 629 C.Famil Door and Brackets(组合数学)
- Codeforces 869 C. The Intriguing Obsession (组合数学)
- Codeforces Round #439 C.The Intriguing Obsession(组合数学)
- CodeForces 28 C.Bath Queue(概率DP+组合数学)
- CodeForces 302C(数学)
- codeforces 550c (数学)
- codeforces 554C Kyoya and Colored Balls 【组合数学+费马小定理】
- codeforces(559C)--C. Gerald and Giant Chess(组合数学)
- codeforces 294C C. Shaass and Lights(组合数学)
- Codeforces 666C C. Codeword 组合数学+DP
- Android中 一些维持状态的经验
- iOS中如何在欢迎界面中动态加载广告
- leetcode 5. Longest Palindromic Substring
- 【Light】[1048]Conquering Keokradong
- Oracle 修改表结构
- 【CodeForces】554C - CodeForces 554C(组合数学)
- 【黑马Android】(06)使用HttpClient方式请求网络/网易新闻案例
- 项目中的一些函数总结
- 基于UITextView的UITextField功能效果(有占位符功能)
- Android Studio导入.so库文件方法
- 关于微信支付
- Python 请用sorted对上述列表按名字和分数排序
- %type和% rowtype 的区别
- Mybatis中Collection集合标签的使用