永夜初晗凝碧天

本博客现已全部转移到新地址,欲获取更多精彩文章,请访问http://acshiryu.github.io/

导航

poj1006 中国剩余定理

Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 82054 Accepted: 24786

Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. 
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak. 

Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: 

Case 1: the next triple peak occurs in 1234 days. 

Use the plural form ``days'' even if the answer is 1.

Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

原题附带有中文翻译,这里就不重复了,详见http://poj.org/problem?id=1006&lang=zh-CN&change=true

很明显这题要求的是使等式 (x+d)= p mod 23 = e mod 28 = i mod 33 成立的最小的x,注意x不能为0
有几组特殊数据供参考
24 29 34 0           1
24 29 34 1           21252
24 29 34 2           21251
0  0  0  0             21252
关于中国剩余定理,我理解的也不是很好,直接套的模板,点击下载
套模板果然方便,1A

http://www.cnblogs.com/ACShiryu

参考代码:

 1 #include <iostream>  
2 using namespace std;
3
4 int Extended_Euclid(int a,int b,int &x,int &y) //扩展欧几里得算法
5 {
6 int d;
7 if(b==0)
8 {
9 x=1;y=0;
10 return a;
11 }
12 d=Extended_Euclid(b,a%b,y,x);
13 y-=a/b*x;
14 return d;
15 }
16
17 int Chinese_Remainder(int a[],int w[],int len) //中国剩余定理 a[]存放余数 w[]存放两两互质的数
18 {
19 int i,d,x,y,m,n,ret;
20 ret=0;
21 n=1;
22 for (i=0;i<len;i++)
23 n*=w[i];
24 for (i=0;i<len;i++)
25 {
26 m=n/w[i];
27 d=Extended_Euclid(w[i],m,x,y);
28 ret=(ret+y*m*a[i])%n;
29 }
30 return (n+ret%n)%n;
31 }
32
33
34 int main()
35 {
36 int n;
37 int w[3],a[]={23,28,33};
38 int k = 1;
39 while (~scanf("%d%d%d%d",&w[0],&w[1],&w[2],&n))
40 {
41 if ( w[0]==-1&&w[1]==-1&&w[2]==-1&&n==-1)
42 break;
43 int t = Chinese_Remainder(w,a,3) - n ; //引用中国剩余定理
44 t=t % ( 23 * 28 * 33 ) ;
45 if ( t <= 0 )
46 t += 23 * 28 * 33 ;
47 printf("Case %d: the next triple peak occurs in ",k) ;
48 printf("%d days.\n",t);
49 k++;
50 }
51 return 0;
52 }

posted on 2011-08-09 09:52  ACShiryu  阅读(652)  评论(0编辑  收藏  举报