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. Javascript基础系列之(六)循环语句(do while循环)

    do/while 循环是 while 循环的变体.该循环会执行一次代码块,在检查条件是否为真之前,然后如果条件为真的话,就会重复这个循环. 语法结构如下 do { statement } while ...

  2. centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记

    centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记 目录[-] 过程 1.安装RVM 2.利用rvm安装 Ruby 1.9.3 并设为默认 3.安装rails 4.安装 ...

  3. 『jQuery』.html(),.text()和.val()的概述及使用

    转自http://www.jb51.net/article/35867.htm 如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文 ...

  4. Mysql常出现的问题

    1.mysql如何导入.txt文件?load data local infile 'D:\\data.txt' into table 表名 fields terminated by '\t';2.my ...

  5. codeforces 719B:Anatoly and Cockroaches

    Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...

  6. Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server

    目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...

  7. CVE-2014-4877 && wget: FTP Symlink Arbitrary Filesystem Access

    目录 . 漏洞基本描述 . 漏洞带来的影响 . 漏洞攻击场景重现 . 漏洞的利用场景 . 漏洞原理分析 . 漏洞修复方案 . 攻防思考 1. 漏洞基本描述 0x1: Wget简介 wget是一个从网络 ...

  8. windows进程/线程创建过程 --- windows操作系统学习

    有了之前的对进程和线程对象的学习的铺垫后,我们现在可以开始学习windows下的进程创建过程了,我将尝试着从源代码的层次来分析在windows下创建一个进程都要涉及到哪些步骤,都要涉及到哪些数据结构. ...

  9. Myeclipse连接mysql数据库

    第一步:下载mysql驱动 驱动地址:http://www.mysql.com/products/connector/ .选择JDBC Driver for MySQL (Connector/J). ...

  10. CentOS下yum安装VNCserver

    VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...