首页 > ACM题库 > HDU-杭电 > HDU 1370 Biorhythms-数论-[解题报告] C++
2013
12-09

HDU 1370 Biorhythms-数论-[解题报告] C++

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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

输入:

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.

输出:

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.

样例输入:

1

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

样例输出:

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个循环周期,周期天数分别为23、28、33。对于某一年,已知某年这3个周期的某一峰值分别是当年的第p、e、i天,

问从第d天开始到最近一个满足3个周期都达到峰值的日期还有多少天。

初学者不会用模板 希望大牛可以指点

我就是照着算法导论的推理一步一步写的

n=n1*n2*n3*……*nk

其中mi=n /  ni 

其中ci=mi (mi(逆元)mod  ni)

a≡(a1*c1+a2*c2+…..+ak*ck)(mod n)

#include <iostream>
using namespace std;
int exgcd(int a,int b,int &x,int &y)
{
    if(a==0)
    {
        x=0;
        y=1;
        return b;
    }
    int g = exgcd(b%a,a,x,y);
    int tem = y;
    y=x;
    x=tem-(b/a)*y;
    return g;
}
int inv(int a,int n)//求逆元
{
    int x,y;
    exgcd(a,n,x,y);
    return (x%n+n)%n;
}
int main()
{
    int N,P,E,I,D,ans;
    const int m1=23,m2=28,m3=33,M1=28*33,M2=23*33,M3=23*28,m=23*28*33;
    const int M11 = inv(M1,m1),M22 = inv(M2,m2),M33 = inv(M3,m3);//求mi的逆元 
    scanf("%d",&N);
    while(scanf("%d%d%d%d",&P,&E,&I,&D)!=EOF)
    {
        if(P==-1&&E==-1&&I==-1&&D==-1)break;
        //a[i]*(mi*mi(逆)mod ni)因为在求逆元的时候已经mod ni了 
        //(mi*mi(逆)mod ni)是ci 
		ans = (P*M1*M11 + E*M2*M22 + I*M3*M33)%m;
        ans -= D;
        if(ans<=0) ans+=m;
        printf("Case %d: the next triple peak occurs in %d days.\n",N++,ans);
        //cout<<"Case "<<N++<<": the next triple peak occurs in "<<i<<" days.\n";
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/luyuncheng/article/details/8000303


  1. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示

  2. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  3. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥