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 ...
随机推荐
- 由DataGridTextColumn不能获取到父级DataContext引发的思考
在项目中使用DataGrid需要根据业务动态隐藏某些列,思路都是给DataGrid中列的Visibility属性绑定值来实现(项目使用MVVM),如下 <DataGridTextColumn H ...
- TRUNC函数,ORA-01898 精度说明符过多
TRUNC(SYSDATE)即可默认当前日期(年月日),TRUNC(SYSDATE,'yyyy-mm-dd'),精度说明符过多
- winform之权限判断小技巧
每个页面都要判断用户是否登陆并且判断用户是否拥有相应的权限,,以至于每个页面都要判断Session["user"]是否为空,后期不好维护 小技巧: 因为每个页面都继承与Page类, ...
- .NET Core 在Visual Studio 2015 下的使用-MSDN
.NET Core RC2 现已推出,这是真正的"候选发布"而非 RC1 Beta 冒充的候选发布(如果是那样,请考虑发布后出现的所有更改).当前,围绕 .NET Core 的开发 ...
- 教你一步一步实现一个Promise
Promise我想现在大家都非常熟悉了,主要作用就是解决异步回调问题,这里简单介绍下. Promise规范是CommonJS规范之一,而Promise规范又分了好多种,比如 Promises/A.Pr ...
- Gallery 图片画廊
Gallery 图片画廊 学习网址http://amazeui.org/widgets/gallery?_ver=2.x
- 【HDU 2955】Robberies(DP)
题意是给你抢劫每个银行可获得的钱m和被抓的概率p,求被抓的概率小于P,最多能抢多少钱.01背包问题,体积是m,价值是p.被抓的概率不是简单相加,而应该是1−Π(1−p[i])DP:dp[i]表示抢到i ...
- python逐行读写
代码: fileReadObj = open("input.txt") fileWriteObj = open("output.txt", 'w') fileL ...
- request.getAttribute() 和 request.getParameter() 有何区别?
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别: (1)HttpServletRequest类有setAttri ...
- 搭建一个springMVC项目以及遇到的问题
首先找到jar包(lz现在还在学习maven,以后回了,就用maven了,自己配置时,jar包不全就很容易到时搭建失败)