【组合数】hrbustoj 1744 Pascal's Triangle

Pascal's Triangle
Time Limit: 1000 MSMemory Limit: 65535 K
Total Submit: 115(23 users)Total Accepted: 27(16 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春季校赛 - 现场赛
SubmitStatisticDiscussSharedcodes

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

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

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define mod 20000003

const 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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值