solution for 1006 中国剩余定理

一、要求

http://poj.org/problem?id=1006

Biorhythms

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 124113   Accepted: 39154

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.

   大概意思就是人的体力,情绪和智力的状态分别以23天,28天和33天为周期循环,每个循环中有一天是该指标对应的峰值日。输入p,e,i,d。分别代表第p天体力是峰值,第e天情绪是峰值,第i天智力是峰值,而当前是第d天。让你求多少天之后体力、情绪和智力的峰值日凑到同一天。  这个题目用暴力破解的方法可以解决,但是使用中国剩余定理去解决这种问题无疑是一种更加优雅的方法。

二、中国剩余定理

  中国南北朝时期(公元5世纪)的数学著作《孙子算经》卷下第二十六题,叫做“物不知数”问题,原文如下:
  有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?
  即,一个整数除以三余二,除以五余三,除以七余二,求这个整数。

按照我的理解,除数是三个质数3、5、7,所以满足这个条件的整数一定是以3*5*7=105为周期循环出现的。我们只需要求出满足条件的一个数,把它除以105得到的余数就是满足条件的最小解。

  我们先找到符合第一个条件:除以三余二的数。这里我们为了不让除数5和7惹麻烦,我们就在5和7的公倍数中找:

5和7的最小公倍数:35。35%3=2       好的,我们找到了的第一个数A=35

同理再找第二个数:

3和7的最小公倍数:21。21%5=1    我们要除五余三的数,所以把结果乘上3,(21*3)%5=3   第二个数B=63

再找第三个数:

3和5的最小公倍数:15。15%7=1-------->(15*2)%7=2           C=30

我们看到 A: 除3余2 除5余0 除7余0

B: 除3余0 除5余3 除7余0

C: 除3余0 除5余0 除7余2                 我们把这三个数加起来

Σ=  D: 除3余2 除5余3 除7余2  =128       是同时满足这三个条件的数!

明显D不是最小满足这个条件的数,把它减去105得到28就是这个问题的解。

三、应用

回到1006题中,以第三行的输入:5 20 34 325为例,X除23余5,除28余20,除33余34(即余1)。可以得到

A:28*33=924      924除23余4,而我们要的是余5的数,怎么办呢?我们把924从1乘到23,肯定能找到余数是5的结果:6468。

同理。B=12144,C=1288

X=A+B+C=19990,减掉325得到19575即为所求。

四、代码

  上班的时候用记事本写的,没注释,将就着看吧。

 1 #include<stdio.h>
 2
 3 int p,e,i,p1,e1,i1,t1,t2,t3,t,sum,result,j,count;
 4 p=23,e=28,i=33,count=1;
 5
 6 void cal(int p1,int e1,int i1,int t)
 7 {
 8     t1=e*i;
 9     for(j=0;j<p;j++)
10     {
11         if(t1%p==p1%p)
12         {
13             break;
14         }
15         t1+=e*i;
16     }
17
18     t2=p*i;
19     for(j=0;j<e;j++)
20     {
21         if(t2%e==e1%e)
22         {
23             break;
24         }
25         t2+=p*i;
26     }
27
28     t3=p*e;
29     for(j=0;j<i;j++)
30     {
31         if(t3%i==i1%i)
32         {
33             break;
34         }
35         t3+=p*e;
36     }
37
38     sum=t1+t2+t3;
39     result=sum;
40
41     if(sum<=t)
42     {
43         printf("Case %d: the next triple peak occurs in %d days.\n",count,sum-t);
44     }
45     else
46     {
47         while(sum>t)
48         {
49             sum-=21252;
50         }
51         printf("Case %d: the next triple peak occurs in %d days.\n",count++,sum-t+21252);
52     }
53 }
54 int main()
55 {
56     while(1)
57     {
58         scanf("%d %d %d %d",&p1,&e1,&i1,&t);
59         if(p1==-1)
60         {
61             break;
62         }
63         else
64         {
65             cal(p1,e1,i1,t);
66         }
67     }
68
69
70     return 0;
71 }
				
时间: 2024-10-07 08:19:00

solution for 1006 中国剩余定理的相关文章

POJ 1006 中国剩余定理

[题意]: 给定p,e,i,d,求解 (x + d) % 23 = p (x + d) % 28 = e(x + d) % 33 = i x最小正整数值 [知识点]: 中国剩余定理 [题解]: 典型的 xmodmi = ai模型,其中mi间两两互素.但该题式子较少,也可以直接自己化简带入值. [代码]: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #inc

转载----POJ 1006 中国剩余定理

本文为转载,源地址:   http://blog.csdn.net/dongfengkuayue/article/details/6461298 POJ 1006   Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78980   Accepted: 23740 Description Some people believe that there are three cycles in a perso

Biorhythms POJ - 1006 中国剩余定理

定理证明:https://blog.csdn.net/d_x_d/article/details/48466957 https://blog.csdn.net/lyy289065406/article/details/6648551 关键思路在于构造一个每部分膜另外一个不包含在部分内的部分的余数是1  然后把各部分分别乘以对应的余数 其中题目给出的第一天天数  就是  (n+d)%xn 的余数 不用处理 1 2 #include<iostream> 3 using namespace std;

POJ 1006:Biorhythms 中国剩余定理

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 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,

poj 1006(剩余定理)

生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111426   Accepted: 34702 Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易高度集中.因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天.对于每个人,我们想知道

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 "门前大桥下游过一

acm数论之旅--中国剩余定理

中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这个答案 5*7*inv(5*7,  3) % 3  =  1 3*7*inv(3*7,  5) % 5  =  1 3*5*inv(3*5,  7) % 7  =  1 这3个式子对不对,别告诉我逆元你忘了(*′?`*),忘了的人请翻阅前几章复习 然后两边同乘你需要的数 2 * 5*7*inv(5*

数论E - Biorhythms(中国剩余定理,一水)

E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status 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

gcd,扩展欧几里得,中国剩余定理

1.gcd: int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题:有一正整数ans,对于每一对数,都有:(ans-a[i])mod m[i]=0.求此数最小为多少. 输入样例: 1 10 2 3 1 2 3 2 3 5 8 1 2 3 4 5 6 7 8 97 89 67 61 59 53 47 88 12 1 2 3 4 5 6 7 8 9