>On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pick mm different apples among nn of them and modulo it with MM. MM is the product of several different primes.

Input

On the first line there is an integer T(T≤20)T(T≤20) representing the number of test cases.

Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10)n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where kk is the number of primes. Following on the next line are kk different primes p1,...,pkp1,...,pk. It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤1018M=p1·p2···pk≤1018 and pi≤105pi≤105 for every i∈{1,...,k}i∈{1,...,k}.OutputFor each test case output the correct combination on a line.Sample Input

1
9 5 2
3 5

Sample Output

6

题意:

让你求出C(n,m)%M的值。

思路:

此题的 n和m非常大,因此不能用快速幂取模,这里我们只能用lucas定理,但lucas定理有一个条件,要求C(n,m)%M的M必须要为素数,因此,我们又要用到中国剩余定理。

经验:

  • 按照这样的方法,现在大的组合数都可以化小了。
  • 注意long long范围,超范围时用快速乘法的方法做乘,欧拉算法时里有用过。即代码里的mul()函数。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=;
LL fac[maxn],mod[maxn],odd[maxn],M,Mod;
void factorial()
{
fac[]=; for(int i=;i<=Mod;i++) fac[i]=fac[i-]*i%Mod;
}
LL f_pow(LL a,LL x)
{
LL res=; a%=Mod;
while(x){ if(x&) res=res*a%Mod;a=a*a%Mod; x>>=; }return res;
}
LL C(LL n,LL m)
{
if(m>n) return ; return fac[n]*f_pow(fac[m]*fac[n-m]%Mod,Mod-)%Mod;
}
LL Lucas(LL n,LL m)
{
if(m==) return ; return C(n%Mod,m%Mod)*Lucas(n/Mod,m/Mod)%Mod;
}
LL mul(LL x,LL y,LL p)
{
LL res=;
while(y){
if(y&) res=(res+x)%p;y>>=;x=(x+x)%p;
}return res%p;
}
void China(int k)
{
LL ans=;
for(int i=;i<=k;i++){
Mod=mod[i];
ans=ans+mul(mul(M/mod[i],f_pow(M/mod[i],mod[i]-),M),odd[i],M);
}printf("%lld\n",(ans+M)%M);
}
int main()
{
LL T,n,m,k;
scanf("%lld",&T);
while(T--){
M=;
scanf("%lld%lld%lld",&n,&m,&k);
for(int i=;i<=k;i++){
scanf("%d",&mod[i]);Mod=mod[i];M*=mod[i];
factorial();
odd[i]=Lucas(n,m)%Mod;
}
China(k);
}return ;
}

再总结一下剩余定理

设正整数两两互素,则同余方程组

有整数解。并且在模下的解是唯一的,解为

其中,而的逆元。

HDU5446 Unknown Treasure(组合数膜合数-->Lucas+中国剩余定理)的更多相关文章

  1. BZOJ-1951 古代猪文 (组合数取模Lucas+中国剩余定理+拓展欧几里得+快速幂)

    数论神题了吧算是 1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1573 Solved: 650 [Submit ...

  2. HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

    HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018 思路:这题基本上算是模版题了 ...

  3. Lucas+中国剩余定理 HDOJ 5446 Unknown Treasure

    题目传送门 题意:很裸,就是求C (n, m) % (p1 * p2 * p3 * .... * pk) 分析:首先n,m<= 1e18, 要用到Lucas定理求大组合数取模,当然p[]的乘积& ...

  4. HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...

  5. FJNU2018低程A 逃跑路线(Lucas + 中国剩余定理 + LGV定理)题解

    题目描述 n个人在w*h的监狱里面想要逃跑,已知他们的同伙在坐标(bi,h)接应他们,他们现在被关在(ai,1)现在他们必须要到同伙那里才有逃出去的机会,这n个人又很蠢只会从(x,y)->(x+ ...

  6. BZOJ 1951 [SDOI2010]古代猪文 (组合数学+欧拉降幂+中国剩余定理)

    题目大意:求$G^{\sum_{m|n} C_{n}^{m}}\;mod\;999911659\;$的值$(n,g<=10^{9})$ 并没有想到欧拉定理.. 999911659是一个质数,所以 ...

  7. Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)

    题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...

  8. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  9. Unknown Treasure(hdu5446)

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. 【转】AC神组合数取模大全

    貌似少了几张图片,不过没有图片也没什么关系的感觉. 最后的究极篇也想出来了,但是貌似找不到题目,好尴尬.. 这个表示的是从n个元素中选取m个元素的方案数. (PS.组合数求模似乎只用在信息学竞赛和 A ...

  2. maven;spring;pom

    [说明]因为对环境配置文件理解的不充分,遇到问题经常是无法独自解决,特别是maven和javaweb的转换,也是糊里糊涂的,今天就又出问题了. [说明] 一:今日完成 1)任务二的效果展示看的我一脸懵 ...

  3. python脚本分析nginx访问日志

    日志格式如下: 223.74.135.248 [11/May/2017:11:19:47 +0800] "POST /login/getValidateCode HTTP/1.1" ...

  4. iOS基础动画的KeyPath取值

    一 .基础动画 1.基础动画的属性详解 注:Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程. 属性 解读 Autoreverses 设定这个属性为 YES 时,在它到达目的 ...

  5. Jquery遍历table并拿到每个单元格里的值

    $('#table_id tr').each(function(i){ alert(i); if(i!=0){ //td内的文本 var v0 = $(this).children('td').eq( ...

  6. linux c编程:popen

    我们在执行shell命令比如cat /etc/group | grep root的时候,通过管道的机制将cat /etc/group的结果传递给grep root,然后将结果显示出来 linux中提供 ...

  7. Tensorflow 初级教程(二)

    一.Tensorflow 扩展功能 1.自动求导 2.子图的执行 3.计算图控制流 4.队列/容器 Tensorflow 自动求导 当计算tensor C关于tensor W的梯度时,会先寻找从W到C ...

  8. (转)基于libRTMP的流媒体直播之 AAC、H264 推送

    参考: 1,基于libRTMP的流媒体直播之 AAC.H264 推送 http://billhoo.blog.51cto.com/2337751/1557646

  9. 什么是gevent

    gevent是一个基于协程的python网络库,它使用greenlet在libev或libuv事件循环之上提供高级同步API 功能包括 基于libev或libuv的快速时间循环 基于greenlets ...

  10. Hadoop Pig

    Pig包括两部分: 用于描述数据流的语言,称为Pig Latin. 用于执行Pig Latin程序的执行环境,当前有两个环境:单JVM中的本地执行环境和Hadoop集群上的分布式执行环境. Pig内部 ...