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定理运用的更多相关文章

  1. Bzoj 4403: 序列统计 Lucas定理,组合数学,数论

    4403: 序列统计 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 328  Solved: 162[Submit][Status][Discuss] ...

  2. HDU 5226 Tom and matrix(组合数学+Lucas定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5226 题意:给一个矩阵a,a[i][j] = C(i,j)(i>=j) or 0(i < ...

  3. lucas定理和组合数学

    自湖南长沙培训以来的坑...一直未填,今天把这个问题解决掉. 参考: 1.http://www.cnblogs.com/Var123/p/5523068.html 2.http://blog.csdn ...

  4. Lucas定理及应用

    额,前两天刚讲了数据结构,今天我来讲讲组合数学中的一种奇妙优化——Lucas 先看这样一个东西 没学过lucas的肯定会说:还不简单?处理逆元,边乘边膜呗 是,可以,但注意一下数据范围 你算这一次,你 ...

  5. [学习笔记]扩展LUCAS定理

    可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...

  6. hdu 3037 费马小定理+逆元除法取模+Lucas定理

    组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...

  7. Lucas定理初探

    1.1 问题引入 已知\(p\)是一质数,求\(\dbinom{n}{m}\pmod{p}\). 关于组合数,它和排列数都是组合数学中的重要概念.这里会张贴有关这两个数的部分内容. 由于Lucas定理 ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

随机推荐

  1. php中curl的详细解说

    cURL可以使用URL的语法模拟浏览器来传输数据, 因为它是模拟浏览器,因此它同样支持多种协议, FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE ...

  2. jQuery Mobile学习日记

    本次主讲人是王思伦啦啦啦~ 框架特性 jQuery Mobile 以“Write Less, Do More”作为目标,为所有的主流移动操作系统平台提供了高度统一的 UI 框架:jQuery 的移动框 ...

  3. java设计优化--观察者模式

    观察者模式介绍 观察者模式是一种非常有用的设计模式,在软件系统中,当一个对象的行为依赖于另一个对象的状态时,观察者模式就非常有用.如果不适用观察者模式,而实现类似的功能,可能就需要另外启动一个线程不停 ...

  4. Freemarker-数字默认格式化问题

    freemarker在解析数据格式的时候,默认将数字按3位来分割 例如1000被格式化为1,000 这样做看似美观,但在实际操作时候会带来问题.例如我一个页面有一个元素,该元素的值由后台绑定且超过10 ...

  5. spring - ioc和aop

    1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对象的时候,一般都是直接使用关键字类new ...

  6. 似然估计中为什么要取对数以GMM为例

    1.往往假设特征之间独立同分布,那么似然函数往往是连城形式,直接求骗到不好搞,根据log可以把连乘变为连加. 2.另外概率值是小数,多个小数相乘容易赵成浮点数下溢,去log变为连加可以避免这个问题. ...

  7. PHP中soap的使用例子

    PHP 使用soap有两种方式. 一.用wsdl文件 服务器端. <?phpclass service{ public function HelloWorld() { return " ...

  8. Maven学习笔记-02-Maven项目打包配置与测试

    一 Maven项目打包配置 1 为整个项目统一指定字符集 <properties> <project.build.sourceEncoding>UTF-</project ...

  9. 使用Jquery+EasyUI 进行框架项目开发案例讲解之四 组织机构管理源码分享

    http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章  <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...

  10. ecshop后台admin路径怎么修改

    ecshop后台admin路径怎么修改 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-03-25   ecshop如何修改后台admin路径? 大家都知道ec ...