题目连接:传送门
——————————.
Unknown Treasure
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2196 Accepted Submission(s): 814
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. 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 integers n,m,k(1≤m≤n≤10^18,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≤10^18 and pi≤10^5 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
——————————.
题目大意:
就是求C(n,m)%M ,M= p1*p2*p3*p4*…*pn;
题目解释:
大组合数就是用lucas定理求解
而lucas要求是对质数取模的时候才成立
所以分别对p[i] 进行求解 然后构成了一个同余方程组
用中国剩余定理求解就行了
注意大数乘法的时候可能会爆longlong 所以要用快速乘
附本题代码
——————————.
#include <stdio.h>
#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define LL long long int
#define pb push_back
LL qmul(LL a,LL b,LL c)
{
LL res=0;
while(b)
{
if(b&1) res=(res+a)%c;
a=(a+a)%c;
b>>=1;
}
return res;
}
LL qmod(LL a,LL b,LL c)
{
LL res=1;
while(b)
{
if(b&1) res=qmul(res,a,c)%c;
b>>=1;
a=qmul(a,a,c)%c;
}
return res;
}
LL exgcd(LL a,LL b,LL &x,LL &y) //ax+by=d
{
if(!b)
{
x=1;
y=0;
return a;
}
else
{
LL r=exgcd(b,a%b,x,y);
LL t = x;
x = y;
y=t-a/b*x;
return r;
}
}
LL CRT(LL a[],LL m[],LL len) //x%m[i]=a[i]
{
LL i,x,y,M,n=1,ret=0;
for(i=0; i<len; ++i) n*=m[i];
for(i=0; i<len; ++i)
{
M=n/m[i];
exgcd(M,m[i],x,y);
ret=(ret+qmul(qmul(x,M,n),a[i],n))%n;
}
return (ret+n)%n;
}
LL C(LL n,LL m,LL p)//组合数模素数P
{
if(m>n||m<0) return 0;
if(n-m<m) m=n-m;
LL a=1,b=1;
for(int i=0; i<m; ++i)
{
a=a*(n-i)%p;
b=b*(m-i)%p;
}
return a*qmod(b,p-2,p)%p;
}
LL Lucas(LL n,LL m,LL p)
{
LL ans=1;
while(n&&m&&ans)
{
ans=ans*C(n%p,m%p,p)%p;
n/=p,m/=p;
}
return ans;
}
LL a[11],p[11];
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
LL n,m;
int k;
scanf("%I64d%I64d%d",&n,&m,&k);
LL ans = 0;
for(int i=0; i<k; i++)
{
scanf("%I64d",&p[i]);
a[i] = Lucas(n, m, p[i]);
// printf("%I64d ",a[i]);
}
// puts("");
LL sum = CRT(a,p,k);
printf("%I64d\n",sum);
}
return 0;
}