HLG1744组合数学问题与lucas定理运用
|
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 |
| Sample Output |
6 |
思路公式,否则超时
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定理运用的更多相关文章
- Bzoj 4403: 序列统计 Lucas定理,组合数学,数论
4403: 序列统计 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 328 Solved: 162[Submit][Status][Discuss] ...
- 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 < ...
- lucas定理和组合数学
自湖南长沙培训以来的坑...一直未填,今天把这个问题解决掉. 参考: 1.http://www.cnblogs.com/Var123/p/5523068.html 2.http://blog.csdn ...
- Lucas定理及应用
额,前两天刚讲了数据结构,今天我来讲讲组合数学中的一种奇妙优化——Lucas 先看这样一个东西 没学过lucas的肯定会说:还不简单?处理逆元,边乘边膜呗 是,可以,但注意一下数据范围 你算这一次,你 ...
- [学习笔记]扩展LUCAS定理
可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...
- hdu 3037 费马小定理+逆元除法取模+Lucas定理
组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...
- Lucas定理初探
1.1 问题引入 已知\(p\)是一质数,求\(\dbinom{n}{m}\pmod{p}\). 关于组合数,它和排列数都是组合数学中的重要概念.这里会张贴有关这两个数的部分内容. 由于Lucas定理 ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
随机推荐
- [Asp.net]c#中的斜杠和反斜杠
引言 在外地出差,给客户部署项目,三家做的项目要在一起集成,这就造成数据格式不同,路径中的斜杠和反斜杠造成了很大的问题. 查了一下这方面的资料,这里做一些记录,算是一个小结吧. 正斜杠(/)与反斜杠( ...
- 史上最全的javascript知识点总结,浅显易懂。
来源于:http://blog.csdn.net/qiushi_1990/article/details/40260471 一,认识javascript1-1为什么学习JavaScript一).你知道 ...
- jstl标签用法
bean的uri的路径 bean标签是属于struts中的标签,使用要在 Struts 1.3 Libraries中 struts-taglib-1.3.8.jar 中META-INFtld ...
- Java设计模式-适配器模式(Adapter)
适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题.主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式.首先,我们来看看类的适配器模 ...
- selenium ide 录制回放link链接报错
回放是出现以下错误: 也就是回放点击打开新的链接时出现这个错误, 这个问题说的是 点击此链接会新打开一个窗口 selenium1是不支持多窗口切换的 因此会卡在这里,也就录制不支持这个操作,但是很多书 ...
- Java异常分类
一.基本概念 看java的异常结构图 Throwable是所有异常的根,java.lang.ThrowableError是错误,java.lang.ErrorException是异常,java.lan ...
- 新建的 web 工程 有红色的惊叹号
新建的 web 工程 有红色的感叹号问题: 在eclipse 中新建一个web工程,但是工程上有红色的感叹号.解决: 1.右键工程,选择Build Path-->Configur ...
- Git删除文件操作
使用Git删除文件需要使用Git rm命令来实现,最后git commit 需要注意的是直接rm命令删除后是不可以的,可以用git status 命令尝试一下,效果如图下(创建了test文件,演示了g ...
- jquery------.resizable()的使用
index.jsp //加上这两行代码,右下角会有样式效果<link rel="stylesheet" href="//code.jquery.com/ui/1.1 ...
- Laravel教程 五:MVC的基本流程
Laravel教程 五:MVC的基本流程 此文章为原创文章,未经同意,禁止转载. Controller 期间受到很多私事影响,终于还是要好好写写laravel的教程了. 上一篇我们说了数据库和Eloq ...

