The figure below shows Pascal‘s Triangle: Baby H divides Pascal‘s Triangle into some Diagonals, like the following figure: Baby H wants to know the sum of K number in front on the Mth diagonal. Try to calculate it. |
Input |
There are multiple test cases. The first line is a positive integer T (1<=T<=100) indicating the number of test cases. For each test case: Line 1. Two positive integers M and K (1<= M , K <= 100 000). |
Output |
For each test case, output the sum of K number in front on the Mth diagonal in one line. The answer should modulo to 20 000 003. |
Sample Input |
2 2 3 3 4 |
Sample Output |
6 20 |
思路公式,否则超时
C(N,M)+C(N+1,M)+C(N+2,M)==C(N+3,M+1)
代码#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL exp_mod(LL a, LL b, LL p)
{ LL res = 1;
while(b != 0)
{
if(b&1) res = (res * a) % p;
a = (a*a) % p;
b >>= 1;
}
return res;
}
LL Comb(LL a, LL b, LL p)
{
if(a < b) return 0;
if(a == b) return 1;
if(b > a - b) b = a - b; LL ans = 1, ca = 1, cb = 1;
for(LL i = 0; i < b; ++i)
{
ca = (ca * (a - i))%p;
cb = (cb * (b - i))%p;
}
ans = (ca*exp_mod(cb, p - 2, p)) % p;
return ans;
}
LL Lucas(int n, int m, int p)
{
LL ans = 1;
while(n&&m&&ans)
{
ans = (ans*Comb(n%p, m%p, p)) % p;
n /= p;
m /= p;
}
return ans;
}
int main()
{
int n, m, t;
scanf("%d",&t);
int p=20000003;
while(t--)
{
scanf("%d%d",&m,&n);
printf("%lld\n", Lucas(m+n-1, m, p));
}
return 0;
}