HDU 5446 Unknown Treasure Lucas theorem + Chinese remainder theorem

Source: Internet
Author: User
Tags mul

Unknown Treasure

Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 2209 Accepted Submission (s): 821


Problem Descriptionon the "the" to "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 is there to pickmDifferent apples among n of them and modulo it with M. M is the product of several different primes.

Inputon the first line there are an integerT(t≤) Representing the number of test cases.

Each test case starts with three integersN,M,k (1≤ m≤ n≤ 1018,1≤< Span id= "mathjax-span-43" class= "Mi" >k≤ 10) On a line wherekIs the number of primes. Following on the next line iskDifferent primesP1,.. . ,pk . It's Guaranteed thatm= p1 ⋅ P2⋅ ⋅⋅ p K≤ 1018 andpi≤5 For every i∈{1,.. . ,k}.

Outputfor each test case output of the correct combination on a line.

Sample Input19 5 23 5

Sample OUTPUT6

Source2015 ACM/ICPC Asia Regional Changchun Online

Recommendhujie | We have a carefully selected several similar problems for you:5842 5841 5840 5839 5838 Analysis: According to Lucas solution each I:C (n,m)%pi, and then according to the The state surplus theorem integrates these results. will be able to get answers. Note that the Chinese remainder theorem calculates a value of 1 for the least common multiple of the current remainder and other remainder products, because the data volume is too large to be modulo.
#include <iostream>#include<stdio.h>using namespacestd;Long Longpri[ the];Long Longa[ the];Long LongEXT_GCD (Long LongALong LongBLong Long*x,Long Long*y) {    if(b==0)    {        *x=1, *y=0; returnA; }    Long LongR = EXT_GCD (b,a%b,x,y); Long Longt = *x; *x= *y; *y = t-a/b * *y; returnR;}Long LongQuick_mod (Long LongNLong LongMLong LongMoD) {    Long Longans=1;  while(m) {if(m&1) ans= (ans*n)%MoD; M>>=1; N= (n*n)%MoD; }    returnans%MoD;}Long LongGet_c (Long LongNLong LongMLong LongMoD) {    Long LongA=1, b=1;  for(intI=1; i<=m; i++) {b=b*i%MoD; A=a* (n-i+1)%MoD; }    return(A * (Quick_mod (b,mod-2, MoD)))%MoD;}Long LongLucas (Long LongNLong LongMLong LongMoD) {    if(m==0)return 1; return(Lucas (N/mod,m/mod,mod) *get_c (n%mod,m%mod,mod))%MoD;}Long LongMulLong LongALong LongNLong LongMoD) {a= (a%mod+mod)%MoD; N= (n%mod+mod)%MoD; Long LongRET =0;  while(n) {if(n&1) ret= (ret+a)%MoD; A= (a+a)%MoD; N>>=1; }    returnret%MoD;}Long LongChinese_reminder (Long LongA[],Long LongPri[],intLen) {    Long LongMul_pri=1; Long Longres=0;  for(intI=0; i<len;i++) {Mul_pri*=Pri[i]; }     for(intI=0; i<len;i++)    {        Long Longm = mul_pri/Pri[i]; Long Longx, y; EXT_GCD (Pri[i],m,&x,&y); //res= (res+y*m*a[i])%mul_pri;Res= (Res+mul (Mul (Y,M,MUL_PRI), A[I],MUL_PRI))%Mul_pri; }    return((RES%MUL_PRI+MUL_PRI)%mul_pri);}intMain () {intT; Long Longn,m; intK; scanf ("%d",&t);  while(t--) {scanf ("%i64d%i64d%d",&n,&m,&k);  for(intI=0; i<k; i++) scanf ("%i64d", pri+i);  for(intI=0; i<k; i++) {A[i]=Lucas (N,m,pri[i]); }        Long LongAns =Chinese_reminder (a,pri,k); printf ("%i64d\n", ans); }    return 0;}
View Code

HDU 5446 Unknown Treasure Lucas theorem + Chinese remainder theorem

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.