HDU-1370,Biorhythms(中国剩余定理)

简介: HDU-1370,Biorhythms(中国剩余定理)

Problem 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.

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.


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:


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


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.




程序代码:


#include<stdio.h>
int r1,r2,r3,r;
void solve()
{
  int i;
  for(i=1,r1=23*28;;i++)
  {
    if(r1*i%33==1)
      break;
  }
  r1*=i;
  for(i=1,r2=23*33;;i++)
  {
    if(r2*i%28==1)
      break;
  }
  r2*=i;
  for(i=1,r3=28*33;;i++)
  {
    if(r3*i%23==1)
      break;
  }
  r3*=i;
  r=23*28*33;
}
int main()
{
  int Case=1,p,e,i,d,ans,n;
  solve();
  scanf("%d",&n);
  while(~scanf("%d %d %d %d",&p,&e,&i,&d))
  {
    if(p==-1&&e==-1&&i==-1&&d==-1)
      break;
    ans=(r1*i+r2*e+r3*p-d+r)%r;
    if(!ans)
      ans=r;
    printf("Case %d: the next triple peak occurs in %d days.\n",Case++,ans);
  }
  return 0;
}



相关文章
|
6月前
|
Java C++
poj 1503 高精度加法
把输入的数加起来,输入0表示结束。 先看我Java代码,用BigINteger类很多东西都不需要考虑,比如前导0什么的,很方便。不过java效率低点,平均用时600ms,C/C++可以0ms过。
16 1
|
6月前
poj 1185 炮兵阵地 (状态压缩dp)
如果你是刚刚开始做状态压缩dp,我建议你先看看 poj 3254 Corn Fields 这是一道比这一题更简单,更容易入门的题目。 还有在代码中我用了一个很巧妙的方法求一个数二进制数中1的个数 具体请看我博客中 x& (x - 1)==0 这篇文章 链接 。
18 1
|
6月前
|
存储
华为机试HJ43:迷宫问题
华为机试HJ43:迷宫问题
HDU7018.Banzhuan(计算几何+贪心)
HDU7018.Banzhuan(计算几何+贪心)
81 0
HDU7018.Banzhuan(计算几何+贪心)
POJ-3641,Pseudoprime numbers(快速幂)
POJ-3641,Pseudoprime numbers(快速幂)
|
网络架构
POJ-1005,I Think I Need a Houseboat(数学题)
POJ-1005,I Think I Need a Houseboat(数学题)
HDU-1370,Biorhythms(中国剩余定理)
本题主要就是应用中国剩余定理。
|
机器学习/深度学习
棋盘问题 POJ 1321
总时间限制:  1000ms 内存限制:  65536kB 描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
1141 0