lightoj1002隔板法加卢卡斯定理

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

4

4 3

3 5

1000 3

1000 5

Sample Output

Case 1: 15

Case 2: 35

Case 3: 501501

Case 4: 84793457

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int t;
#define mod 1000000007
#define maxn 2000005
#define ll long long
ll n,k;
ll fac[maxn];
void init()
{fac[0]=1;
    for(ll i=1;i<maxn;i++)
        fac[i]=i*fac[i-1]%mod;
}
ll multi(ll a,ll b,ll m)
{
    ll ans=0;
    a%=m;
    while(b)
    {
        if(b&1)
        {
            ans=(ans+a)%m;
            b--;
        }
        b>>=1;
        a=(a+a)%m;
    }
    return ans;
}
ll quick_mod(ll a,ll b,ll m)
{
    ll ans=1;
    a%=m;
    while(b)
    {
        if(b&1)
        {
            ans=multi(ans,a,m);
            b--;
        }
        b>>=1;
        a=multi(a,a,m);
    }
    return ans;
}
ll getc(ll n,ll m)
{
    if(n<m)
        return 0;
    if(m>n-m)
        m=n-m;
    ll s1=1,s2=1;

    return fac[n]*quick_mod(fac[m]*fac[n-m],mod-2,mod);
}
ll lucas(ll n,ll m)
{
    if(m==0)
        return 1;
    return getc(n%mod,m%mod)*lucas(n/mod,m/mod)%mod;

}
int main()
{scanf("%d",&t);
int w=0;
init();
while(t--)
{w++;

    scanf("%lld%lld",&n,&k);
    printf("Case %d: %lld\n",w,lucas(n+k-1,k-1));
}

}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/88761188