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

 
Output
For each test case output the correct combination on a line.
 
Sample Input
1
9 5 2
3 5
 
Sample Output
6
 
Source
 

题目大意:让求C(n,m)%(∏pi) 这个式子的值。

中国剩余定理:

解题思路:首先用lucas定理将求C(a,b)%p转化成求解∏C(bi,ai),这样,我们可以得到c[i]数组。然后用中国剩余定理来求x0的值,即为答案。在求解的过程中需要用到扩展欧几里得来求解Mi的逆元,由于Mi比较大,所以在乘积的时候会爆数据范围,所以改成快速乘取模的方式代替直接乘积。

#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int maxp=1e5+200;
INT p[15],c[15];
INT fac[maxp],inv[maxp];
INT powmod(INT a,INT n,INT mod){//快速幂取模
INT ret=1;
while(n){
if(n&1){
ret=ret*a%mod;
}
n>>=1;
a = a*a%mod;
}
return ret;
}
INT mulmod(INT a,INT b,INT mod){//快速乘取模
a = (a%mod + mod) % mod; //用扩展欧几里得求出的值可能为负值
b = (b%mod + mod) % mod; //用扩展欧几里得求出的值可能为负值
INT ret=0;
while(b){
if(b&1){
ret = (ret+a)%mod;
}
b >>= 1;
a = (a<<1) % mod;
}
return ret;
}
void init(INT n){ //递推出来阶乘和逆元数组
fac[0]=1;
for(int i=1;i<n;i++){
fac[i]=fac[i-1]*i % n;
}
inv[n-1]=powmod(fac[n-1],n-2,n);
for(int i=n-2;i>=0;i--){
inv[i] = inv[i+1] * (i+1) % n;
//fac[n]*inv[fac[n]]≡1%p ==> fac[n-1]*(n*inv[fac[n]])≡1%p
}
}
INT cm(INT n,INT m,INT mod){ //用逆元求组合数取模
if(n<0||m<0||m>n){
return 0;
}
return fac[n]*inv[n-m]%mod*inv[m]%mod;
}
INT lucas(INT n,INT m,INT mod){//lucas递归求P进制时的c
if(m==0){
return 1;
}
return lucas(n/mod,m/mod,mod) * cm(n%mod,m%mod,mod) % mod;
}
INT exgcd(INT a,INT b,INT &x,INT &y){ //求b关于模a的逆元。放在y中
if(b==0) { x = 1; y = 0; return a; }
INT d = exgcd(b, a%b , y, x);
y -= x * (a / b);
return d;
}
void CRT(INT k){//中国剩余定理求解一元线性同余方程组
INT M=1,x,y;
INT ans=0;
for(int i=1;i<=k;i++){
M *= p[i];
}
for(int i=1;i<=k;i++){
INT Mi=M/p[i];
exgcd(p[i],Mi,x,y);
ans = (ans+mulmod(mulmod(y,Mi,M),c[i],M))%M ;
}
printf("%I64d\n",ans);
}
int main(){
INT n,m,k;
int t;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d%I64d",&n,&m,&k);
for(int i=1;i<=k;i++){
scanf("%I64d",&p[i]);
init(p[i]);
c[i] = lucas(n,m,p[i]);
}
CRT(k);
}
return 0;
}

  

HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】的更多相关文章

  1. HDU 5446 Unknown Treasure(Lucas定理+CRT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...

  2. 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.. ...

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

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

  4. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  5. hdu 5446 Unknown Treasure lucas和CRT

    Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  6. hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

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

  7. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

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

  8. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

  9. HDU 5446 Unknown Treasure

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

随机推荐

  1. C# 三元运算

    x=,y=; z=x>y? : 结果z= x=,y=; z=x>y? : 结果z=

  2. Powershell Deploy Service Fabric Application To Local Cluster

    之前写过一篇用 Powershell 部署 Service Fabric Application 到本地集群的随笔,感觉过程有点复杂,这次将流程简化,只需要将应用程序打包,加上配置文件就可以了.   ...

  3. 对XML文档进行修改

    怎样对XML文档时行修改.Insus.NET在此举个简单的例子.XML文档,就以这篇博文:http://www.cnblogs.com/insus/p/3274220.html 如果我们想对其中一个节 ...

  4. 【转】Linux将composer的bin目录放到PATH环境变量中

    将composer的bin目录放到PATH环境变量中 使用composer global config bin-dir --absolute查看composer的bin目录 输出类似 Changed ...

  5. [Oracle入门级]知识概况

    oracle各个版本间的主要技术更新 oracle 增加数据库创建和存储对象 oracle 8i 整体性能提升 oracle9i 实施应用集群 oracle 10g 支持网格计算 oracle 11g ...

  6. silverlight browse information

    public class Browser { /// <summary> /// During static instantiation, only the Netscape flag i ...

  7. the swap trick用于锐减过剩容量

    1.由于vector的复制构造函数只为被复制的vector分配它所需要的空间,故可以用如下的方式来削减vector v中过剩的容量:vector<int>(v).swap(v) 2.the ...

  8. kuangbin专题七 HDU1754 I Hate It (单点修改维护最大值)

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...

  9. HashMap 1.8的源码分析一

    public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, ...

  10. 【实例分割】PANet简单笔记

    PANet是18年的一篇CVPR,作者来自港中文,北大,商汤与腾讯优图,PANET可看作Mask-RCNN+,是在Mask-RCNN基础上做的几处改进. 论文地址:https://arxiv.org/ ...