Lucas' theorem

In number theory, Lucas's theorem expresses the remainder of division of the binomial coefficient by a prime number p in terms of the base p expansions of the integers m and n.     ----wiki



##表达式

对于非负整数mn和素数p,如果有:

![](http://7xrn7f.com1.z0.glb.clouddn.com/16-3-11/46939983.jpg)

则有下式成立:

![](http://7xrn7f.com1.z0.glb.clouddn.com/16-3-11/30039157.jpg)



##牛刀小试
题目链接:[hdu-3037](http://acm.hdu.edu.cn/showproblem.php?pid=3037)
题目大意:求在n棵树上摘不超过m颗豆子的方案,结果对p取模。
解题思路:
首先,n棵树上摘m课豆子的方案数相当于从n个数中可重复的选m个数的组合数,为。那么现在就是求


代码:
```c++
#include
#include
#include
using namespace std;
typedef long long LL;
const int N = 100001;
LL mod;
LL jc[N];

LL quick(LL a, LL b)

{

LL c = 1;

while(b)

{

if(b&1) c = c * a % mod;

b >>= 1;

a = a * a % mod;

}

return c;

}

LL NY(LL a)

{

return quick(a, mod-2);

}

void init()

{

jc[0] = 1;

for(LL i=1; i<mod; i++)

{

jc[i] = i * jc[i-1] % mod;

}

}

LL C(LL n, LL m)

{

if(n < m) return 0;

return jc[n] % mod * NY(jc[m]) % mod * NY(jc[n-m]) % mod;

}

LL Lucas(LL n, LL m)

{

if(!m) return 1;

return Lucas(n/mod, m/mod) * C(n%mod, m%mod) % mod;

}

int main()

{

LL n, m;

int t;

scanf("%d", &t);

while(t--)

{

scanf("%I64d %I64d %I64d", &n, &m, &mod);

init();

printf("%I64d\n", Lucas(n+m, m));

}

return 0;

}

Lucas定理的更多相关文章

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

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

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

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

  3. 大组合数:Lucas定理

    最近碰到一题,问你求mod (p1*p2*p3*……*pl) ,其中n和m数据范围是1~1e18 , l ≤10 , pi ≤ 1e5为不同的质数,并保证M=p1*p2*p3*……*pl ≤ 1e18 ...

  4. 【BZOJ-4591】超能粒子炮·改 数论 + 组合数 + Lucas定理

    4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 95  Solved: 33[Submit][Statu ...

  5. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  6. hdu 3037 Saving Beans Lucas定理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. 【BZOJ1951】【SDOI2010】古代猪文 Lucas定理、中国剩余定理、exgcd、费马小定理

    Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...

  8. 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix

    Tom and matrix Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...

  9. HDU 4349 Xiao Ming's Hope lucas定理

    Xiao Ming's Hope Time Limit:1000MS     Memory Limit:32768KB  Description Xiao Ming likes counting nu ...

  10. HDU3037 Saving Beans(Lucas定理+乘法逆元)

    题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...

随机推荐

  1. Apache Spark源码走读之23 -- Spark MLLib中拟牛顿法L-BFGS的源码实现

    欢迎转载,转载请注明出处,徽沪一郎. 概要 本文就拟牛顿法L-BFGS的由来做一个简要的回顾,然后就其在spark mllib中的实现进行源码走读. 拟牛顿法 数学原理 代码实现 L-BFGS算法中使 ...

  2. The index also can be used for LIKE comparisons if the argument to LIKE is a constant string that does not start with a wildcard character.

    http://dev.mysql.com/doc/refman/5.7/en/index-btree-hash.html MySQL 5.7 Reference Manual  /  ...  /   ...

  3. js获取url中参数

      /** * 获取地址栏参数值 * @param name 参数名 * @returns */ $(function () { var url = location.search; //获取url中 ...

  4. 对list进行切片

    取一个list的部分元素是非常常见的操作.比如,一个list如下: >>> L = ['Adam', 'Lisa', 'Bart', 'Paul'] 取前3个元素,应该怎么做? 笨办 ...

  5. C# Lock 解读[转]

    一.Lock定义     lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.它可以把一段代码定义为互斥段(critical section),互斥段在一个时刻内只允许一个线程进入执行, ...

  6. DevExpress中的ASPxTreeView 递归显示checknodes并获得选中值

    aspx代码 <dx:ASPxTreeView ID="ASPxTreeView1" runat="server"> </dx:ASPxTre ...

  7. Coding过程中遇到的一些bug

    1. 在使用layoutSubviews方法调整自定义view内部的子控件坐标时,最好不要使用子控件的centerX,centerY属性,否则会出现奇怪的bug. 如果一定要用,务必仔细检查,该子控件 ...

  8. js 给样式添加随机颜色

    下面提供了三种获取随机颜色值的方法 方法一: 创建一个颜色 HEX 值数组,然后随机抽取这个数组里6个值,组合生成颜色. function color1(){ var color = "&q ...

  9. 转: 带你玩转Visual Studio——带你理解多字节编码与Unicode码

    上一篇文章带你玩转Visual Studio——带你跳出坑爹的Runtime Library坑帮我们理解了Windows中的各种类型C/C++运行时库及它的来龙去脉,这是C++开发中特别容易误入歧途的 ...

  10. Coursera台大机器学习课程笔记3 – 机器学习的可能性

    提纲: 机器学习为什么可能? 引入计算橙球概率问题 通过用Hoeffding's inequality解决上面的问题,并得出PAC的概念,证明采样数据学习到的h的错误率可以和全局一致是PAC的 将得到 ...