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.
输入描述:
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.
输出描述:
Case: the next triple peak occurs in 1234 days.
Use the plural form “days” even if the answer is 1.
示例1

输入
0 0 0 0
输出
Case: the next triple peak occurs in 21252 days.

分析

题目的意思是,三个周期各有一个peak点,给出的前三个输入为每个周期峯值点出现在这年第几天,其下一次出现必然也是对应的周期后。要找的那天x,三个周期都出现峯值点,也就是
x%23 = p; x%28 = e; x%33 = i;
求出这个x来判断与给定的天数d相差几天。
这就变成了中国剩余定理的问题,求这个x
具体的中国剩余定理见:https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%9B%BD%E5%89%A9%E4%BD%99%E5%AE%9A%E7%90%86
这里M= 23*28*33 = 21252
m1 = 23; m2 = 28;m3 = 33
a1,a2,a3就是输入的前三个数
麻烦一点的是求t1,t2,t3,这里用扩展的欧几里得定理来求,具体原理可见:
http://www.cnblogs.com/My-Sunshine/p/4830388.html
递归的求x,y,其中x就是我们要的ti
注意求和的时候需要取模,以及需要判断是否为负

代码

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[4],m[4],mm[4];
int M= 1;
void extend_Euclide(int a, int b, int &x, int &y)
{
    if(b==0){
        x = 1; y = 0;return;
    }
    else{
        extend_Euclide(b,a%b,x,y);
        int temp = x;
        x = y;
        y = temp-(a/b)*y;
    }
}
int CRT()
{
    M = m[1]*m[2]*m[3];
    for(int i = 1;i<=3;i++)mm[i] = M/m[i];
    int res = 0;
    for(int i = 1;i<=3;i++)
    {
        int x,y;
        extend_Euclide(mm[i],m[i],x,y);
        res = (res+ (a[i]*x*mm[i]))%M;
    }
    if(res<0)res += M;
    return res;
}
int main()
{
    int p,e,i,d;
    m[1] = 23;m[2] = 28;m[3] = 33;
    while(cin>>p>>e>>i>>d)
    {
        a[1] = p; a[2] = e; a[3] = i;
        int k = CRT();
        int ans = 0;
        if(k==0)ans = 21252;
        else if(k<=d) ans = k+ 21252;
        else ans = k-d;
        printf("Case: the next triple peak occurs in %d days.\n",ans);
    }
}
点赞