HDU 5446 Unknown Treasure Lucas and CRT

Source: Internet
Author: User

Unknown Treasure

Time Limit:1 Sec

Memory limit:256 MB

Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=5446
Description

On the the-the-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, the mathematician found out, the correct combination to the lock would is obtained by Calculat ing how many ways be there to pick m different apples among N of them and modulo it with M. M is the product of several D Ifferent primes.

Input

The first line there was an integer 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) in a line where K is the number of primes. Following on the next line is K different primes P1,..., pk. It is guaranteed this m=p1⋅p2⋅⋅⋅pk≤1018 and pi≤105 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

HINT

Test instructions

Here you n,m,num.

and give you num number, K1,k2....,knum.

Then you ask for C (n,m)% (K1*k2*....*knum)

Exercises

First, C (n,m)%k We're going to ask for it.

So we're going to get num answers, and that's the value we get after%k1,k2,k3....knum.

Then we can see is similar to the Han Xin soldiers of the same problem, three people a group left 2, five people in a group left 2 of this ...

The treatment of the surplus theorem in China just fine.

Note that LL*LL will explode, so write a quick multiplication.

Code:

#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//**************************************************************************************voidEXTEND_GCD (ll a,ll &x,ll B,ll &y) {    if(b==0) {x=1, y=0; return;    } ll X1,y1; EXTEND_GCD (B,x1,a%b,y1); X=Y1; Y=x1-(A/b) *Y1;}    LL INV (ll A,ll m) {ll t1,t2;    EXTEND_GCD (A,T1,M,T2); return(t1%m+m)%m;} ll Qpow (ll x,ll y,ll m) {if(!y)return 1; ll ans= Qpow (x,y>>1, M); Ans= ans*ans%m; if(y&1) ans = ans*x%m; returnans;} ll Nump (ll X,ll p) {ll ans=0;  while(x) ans+=x/p,x/=p; returnans;} ll FAC (ll n,ll p,ll PK) {if(n==0)return 1; ll ans=1;  for(LL i=1; i<=pk;i++)    {        if(i%p==0)Continue; Ans= ans*i%PK; } ans= Qpow (ans,n/PK,PK); ll to= n%PK;  for(LL i =1; i<=to;i++)    {        if(i%p==0)Continue; Ans= ans*i%PK; }    returnFAC (N/P,P,PK) *ans%PK;} LL Cal (ll N,ll M,ll p, ll pi,ll PK) {ll a= FAC (N,PI,PK), B=FAC (M,PI,PK), C=FAC (N-M,PI,PK); ll D= Nump (N,PI)-nump (M,PI)-nump (N-m,pi); ll ans= A%PK * INV (B,PK)%PK * INV (C,PK)%pk*qpow (P,D,PK)%PK; returnans* (P/PK)%P*INV (P/PK,PK)%p;} ll MCNMODP (ll n,ll m,ll p) {ll ans=0; ll x=p;  for(LL i =2;i*i<=x&&x>1; i++) {ll k=0, pk=1;  while(x%i==0) {x/=i; K++; PK*=i; }        if(k>0) ans= (ans+cal (N,M,P,I,PK))%p; }    if(x>1) ans= (ans+cal (n,m,p,x,x))%p; returnans;} ll Qtpow (ll x,ll y,ll M) {LL ret=0LL;  for(x%=m;y;y>>=1LL) {if(y&1LL) {ret+=x; RET%=M; if(ret<0) ret+=M; } x+=x; X%=l; if(x<0) x+=M; }    returnret;}voidSolve (ll R[],ll s[],intt) {ll M=1ll,ans=0LL; ll p[ -],q[ -],e[ -];  for(intI=0; i<t;i++) M*=R[i];  for(intI=0; i<t;i++) {ll tmp=m/R[i],tt;        EXTEND_GCD (Tmp,p[i],r[i],q[i]); P[i]%=M; if(p[i]<0) p[i]+=M; E[i]=Qtpow (tmp,p[i],m); TT=Qtpow (e[i],s[i],m); Ans= (ANS+TT)%l; if(ans<0) ans+=M; } printf ("%i64d\n", ans);} ll ccc[ -],ddd[ -];intMain () {intT; scanf ("%d",&t); intnum =0;    ll N,m,p;  while(t--) {memset (CCC,0,sizeof(CCC)); memset (DDD,0,sizeof(DDD)); scanf ("%i64d%i64d%d",&n,&m,&num);  for(intI=0; i<num;i++) {scanf ("%i64d",&Ccc[i]); Ddd[i]=MCNMODP (N,m,ccc[i]);    } solve (Ccc,ddd,num); }    return 0;}

HDU 5446 Unknown Treasure Lucas and CRT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.