您的位置:首页 > 其它

Hrbust 1744 Pascal's Triangle【数论求C(n,m)】

2017-03-19 17:29 288 查看
Pascal's Triangle
Time Limit: 1000 MSMemory Limit: 65535 K
Total Submit: 93(21 users)Total Accepted: 18(14 users)Rating: 







Special Judge: No
Description
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

Source
哈理工2013春季校赛 - 现场赛
题目大意:

让你在在第Mdiagonal分块中,求前K个数的和。

思路:

预处理C(n,m);

过程求和注意取模;

Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define LL long long int
const LL mod = 20000003;
const LL N = 300000+5;
const LL M = 3e5+3;
LL fac[1000005]; //阶乘
LL inv_of_fac[1000005]; //阶乘的逆元
LL qpow(LL x,LL n)
{
LL ret=1;
for(; n; n>>=1)
{
if(n&1) ret=ret*x%mod;
x=x*x%mod;
}
return ret;
}
void init()
{
fac[1]=1;
for(int i=2; i<=M; i++)
fac[i]=fac[i-1]*i%mod;
inv_of_fac[M]=qpow(fac[M],mod-2);
for(int i=M-1; i>=0; i--)
inv_of_fac[i]=inv_of_fac[i+1]*(i+1)%mod;
}
LL C(LL a,LL b)
{
if(b>a) return 0;
if(b==0) return 1;
return fac[a]*inv_of_fac[b]%mod*inv_of_fac[a-b]%mod;
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
LL n,m;
LL sum=0;
scanf("%lld%lld",&m,&n);
for(int i=0;i<n;i++)
{
sum+=C(m-1+i,m-1);
sum%=mod;
}
printf("%lld\n",sum);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hrbust 1744