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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 绿森商城不退款怎么办 电信手机号码过户积分清零怎么办 被电话诈骗骗了怎么办 诈骗电话骗了钱怎么办 被网友骗了一千块钱怎么办 被网友骗了1000块怎么办 微信红包被骗100怎么办 3m投资钱要不回来怎么办 手机照片超过3m怎么办 小说大于3m看不了怎么办 携程订单删除了怎么办 绿叶会员密码忘了怎么办 账号对名错了怎么办 lv双肩包肩带短了怎么办 微信充q币被骗了怎么办 qq隐私密码忘了怎么办 qq锁屏密码忘了怎么办 qq手势密码忘记了怎么办 qq红包的密码忘了怎么办 qq密码被改了怎么办啊 手机qq密码忘了怎么办 腾讯安全中心冻结解冻进不去怎么办 qq钱包充值限额怎么办 qq没绑卡支付密码忘了怎么办 手机qq停止运行该怎么办 手机不支持qq软件运行怎么办 王者传奇冲元宝不到账怎么办 支付宝充值地下城点卷冲错了怎么办 dnf点券冲错了怎么办 百家号改了手机绑定找不到了怎么办 银行卡换了网银怎么办 qq没有银行卡转不出钱怎么办 qq钱包限制一万怎么办 qq余额超过20万怎么办 扣扣忘记了密码怎么办 qq钱包发不出来怎么办 qq支付密码忘了怎么办? 扣扣上转账错了怎么办 qq绑卡存在异常怎么办 微信没绑银行卡忘记支付密码怎么办 微信的自动扣费怎么办