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. swift 2.x学习笔记(二)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0. ...

  2. php session详解

    <?php /* * session_abort — Discard session array changes and finish session 舍弃会话序列变化和结束会话 session ...

  3. Thinking in Java——笔记(9)

    Polymorphism Abstract classes and methods If you have an abstract class, objects of that specific cl ...

  4. Linux下安装vsftpd

    一.安装vsftpd及相关依赖包 #vsftpd安装程序 yum install vsftpd #vsftpd虚拟登陆账户必要依赖包 yum install pam* db4* 安装完之后,vsftp ...

  5. Maintaining Your Signing Identities and Certificates 维护你的签名标识和证书

    Code signing your app lets users trust that your app has been created by a source known to Apple and ...

  6. Python开发程序:FTP程序

    作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp se ...

  7. http-2.4.18 安装

    编译最新的httpd 2.4.18     编译提示apr版本低于1.4 rpm repo源是1.39的 因此编译apr 下载 apr apr-util  apr-iconv [root@ansibl ...

  8. 使用Karma 来进行 JavaScript 测试

    最近接触了一些新的前端开发知识,主要是利用AngularJS做single page application.我也借这个机会,花了几天时间了解了如何对javascript进行测试. 这里将介绍一些使用 ...

  9. 计划将项目中使用entity framework的要点记录到改栏目下

    ef监控sql执行性能日志.http://www.cnblogs.com/CreateMyself/p/5277681.html http://123.122.205.38/cn_sql_server ...

  10. JS中同名函数有效执行顺序

    html中如果出现函数同名时:如果有多个外部引入的js文件,例如a.js和b.js(引入顺序假定是a.js,然后是b.js),同时html中本身也有内部的js.那么针对 出现函数名一样的情况时,无论他 ...