light oj 1102 Problem Makes Problem [组合数+逆元]

Description

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 15solutions. 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

4

4 3

3 5

1000 3

1000 5

Sample Output

Case 1: 15

Case 2: 35

Case 3: 501501

Case 4: 84793457


今天终于懂了乘法逆元是用来干嘛的了, 对于加减乘法而言,过程取模是无所谓的, 但是当涉及除法的时候,就无法正常的取模,所以需要换一种思路去解决;

就好比 a/b = a * b^-1 一样, 即乘倒数, 只不过这个倒数是在取模后形成的 所以是乘法逆元;


题目大意:给你一个数字n,代表这个数为n ;给你一个k,代表拆分数的个数, 求所有的可能性;

题解:先把过程看做  x1 + x2 + x3 +...+xk = n ;  这样就变成了挡板问题 ,即:把n个物品放在m个盒子里面的方案数


如上图一般,实质就是求解 C(n+k-1,k-1)的方案数, 约定f(a)代表a的阶乘,对于组合数 C(m,n) =  f(m)/(f(n)*f(m-n));

由于避免除法,所以分别求 1/f(n) 和  1/f(m-n)的逆元, 假设 求 1/A的逆元, 由于mod是质数 。 所以由费马小定理得

A^(p-1)%p = 1 % p      --------->  两边同乘 A^-1 ----------->A^(p-2) %p = A^-1%p ; 即可得逆元 , 随后快速幂处理逆元即可

AC代码:

#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;
ll a[2000000];
void C()
{
    memset(a,0,sizeof(a));
    a[0] = a[1] = 1 ;
    for(int i = 2 ; i <=2000000;i++)
        a[i] = a[i-1]*i%mod;
}
ll quick(ll a , ll b)
{
    ll res = 1 ;
    while(b)
    {
        if(b&1) res = res *  a %mod ;
        b>>=1;
        a = a * a %mod ;
    }
    return res ;
}
int main()
{
    int t ;
    cin>>t;
    C();
    for(int cas = 1 ; cas<=t;cas++)
    {
        ll n , k ;
        cin>>n>>k;
        ll c = a[k-1];
        ll d = a[n];
        printf("Case %d: ",cas);
        cout<<a[n+k-1]*quick(c,mod-2)%mod*quick(d,mod-2)%mod<<endl;
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kelisita

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值