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

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

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 }
时间: 2024-10-21 22:53:35

HDU——T 3579 Hello Kiki的相关文章

HDU 3579 Hello Kiki 中国剩余定理(合并方程

题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> using namespace s

hdu 3579 Hello Kiki (中国剩余定理)

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1943    Accepted Submission(s): 693 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins s

hdu 3579 Hello Kiki

http://acm.hdu.edu.cn/showproblem.php?pid=3579 注意下最后的答案等于0是不行的,因为要的是正整数 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define inf (0x3f3f3f3f) typedef long

HDU——3579 Hello Kiki

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4206    Accepted Submission(s): 1616 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins

hdu 3579 Hello Kiki 不互质的中国剩余定理

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一

中国剩余定理 hdu 3579

HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3107    Accepted Submission(s): 1157 Problem Description One day I was shopping in the supermarket. There was a cashier counti

HDU 1517: kiki&#39;s game

/** * @link http://acm.hdu.edu.cn/showproblem.php?pid=1517 * @author Sycamore * @date Aug, 21 */// Suppose that their exists a direction(vertically or horizontally) in which// the steps remaining to arrive at the destination (n-1 or m-1) is even, as

HDU 2147 —— kiki&#39;s game

kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others) Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes the checkerboard game.The size of th

【HDU】2147 kiki&#39;s game

http://acm.hdu.edu.cn/showproblem.php?pid=2147 题意:n×m的棋盘,每次可以向左走.向下走.向左下走,初始在(1, m),n,m<=2000,问先手是否胜利. #include <cstdio> using namespace std; int main() { int n, m; while(scanf("%d%d", &n, &m), n|m) (n&1)&&(m&1)?