#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define ULL unsigned long long
#define LL long long
LL N,M;
int T,K;
LL Prime[12],B[12];
LL power(LL a,LL b,LL p){
LL res=1;
while(b!=0){
if(b&1)
res=(res*a)%p;
a=(a*a)%p;
b>>=1;
}
return res;
}
LL Cm(LL a,LL b,LL p){
if(a<b)
return 0;
if(a==b)
return 1;
if(b>a-b)
b=a-b;
LL ans=1,ca=1,cb=1;
while(b){
ca=(ca*a)%p;
cb=(cb*b)%p;
a--;
b--;
}
ans=(ca*power(cb,p-2,p))%p;
return ans;
}
LL Lucas(LL n,LL m,LL p){
if(m==0)
return 1;
return (LL)Cm(n%p,m%p,p)*(LL)Lucas(n/p,m/p,p)%p;
}
LL Ext_gcd(LL a,LL b,LL &x,LL &y){
if(b==0){
x=1;y=0;return a;
}
LL ret=Ext_gcd(b,a%b,y,x);
y-=a/b*x;
return ret;
}
LL multi(LL a,LL b,LL p){
LL ret=0;
while(b){
if(b&1)
ret=(ret+a)%p;
a=(a+a)%p;
b>>=1;
}
return ret;
}
LL china(LL n,LL *m,LL *a){
LL M=1,d,y,x=0;
for(int i=0;i<n;i++)
M*=m[i];
for(int i=0;i<n;i++){
LL w=M/m[i];
d=Ext_gcd(m[i],w,d,y);
x=(x+multi(multi(y,w,M),a[i],M))%M;
}
return (x+M)%M;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%lld%lld%d",&N,&M,&K);
for(int i=0;i<K;i++){
scanf("%lld",&Prime[i]);
B[i]=Lucas(N,M,Prime[i]);
}
printf("%lld\n",china(K,Prime,B));
}
return 0;
}
HDU 5446 Unknown Treasure
最新推荐文章于 2019-08-02 23:20:12 发布