lightoj 1102 - Problem Makes Problem(组合数+逆元)
来源:互联网 发布:神曲 天堂 知乎 编辑:程序博客网 时间:2024/05/24 23:16
As I am fond of making easier problems, I discovered a problem. Actually, the problem is 'how can you make n by adding k non-negative integers?' I think a small example will make things clear. Suppose n=4 and k=3. There are 15 solutions. They are
1. 0 0 4
2. 0 1 3
3. 0 2 2
4. 0 3 1
5. 0 4 0
6. 1 0 3
7. 1 1 2
8. 1 2 1
9. 1 3 0
10. 2 0 2
11. 2 1 1
12. 2 2 0
13. 3 0 1
14. 3 1 0
15. 4 0 0
As I have already told you that I use to make problems easier, so, you don't have to find the actual result. You should report the result modulo 1000,000,007.
Input
Input starts with an integer T (≤ 25000), denoting the number of test cases.
Each case contains two integer n (0 ≤ n ≤ 106) and k (1 ≤ k ≤ 106).
Output
For each case, print the case number and the result modulo 1000000007.
Sample Input
Output for Sample Input
4
4 3
3 5
1000 3
1000 5
Case 1: 15
Case 2: 35
Case 3: 501501
Case 4: 84793457
题意是给你一个数n,让你把它分成非负的k份。
就是方程n=x1+x2+x3+...+xk非负解的个数。可以看成把n+k分成k份,然后分出来的k个每个再减1,这样就可以用隔板法用k-1个板隔成k份,就是C(n+k-1,k-1)。
接下来主要就是求组合数了。C(a,b)% p = f[a]/(f[a-b]*f[b]) % p(f[]是阶乘)
因为除法不能取模,所以要利用乘法逆元。
这里可以直接求乘法逆元(用拓展欧几里得)
也可以用费马小定理:ap-1==1mod(p),所以逆元a-1==ap-2mod(p),然后用快速幂求出来就可以了。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define LL long long#define mod 1000000007using namespace std;LL f[2000010];void init(){ f[0] = 1; for(int i=1;i<=2000000;i++) f[i] = f[i-1]*i%mod;}LL quickpow(LL x,int n){ LL res = 1; while(n > 0) { if(n&1) res = res*x%mod; x = x*x%mod; n >>= 1; } return res;}int main(void){ int T,n,k; init(); scanf("%d",&T); int cas = 1; while(T--) { scanf("%d%d",&n,&k); LL ans = quickpow(f[n]*f[k-1]%mod,mod-2); ans = ans*f[n+k-1]%mod; printf("Case %d: %lld\n",cas++,ans); } return 0;}
- lightoj 1102 - Problem Makes Problem(组合数+逆元)
- lightoj 1102 - Problem Makes Problem (组合+逆元)
- light oj 1102 Problem Makes Problem [组合数+逆元]
- LightOJ 1102 Problem Makes Problem(组合数学)
- LightOJ - 1102 Problem Makes Problem
- Light OJ 1102 Problem Makes Problem 组合数
- lightoj 1067 - Combinations (组合数、乘法逆元)
- 【noip 2016】 组合数问题(problem)
- 题(problem) (组合求方案数)
- LightOJ-1102-组合数学,逆元
- NOIP2016Day2T1组合数问题(problem)
- Noip2016day2 组合数问题problem
- CF689E:Mike and Geometry Problem(组合数)
- 组合Problem
- FZU1753 Another Easy Problem【组合数】
- codeforces contest 869 problem C(组合数)
- Codeforces Problem 711E ZS and The Birthday Paradox(抽屉原理+乘法逆元+费马小定理+组合数+快速幂+概率论)
- hdu4869(逆元+求组合数)
- MFC中,弹窗选择确定还是取消
- pycharm 远程调试openstack 代码
- Java Web Start
- CF607B: Zuma(区间dp)
- python pyqt5安装失败问题
- lightoj 1102 - Problem Makes Problem(组合数+逆元)
- Keras使用过程问题汇总
- 数据库事务隔离级别--脏读,幻读,不可重复读
- Java web项目 在线网络考试修改考试问题部分代码
- css框模型
- Extjs 限制 textfield / textarea 输入字数 , 兼容 IE Firefox Chrome
- 递归之无限极评论
- 微信小程序开发资料、工具、教程、源码(全套免费下载)
- google chrome的一个计算器扩展