首页 > 代码库 > HDU——T 3579 Hello Kiki

HDU——T 3579 Hello Kiki

http://acm.hdu.edu.cn/showproblem.php?pid=3579

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4208    Accepted Submission(s): 1617


Problem Description
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki‘s father found her note and he wanted to know how much coins Kiki was counting.
 

 

Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
 

 

Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
 

 

Sample Input
2214 575 56519 54 40 24 8011 2 36 20 76
 

 

Sample Output
Case 1: 341Case 2: 5996
 

 

Author
digiter (Special Thanks echo)
 

 

Source
2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU
 
我的妈呀 CRT 还不互质
shit
 1 #include <algorithm> 2 #include <cstdio> 3  4 using namespace std; 5  6 int n,m[23],a[23]; 7  8 int exgcd(int a,int b,int &x,int &y) 9 {10     if(!b)11     {12         x=1; y=0;13         return a;14     }15     int ret=exgcd(b,a%b,x,y),tmp=x;16     x=y; y=tmp-a/b*y;17     return ret;18 }19 int CRT(int m[],int a[])20 {21     int ret=a[1],mm=m[1];22     for(int i=2;i<=n;i++)23     {24         int gcd,x,y,b=m[i],tmp=a[i];25         int c=tmp-ret; gcd=exgcd(mm,b,x,y);26         if(c%gcd) return -1;27         x=x*c/gcd;28         int mod=b/gcd;29         x=(x%mod+mod)%mod;30         ret+=mm*x; mm*=mod;31     }32     if(!ret) ret+=mm;33     return ret;34 }35 36 int main()37 {38     int t; scanf("%d",&t);39     for(int k=1;k<=t;k++)40     {41         scanf("%d",&n);42         for(int i=1;i<=n;i++) scanf("%d",m+i);43         for(int i=1;i<=n;i++) scanf("%d",a+i);44         printf("Case %d: %d\n",k,CRT(m,a));45     }46     return 0;47 }

 

HDU——T 3579 Hello Kiki