>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. Xshell调节字体大小和样式

    有时候没有看着字体太小的,好难受, 调节字体大小: ALT+P快捷键打开

  2. php字符串操作: 去掉UTF-16的空格

    $s = json_encode($s); $s = str_replace('\u00a0','',$s); $s = str_replace('\u3000','',$s); $s = str_r ...

  3. JavaScript-Confirm用法

    function checkMobileBind() {    if(confirm('您尚未绑定手机,是否前往绑定?'))    {        window.location='http://m ...

  4. 洛谷 P1558 色板游戏

    洛谷 题解里面好像都是压位什么的, 身为蒟蒻的我真的不会, 所以就来谈谈我的30颗线段树蠢方法吧! 这题初看没有头绪. 然后发现颜色范围好像只有30: 所以,我就想到一种\(sao\)操作,搞30颗线 ...

  5. SAP号码段

    [转http://blog.csdn.net/wengyupeng/article/details/8513527] 1.通用Tcode:SNRO 常用维护特定Number range Tcode: ...

  6. 2.5链表 链式A+B

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAowAAAGpCAIAAACI2PCZAAAgAElEQVR4nO2d3YsdSX6m++/wXf8Fxu ...

  7. Web Service概念辨析

    Web Service包含两个概念. 其一是Web Service标准体系,由SOAP.WSDL.UDDI三要素组成,是平台和语言无关的.在这个概念里和WCF做比较是错误的,因为前者是行业标准,后者是 ...

  8. Intel Quick Sync Video Encoder 2

    这边博客主要记录在预研quick sync中涉及到的一些性能质量相关的关键参数设置. github: https://github.com/MarkRepo/qsve 1. VPP处理过程伪代码: M ...

  9. 阿里云修改centos7主机名

    为了玩Docker,买个阿里云主机也是够拼的了. [root@iZ284olvkmjZ ~]# 不过主机名中怎么好DT,无奈,修改. 我们需要的是永久生效,阿里云提供了两种方法: 方法(1). 输入h ...

  10. HDU - 5703 Desert 【找规律】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5703 题意 给出一杯容量为N的水 每次至少喝1个单位 有多少种不同的方式喝完 比如 给出3 就有4种方 ...