HDOJ 题目5446 Unknown Treasure(Lucas+费马小定理+CRT)
来源:互联网 发布:algorithm算法中的sort 编辑:程序博客网 时间:2024/05/17 23:53
Unknown Treasure
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1662 Accepted Submission(s): 617
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 pickm different apples among n of them and modulo it with M .M is 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 integersn,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 k different primes p1,...,pk . It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤1018 and pi≤105 for every i∈{1,...,k} .
Each test case starts with three integers
Output
For each test case output the correct combination on a line.
Sample Input
19 5 23 5
Sample Output
6
Source
2015 ACM/ICPC Asia Regional Changchun Online
Recommend
hujie | We have carefully selected several similar problems for you: 5493 5492 5491 5490 5489
题目大意:给n,m,k,就是求c(n,m)对下边k个素数取余
ac代码
#include<stdio.h>#include<string.h>#define LL __int64LL fac[12][100005],M[12];LL tp[12];void get_fact(LL k){int i,j;for(i=0;i<k;i++){fac[i][0]=1;for(j=1;j<=tp[i];j++)fac[i][j]=(fac[i][j-1]*j)%tp[i];}}LL qpow(LL a,LL b,LL mod){LL ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans%mod;}LL lucas(LL n,LL m,int x){LL ans=1;if(n<m)return 0;while(n&&m){LL a=n%tp[x];LL b=m%tp[x];if(a<b)return 0;ans=(ans*fac[x][a]*qpow(fac[x][b]*fac[x][a-b]%tp[x],tp[x]-2,tp[x]))%tp[x];n/=tp[x];m/=tp[x];}return ans;}void exgcd(LL a,LL b,LL &x,LL &y){if(b==0){x=1;y=0;return;}exgcd(b,a%b,x,y);LL t=x;x=y;y=t-a/b*y;}LL CRT(int k){LL a1,b1,a2,b2,a,b,c,x,y;a1=tp[0],b1=M[0];for(int i=1;i<k;i++){a2=tp[i],b2=M[i];a=a1;b=a2;c=b2-b1;exgcd(a,b,x,y);x=((c*x)%b+b)%b;b1=b1+a1*x;a1=a1*b;}return b1;}int main(){LL n,g;int t;scanf("%d",&t);while(t--){LL n,m,k;scanf("%I64d%I64d%I64d",&n,&m,&k);LL i;int j;memset(M,0,sizeof(M));for(i=0;i<k;i++){scanf("%I64d",&tp[i]);//M[j]=(M[j]+lucas(n,m,i))%tp[i];}get_fact(k);for(i=0;i<k;i++){M[i]=(M[i]+lucas(n,m,i))%tp[i];}printf("%I64d\n",CRT(k));}}
0 0
- HDOJ 题目5446 Unknown Treasure(Lucas+费马小定理+CRT)
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
- HDU 5446 Unknown Treasure(CRT+lucas定理)
- HDU 5446 Unknown Treasure(lucas+中国剩余定理 / CRT)
- hdoj 5446 Unknown Treasure 【lucas + CRT】
- hdu 5446 Unknown Treasure (Lucas定理+CRT+快速乘)
- HDU 5446 Unknown Treasure Lucas+CRT
- HDU 5446 Unknown Treasure [lucas+CRT]【数论】
- HDU 5446 Unknown Treasure(Lucas定理+中国剩余定理)
- hdu 5446 Unknown Treasure(Lucas定理+中国剩余定理)
- HDU 5446 Unknown Treasure(Lucas定理+中国剩余定理)
- hdu 5446 Unknown Treasure (Lucas定理+中国剩余定理)
- hdu 5446 Unknown Treasure lucas+CRT || 扩展lucas
- hdu 5446 Unknown Treasure(lucas+中国剩余定理)
- Hdu 5446 Unknown Treasure(Lucas+中国剩余定理)
- hdu 5446 Unknown Treasure(lucas定理+中国剩余定理)
- HDU 5446 Unknown Treasure Lucas定理+中国剩余定理
- HDU 5446 Unknown Treasure 解题报告(Lucas定理 + 中国剩余定理)
- jQuery before和insertBefore区别
- iOS学习笔记——UIPickerView的简单应用
- uml在线工具
- GOOGLE MAP API 如何通过经纬度获取地址信息?
- UILabel详解
- HDOJ 题目5446 Unknown Treasure(Lucas+费马小定理+CRT)
- Python解析json文件报错:'utf8' codec can't decode byte 0xbb in position 0: invalid start byte
- 25个SSH命令收藏备用
- linux wc命令
- Linux下md5sum 校验文件完整性
- Sqlserver 存储过程
- OFBIZ webservice简介
- Android:通过自定义ImageView实现带边框的ImageView
- zigbee学习之睡眠定时器