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
≤
10
18
,
1
≤
k
≤
10
)
on a line where k is the number of primes. Following on the next line are kk different primes
p1,...,pk
p
1
,
.
.
.
,
p
k
. It is guaranteed that
M=p1⋅p2⋅⋅⋅pk≤1018
M
=
p
1
·
p
2
·
·
·
p
k
≤
10
18
and
pi≤105
p
i
≤
10
5
for every
i∈1,...,k
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
2018.8.24重新更新了代码,对于有多个不同质数取模,我换了一种风格来写
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ull unsigned long long
#define ll long long
#define maxn 100005
using namespace std;
ll mod;
ll fac[maxn];
inline ll mul(ll x,ll y){
x=x%mod,y=y%mod;
return ((x*y-(ll)(((long double)x*y+0.5)/mod)*mod)%mod+mod)%mod;
}
ll P(ll a,ll b)
{
ll ans=1;
a%=mod;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
inline void init()
{
fac[0]=1;
for(int i=1;i<mod;i++)
fac[i]=fac[i-1]*i%mod;
}
inline ll C(ll _n,ll _m)
{
if(_n<_m)return 0;
return fac[_n]*P(fac[_n-_m]*fac[_m]%mod,mod-2)%mod;
}
inline ll Lucas(ll _n,ll _m)
{
if(!_m)return 1;
return Lucas(_n/mod,_m/mod)*C(_n%mod,_m%mod)%mod;
}
ll k;
ll p[15];
ll r[15];
ll CRT()
{
ll ans=0;
ll M=1;
for(int i=0;i<k;i++)M*=p[i];
for(int i=0;i<k;i++)
{
mod=p[i];
ll Mi=M/p[i];
ll ti=P(Mi,mod-2);
mod=M;
ll temp=mul(mul(Mi,ti),r[i]);
ans=(ans+temp)%M;
}
return ans;
}
int main()
{
int t;
ll n,m;
cin>>t;
while(t--)
{
cin>>n>>m>>k;
for(int i=0;i<k;i++)
scanf("%lld",p+i);
for(int i=0;i<k;i++)
{
mod=p[i];
init();
r[i]=Lucas(n,m);
}
cout<<CRT()<<endl;
}
return 0;
}