Biorhythms
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 102540 | Accepted: 31642 |
Description
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
Output
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.生理周期
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 102540 Accepted: 31642 Description
人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。Input
输入四个整数:p, e, i和d。 p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于p, e, 或 i。 所有给定时间是非负的并且小于365, 所求的时间小于21252。 当p = e = i = d = -1时,输入数据结束。Output
从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。 采用以下格式: Case 1: the next triple peak occurs in 1234 days. 注意:即使结果是1天,也使用复数形式“days”。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 -1Sample 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.
解答这个问题源自于我国数学古书《孙子算经》中的一道问题:“今有物,不知其数,三三数之,剩二;五五数之,剩三;七七数之,剩二。问物几何?”意思是一个整数除以三余二,除以五余三,除以七余二,求这个整数(满足条件且最小)。
做法是:
n%3=2,n%5=3,n%7=2且3,5,7互质 找到使(5×7)的倍数模3得1的数,答案是70 找到使(3×7)的倍数模5得1的数,答案是21 找到使(3×5)的倍数模7得1的数,答案是15 那么(70×2+21×3+15×2) % (3×5×7) = 23,23就是最终答案了理解如下:
70×2 = 140,当中的2是指n%3 = 2的2,因为70%3=1,乘2后使其能满足%3=2的条件,同理, 21×3使其能满足%5=3 15×2使其能满足%7=2 又70能整除5和7,21能整除3和7,15能整除3和5 因此70×2+21×3+15×2 = 233能满足%3=2,%5=3,%7=2的条件 将233对3×5×7=105取模,是为了取得最小的情况,因为105是能够同时整除3,5,7的数,如果减去,对该数满足%3=2,%5=3,%7=2没有影响。因此对105取模,得到的结果便是最小并满足条件的数拓展开来问题就是有同余方程组 a = ai (mod ni), 求未知数a。方法是:
定义 n=n1*n2...nk, 其中因子两两互质 mi = n1*n2*...nk / ni; ci = mi(mf mod ni); 其中 mi*mf mod ni = 1; 则 a = (a1*c1+a2*c2+...+ak*ck) (mod n)中国剩余定理关键是mf的求法,如果理解了扩展欧几里得 ax+by=d, 就可以想到:
mi*mf mod ni = 1 => mi*mf+ni*y=1;这里的中国剩余定理必须要求除数是互质的,但是有些题目的同余方程式除数并不互质,那么将不能使用传统的中国剩余定理
中国剩余定理: 三周期
注释:三数为a b c,余数分别为 m1 m2 m3,%为求余计算,&&是“且”运算
⒈ 分别找出能被两个数整除,而满足被第三个整除余一的最小的数。
k1%b==k1%c==0 &&k1%a==1;
k2%a==k2%c==0 &&k2%b==1;
k3%a==k3%b==0 &&k3%c==1;
⒉ 将三个数(能被两个数整除、除以第三个数余1)乘对应数字的余数再加起来,减去这三个数的最小公倍数即得结果。
Answer = k1×m1 + k2×m2 + k3×m3 - P×(a×b×c);
P为满足Answer > 0的最大整数;
或者 Answer = (k1×m1 + k2×m2+ k3×m3)%(a×b×c) ;
同样,这道题的解法就是:
已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i
使33×28×a被23除余1,用33×28×8=5544;
使23×33×b被28除余1,用23×33×19=14421;
使23×28×c被33除余1,用23×28×2=1288。
因此有(5544×p+14421×e+1288×i)% lcm(23,28,33) =n+d又23、28、33互质,即lcm(23,28,33)= 21252;
所以有n=(5544×p+14421×e+1288×i-d)%21252本题所求的是最小整数解,避免n为负,因此最后结果为n= [n+21252]% 21252
那么最终求解n的表达式就是:n=(5544*p+14421*e+1288*i-d+21252)%21252;
代码
#include <iostream>
using namespace std;
int yuyi(int e,int i,int p)
{
int a,j=2;
a=e*i;
while(a%p!=1)
{
a=a/(j-1)*j;
j++;
}
return a;
}
int main()
{
int p,e,i,d,a,b,c,n,j=0;
while(cin>>p>>e>>i>>d)
{
if(p==-1 && e==-1 && i==-1 && d==-1)
break;
j++;
a=b=c=0;
a=yuyi(28,33,23);
b=yuyi(23,33,28);
c=yuyi(23,28,33);
n=(a*p+b*e+i*c)%(23*28*33)-d;
while(n<=0)
{
n=n+23*28*33;
}
cout<<"Case "<<j<<": the next triple peak occurs in "<<n<<" days."<<endl;
}
return 0;
}