码迷,mamicode.com
首页 > 其他好文 > 详细

HLG1744组合数学问题与lucas定理运用

时间:2015-04-08 21:16:45      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

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

HLG1744组合数学问题与lucas定理运用

标签:

原文地址:http://www.cnblogs.com/13224ACMer/p/4403536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!