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. canvas的默认尺寸

    canvas一直就是偶尔看看,随便画点小东西,没有认真琢磨过,今天打算认真的从头学一下,画线的时候感觉坐标不太正常,后来发现,canvas有自己的默认尺寸 写法如下 <canvas id=&qu ...

  2. .gitignore文件不起作用

    当我们用git时常常会习惯把我们不想上传到远程代码库中的一些本地文件(夹)放在一个叫做.gitignore的文件中,例如常见的本地build文件夹,一些IDE如Intellig,Eclipse的项目管 ...

  3. c语言中的指针问题

    “*”符号的作用在C语言中有两种: 1.声明该变量是指针,例如:int * p;//表示声明一个int类型的指针,变量名为p 2.在指针运算时,表示取这个地址上的内容,例如  temp = *p;// ...

  4. react-native win7环境搭建

    时间:2016-08-22 晚,西安 1.安装jdk java version "1.6.0_45"Java(TM) SE Runtime Environment (build 1 ...

  5. Day2(2016/1/22)——Testing

    Activity Button,Toast,Finish 显式intent 隐式明天再看…… 感觉要先补一补java……

  6. awk 合并文件

      问题描述:两个文件a.dat, b.dat a.dat 0    100 1    99 2    93 3    90 ... b.dat 0   0 1   3 2   0 3   2 ... ...

  7. c#中关键词out和ref的区别

    c#中关键词out和ref用来表明以传引用的方式传递参数. 区别如下: 如果方法的参数用out标记,表示方法被调用前不需初始化参数,方法内不能读取此参数的值,在方法返回前必须向此参数写入值: 如果方法 ...

  8. Java 使用jaxp修改节点

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <perso ...

  9. lua语言三则特性

    pack和unpack 对于一个函数, 要将其入参转换为一个表, 则pack函数合适. 对于一个表要将其转换为 一个函数的入参, 则 lua原生提供的 unpack函数可以实现. do arrayDa ...

  10. EXCEL导入导出自己整理的一些方法

    //导入Excel代码 protected DataTable ExcelHelper(string filePaht) { string sFilePath2003 = Server.MapPath ...