H - Biorhythms (中国剩余定理)

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.

题意:求3个数的相遇周期。

定义M=23*28*33;

m[1]=23,m[2]=28,m[3]=33;

我们要求一个数res:使得res%23==p(mod  m[1] ),res%28==e(mod  m[2] )  ,res%33==i(mode  m[3]).

而中国剩余定理就是专门来求这个的。

我们可以求出来3个数x1,x2,x3,这3个数加起来mod  M即为最后周期,最后再根据d的取值,进行更改即可。

那么:第一个数要满足x1%23==p,x1%28==0,x1%33=0;

第二个数要满足x2%23==0,x2%28==e,x2%33=0;

第三个数要满足x2%23==0,x2%28==0,x2%33=i;

这样加起来mod  M即为res。

那么怎么求这3个数呢:根据扩展欧几里得算法即可

函数原型exgcd(Mi,m[i],x,  y).

则x1=M1*x*p;  (M1=M/m[1])  //M首先是m[1]*m[2]*m[3]   ,除去m[1]以后对m[2],m[3]取余都为0,我们要保证对m[1]取余为p,则再乘上p即可,以下同理。

x2=M2*x*e;   (M2=M/m[2])

x3=M3*x*i;   (M3=M/m[3])

 

#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
int m[10],r[10];
void exgcd(ll a,ll b,ll &x,ll &y)  //扩展欧几里得
{
    if(b!=0)
    {
        exgcd(b,a%b,x,y);
        int t=x;
        x=y;
        y=t-(a/b)*y;
    }
    else
        x=1,y=0;
}
ll CSYDL()  //中国剩余定理,这就是精髓了
{
    ll M = 1;
    ll ans = 0;
    for(int i=1; i<=3; i++)  //计算出乘积
        M *= m[i];
    for(int i=1; i<=3; i++)
    {
        ll x, y;
        ll Mi = M / m[i];  //我们算每一个x的时候要除去当前m[i]
        exgcd(Mi, m[i], x, y);  //扩展欧几里得求x
        ans = (ans + Mi * x * r[i]) % M;//将每个数相加
    }
    if(ans <= 0)
        ans += M;
    return ans;

}
int main()
{
    int p,e,i,d,icas=1;
    m[1]=23,m[2]=28,m[3]=33;
    while(cin>>p>>e>>i>>d)
    {
        if(p==-1 && e==-1&&e==-1 && d==-1)
            break;
        r[1]=p,r[2]=e,r[3]=i;
        ll k=CSYDL();
        if(k<=d)   //如果算出来的第一个相遇周期小于当前天数,则应该在下一个周期减去当前天数
            k=k+21252;
        d=k-d;
        printf("Case %d: the next triple peak occurs in %d days.\n",icas++,d);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值