码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)

时间:2019-02-12 00:29:08      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:scan   stream   note   VID   was   run   HERE   ota   can   

Hello Kiki

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


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
2 2 14 57 5 56 5 19 54 40 24 80 11 2 36 20 76
 

 

Sample Output
Case 1: 341 Case 2: 5996
 1 #include <iostream>
 2 #include<stdio.h>
 3 #include <algorithm>
 4 #include<string.h>
 5 #include<cstring>
 6 #include<math.h>
 7 #define inf 0x3f3f3f3f
 8 #define ll long long
 9 using namespace std;
10 
11 int m[15];
12 int r[15];
13 int n,x,y;
14 int gcd;
15 
16 int exgcd(int a,int b,int &x,int &y)
17 {
18     if(b==0)
19     {
20         x=1;
21         y=0;
22         return a;
23     }
24     int q=exgcd(b,a%b,y,x);
25     y=y-(a/b)*x;
26     return q;
27 }
28 
29 int main()
30 {
31     int t;
32     scanf("%d",&t);
33     for(int cnt=1;cnt<=t;cnt++)
34     {
35         bool flag=true;
36         scanf("%d",&n);
37         for(int i=0;i<n;i++)
38             scanf("%d",&m[i]);
39         for(int i=0;i<n;i++)
40             scanf("%d",&r[i]);
41         int a1=m[0];
42         int r1=r[0];
43         for(int i=1;i<n;i++)
44         {
45             int b1=m[i];
46             int r2=r[i];
47             int d=r2-r1;
48             gcd=exgcd(a1,b1,x,y);
49             if(d%gcd) {flag=false;break;}
50             int multiple=d/gcd;
51             int p=b1/gcd;
52             x=( (x*multiple)%p+p )%p;
53             r1=r1+x*a1;
54             a1=a1*b1/gcd;
55         }
56         if(flag)
57         {
58             if(r1==0) r1=a1+r1;///坑:如果余数是0则加一个最小公倍数
59             printf("Case %d: %d\n",cnt,r1);
60         }
61         else printf("Case %d: -1\n",cnt);
62     }
63     return 0;
64 }

 

hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)

标签:scan   stream   note   VID   was   run   HERE   ota   can   

原文地址:https://www.cnblogs.com/shoulinniao/p/10363663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!