0
点赞
收藏
分享

微信扫一扫

LightOJ 1102 Problem Makes Problem(卢卡斯+阶乘处理+隔板法)

西曲风 2023-02-07 阅读 70


Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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
 

题意 : 输入n,k,输出 将n分成k个非负整数之和的方案数。

分析:隔板法的经典应有,具体原理:隔板法详解:​​点这里​​

ans = C(n+k-1,k-1);

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
#define LL __int64
const int N=2000100;
const int p=1000000007;

LL fac[N];

void init() //预处理阶乘
{
LL i;
fac[0] =1;
for(i =1; i < N; i++)
fac[i] = fac[i-1]*i % p;
}

LL exp_mod(LL a, LL b)
{
LL tmp = a % p, ans =1;
while(b)
{
if(b & 1) ans = ans * tmp % p;
tmp = tmp*tmp % p;
b >>=1;
}
return ans;
}

LL C(LL n, LL m)
{
if(m > n)
return 0;
return fac[n]*exp_mod(fac[m]*fac[n-m], p-2) % p;//逆元
}

LL Lucas(LL n, LL m)
{
if(m ==0)
return 1;
return (C(n%p, m%p)*Lucas(n/p, m/p))%p;
}


int main()
{
int T;
LL n,m,cas=1;

init();

scanf("%d",&T);
while(T--)
{
scanf("%lld %lld",&n,&m);
printf("Case %lld: %lld\n",cas++,Lucas(n+m-1,m-1));
}
return 0;
}

 

举报

相关推荐

0 条评论