【组合数】hrbustoj 1744 Pascal's Triangle

来源:互联网 发布:mac os 10.11 iso镜像 编辑:程序博客网 时间:2024/05/22 03:48

Pascal's TriangleTime Limit: 1000 MSMemory Limit: 65535 KTotal Submit: 115(23 users)Total Accepted: 27(16 users)Rating: Special Judge: NoDescription

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
22 33 4
Sample Output
620
Source哈理工2013春季校赛 - 现场赛SubmitStatisticDiscussSharedcodes

题意:看第二幅图片,求和:第m条对角线的前k个数的和;

思路:组合数取模(数据小),预处理阶乘与阶乘的逆元;

代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;#define mod 20000003const int maxn=1000005;ll fac[maxn+10],inv[maxn+10];ll mod_pow(ll x,ll n){    ll res=1;    while(n>0)    {        if(n&1) res=res*x%mod;        x=x*x%mod;        n>>=1;    }    return res%mod;}void init(){    fac[0]=1;fac[1]=1;    for(ll i=2;i<=maxn;i++)    {        fac[i]=fac[i-1]*i*1LL;        fac[i]%=mod;    }    inv[maxn]=mod_pow(fac[maxn],mod-2);    for(int i=maxn-1;i>=0;i--)        inv[i]=(inv[i+1]*(i+1))%mod;}ll C(ll n,ll m){    return fac[n]*inv[m]%mod*inv[n-m]%mod;}int main(){    int T;    init();    scanf("%d",&T);    while(T--)    {        ll m,k;        ll res=0;        scanf("%lld%lld",&m,&k);        for(ll i=m-1;i<m-1+k;i++)        {//            printf("%d ",C(i,m-1));            res=res+C(i,m-1);            res%=mod;        }        printf("%lld\n",res);    }    return 0;}





原创粉丝点击