hdu5446 Unknown Treasure(数论综合题:大组合数取大合数模:Lucas+CRT)
2015-09-22 11:16
399 查看
Link:http://acm.hdu.edu.cn/showproblem.php?pid=5446
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1590 Accepted Submission(s): 585
Problem Description
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 m different
apples among n of
them and modulo it with M. Mis
the product of several different primes.
Input
On the first line there is an integer 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) 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
Sample Output
Source
2015 ACM/ICPC Asia Regional Changchun Online
题意:求C(n,m)%M,其中M=(m0*m2*…*m(k-1)),mi为素数。
AC code:
Unknown Treasure
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1590 Accepted Submission(s): 585
Problem Description
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 m different
apples among n of
them and modulo it with M. Mis
the product of several different primes.
Input
On the first line there is an integer 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) 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
2015 ACM/ICPC Asia Regional Changchun Online
题意:求C(n,m)%M,其中M=(m0*m2*…*m(k-1)),mi为素数。
AC code:
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #include<map> #include<stack> #include<vector> #define LL long long #define MAXN 1000010 using namespace std; const int N=20;//模方程数 LL a ,mod ; LL mul(LL a,LL b,LL mod)//a*b%mod { LL ans=0; while(b){ if(b&1) ans=(ans+a)%mod; b>>=1; a=(a+a)%mod; } return ans; } LL quick_mod(LL a,LL b,LL m)//a^b%m { LL ans=1; a%=m; while(b) { if(b&1) { ans=ans*a%m; } b>>=1; a=a*a%m; } return ans; } LL getC(LL n,LL m,int cur)//C(n,m)%mod[cur] { LL p=mod[cur]; if(m>n) return 0; if(m>n-m) m=n-m; LL ans=1; for(int i=1;i<=m;i++) { LL a=(n+i-m)%p; LL b=i%p; //ans=mul(ans,mul(a,quick_mod(b,p-2,p),p),p);//p为素数,i对p的逆元可以不用扩张欧几里得进行求解 re=i^(P-2) ans = ans * (a * quick_mod(b, p-2,p) % p) % p; } return ans; } LL Lucas(LL n,LL k,int cur)//求C(n,k)%mod[cur] { LL p=mod[cur]; if(k==0) return 1%p; //return getC(n%p,k%p,cur)*Lucas(n/p,k/p,cur)%p; return getC(n % p, k % p,cur) * Lucas(n / p, k / p,cur) % p; } void extend_Euclid(LL a,LL b,LL &x,LL &y) { if(b==0) { x=1; y=0; return; } extend_Euclid(b,a%b,x,y); LL tmp=x; x=y; y=tmp-a/b*y; } LL CRT(LL a[],LL m[],int k)//求C(n,m)%M,其中M=(m0*m2*…*m(k-1)),mi为素数,则先用a[i]存储模方程C(n,m)%mi, { //m[]存储所有素数因子mi,k表示总共有k个模方程,返回C(n,m)%M的值 LL M=1; LL ans=0; for(int i=0;i<k;i++) M*=mod[i]; for(int i=0;i<k;i++) { LL x,y,tmp; LL Mi=M/m[i]; extend_Euclid(Mi,m[i],x,y); if(x<0) { x=-x; tmp=mul(Mi,x,M); tmp=mul(tmp,a[i],M); tmp=-tmp; } else { tmp=mul(Mi,x,M); tmp=mul(tmp,a[i],M); } ans=(ans+tmp)%M; } while(ans<0) ans+=M; return ans; } int main() { //freopen("D:\\in.txt","r",stdin); int T; scanf("%d",&T); while(T--) { LL n,m; int k; scanf("%lld%lld%d",&n,&m,&k); //k=1; for(int i=0;i<k;i++) scanf("%lld",&mod[i]); for(int i=0;i<k;i++) a[i]=Lucas(n,m,i)%mod[i]; //printf("%I64d\n",a[0]); printf("%lld\n",CRT(a,mod,k)); } return 0; }
相关文章推荐
- iOS9问题备忘
- VS2010与水晶报表V13的打包集成小结
- 【ajax跨域】原因原理解决
- php类获取静态变量值以及调用
- 热更新-Android与Lua相互通信
- XMLItergration.java
- java.io.IOException: Stream closed
- Android App性能信息获取方法
- iOS 9中使用手Q支付SDK须知
- 关于Ubuntu 9.1 Server 自带安装的Tomcat + Java 命令
- 欢迎使用CSDN-markdown编辑器
- 错误名称:EntityCommandExecutionException
- Java项目引入第三方Jar包
- 关东升的iOS实战系列图书 《iOS实战:入门与提高卷(Swift版)》已经上市
- SyntaxError: Non-ASCII character '\xe5'
- 10 条真心有趣的 Linux 命令
- java四种线程池的使用,觉得写得不错,值得借鉴
- “拒绝了对对象数据库的 EXECUTE 权限”之解决
- Gradle project sync failed Error:Connection reset问题
- ThreadLocal