Unknown Treasure HDU

来源:互联网 发布:剑三正太脸型数据网盘 编辑:程序博客网 时间:2024/06/05 19:22

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(T20) representing the number of test cases.

Each test case starts with three integers n,m,k(1mn1018,1k10) on a line where k is the number of primes. Following on the next line are kk different primes p1,...,pk. It is guaranteed that M=p1p2pk1018 and pi105for every i1,...,k.
Output
For each test case output the correct combination on a line.
Sample Input
1
9 5 2
3 5
Sample Output
6
这个大数取模,而且都是素数并且两两不等,而且素数的范围在5次方内

#include<cstdio>#include<algorithm>#include<cmath>#include<iostream>#include<stack>#include<vector>#include<cstring>#define N 1005#define INF 0x3f3f3f3ftypedef long long ll;using namespace std; ll fac[100005];ll n,m,k;ll p[15];ll r[15];ll P(ll a,ll b,ll mod){    ll ans=1;    a%=mod;    while(b)    {        if(b&1)            ans=ans*a%mod;        a=a*a%mod;        b>>=1;    }    return ans;}ll comb(ll n,ll m,ll mod){    if(n<m)        return 0;    return fac[n]*P(fac[m]*fac[n-m],mod-2,mod)%mod;}ll Lucas(ll n,ll m,ll mod){    if(!m)return 1;    if(n==m)return 1;    return Lucas(n/mod,m/mod,mod)*comb(n%mod,m%mod,mod)%mod;}ll getR(ll mod){    for(int i=1;i<mod;i++)        fac[i]=fac[i-1]*i%mod;    return Lucas(n,m,mod);}ll modmul(ll a, ll b, ll MOD) {    ll ret = 0;    while(b) {        if(b & 1) ret = (ret + a) % MOD;        a = (a + a) % MOD;        b >>= 1;    }    return ret;}ll CRT(){    ll M=1;    for(int i=1;i<=k;i++)        M*=p[i];    ll ans=0;    ll temp;    for(int i=1;i<=k;i++)    {        temp=P(M/p[i],p[i]-2,p[i]);        temp=modmul(temp,r[i],M);        temp=modmul(temp,M/p[i],M);        ans=(ans+temp)%M;    }    return ans;}int main(){    fac[0]=1;    int t;    scanf("%d",&t);    while(t--)    {        scanf("%lld%lld%lld",&n,&m,&k);        for(int i=1;i<=k;i++)        {            scanf("%lld",p+i);            r[i]=getR(p[i]);        }         printf("%lld\n",CRT());    }    return 0;}
原创粉丝点击