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 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

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  3573 3574 3575 3576 3577

模板题,注意ans=0的情况,题目要求的是最小的正整数。

 1 //0MS    244K    1571 B    C++
 2 #include<stdio.h>
 3 __int64 gcd(__int64 a,__int64 b)
 4 {
 5     return b?gcd(b,a%b):a;
 6 }
 7 __int64 extend_euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
 8 {
 9     if(b==0){
10         x=1;y=0;
11         return a;
12     }
13     __int64 d=extend_euclid(b,a%b,x,y);
14     __int64 t=x;
15     x=y;
16     y=t-a/b*y;
17     return d;
18 }
19 __int64 inv(__int64 a,__int64 n)
20 {
21     __int64 x,y;
22     __int64 t=extend_euclid(a,n,x,y);
23     if(t!=1) return -1;
24     return (x%n+n)%n;
25 }
26 bool merge(__int64 a1,__int64 n1,__int64 a2,__int64 n2,__int64 &a3,__int64 &n3)
27 {
28     __int64 d=gcd(n1,n2);
29     __int64 c=a2-a1;
30     if(c%d) return false;
31     c=(c%n2+n2)%n2;
32     c/=d;
33     n1/=d;
34     n2/=d;
35     c*=inv(n1,n2);
36     c%=n2;
37     c*=n1*d;
38     c+=a1;
39     n3=n1*n2*d;
40     a3=(c%n3+n3)%n3;
41     return true;
42 }
43 __int64 china_reminder2(int len,__int64 *a,__int64 *n,__int64 &lcm)
44 {
45     __int64 a1=a[0],n1=n[0];
46     __int64 a2,n2;
47     for(int i=1;i<len;i++){
48          __int64 aa,nn;
49          a2=a[i],n2=n[i];
50          if(!merge(a1,n1,a2,n2,aa,nn)) return -1;
51          a1=aa;
52          n1=nn;
53     }
54     lcm=n1;
55     return (a1%n1+n1)%n1;
56 }
57 int main(void)
58 {
59     int t,n,k=1;
60     __int64 a[10],b[10],lcm;
61     scanf("%d",&t);
62     while(t--)
63     {
64         scanf("%d",&n);
65         for(int i=0;i<n;i++)
66             scanf("%I64d",&a[i]);
67         for(int i=0;i<n;i++)
68             scanf("%I64d",&b[i]);
69         printf("Case %d: ",k++);
70         __int64 ans=china_reminder2(n,b,a,lcm);
71         if(ans==0) ans+=lcm;
72         printf("%I64d\n",ans);
73     }
74     return 0;
75 }

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

时间: 2024-10-09 21:47:00

hdu 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 3430 Shuffling(置换群+中国剩余定理)

题目链接:hdu 3430 Shuffling 题意: 给出n张牌,标号为1-n,然后给出两个序列,序列1表示序列1,2,3,4--,n洗一次牌后到达的. 序列2表示目标序列,问初始序列按序列1的洗牌方式洗几次能到达序列2的情况,如果不能到达输出-1. 题解: 在初始序列和序列1的变换中找出1能变到那些牌,这些牌构成一个集合,这些集合中的牌必然是能够相互到达的. 然后在序列2中也找出这样一个集合,集合中这些元素的相互顺序是要一样的,这就是判断能否达到. 然后这样可以列出几个线性同余方程组,用中国

hdu X问题 (中国剩余定理不互质)

http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4439    Accepted Submission(s): 1435 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0],

hdu 5446 Unknown Treasure 中国剩余定理+lucas

题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define l

HDU 5238 Calculator(中国剩余定理+线段树)

题意: 有加,乘,次方3种运算,初始值为x,给定运算式. 现在有2种操作: 第一种:告诉你x的值,求答案模29393. 第二种:更改某个位置的运算. 解析: 线段树维护值域的问题,但是那个操作并不能简单的合并,因为值域还是很大的数组开不下,所以我们得另寻他法. 可以发觉29393并不是质数,29393 = 7×13×17×19. 设: t1=ans%7,t2=ans%13,t3=ans%17,t4=ans%19 那么问题的解就是模方程组 ???????????x≡t1(mod)7x≡t2(mod

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