ACM: 一道简单数论题 (重要的是建…

来源:互联网 发布:在淘宝上如何开店 编辑:程序博客网 时间:2024/05/14 00:34

                                                                           Biorhythms

 

Description

Some people believe that there are three cycles in a person'slife that start the day he or she is born. These three cycles arethe physical, emotional, and intellectual cycles, and they haveperiods of lengths 23, 28, and 33 days, respectively. There is onepeak in each period of a cycle. At the peak of a cycle, a personperforms 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 beeasier.
Since the three cycles have different periods, the peaks of thethree cycles generally occur at different times. We would like todetermine when a triple peak occurs (the peaks of all three cyclesoccur in the same day) for any person. For each cycle, you will begiven the number of days from the beginning of the current year atwhich one of its peaks (not necessarily the first) occurs. You willalso be given a date expressed as the number of days from thebeginning of the current year. You task is to determine the numberof days from the given date to the next triple peak. The given dateis not counted. For example, if the given date is 10 and the nexttriple peak occurs on day 12, the answer is 2, not 3. If a triplepeak occurs on the given date, you should give the number of daysto the next occurrence of a triple peak.

Input

You will be given a number of cases. The input for each caseconsists 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 currentyear at which the physical, emotional, and intellectual cyclespeak, respectively. The value d is the given date and may besmaller than any of p, e, or i. All values are non-negative and atmost 365, and you may assume that a triple peak will occur within21252 days of the given date. The end of input is indicated by aline in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a messageindicating the number of days to the next triple peak, in theform:

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 728

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

 

 题目意思:给出你当年 physical,emotional, and intellectual 三个周期开始的时间(不包括当天) 计算三个周期重合的时间天数.

 建模思想:中国剩余定理.

 1、分别找出能任两个数整除,而满足被第三个整除余几的数。

  2、将三个未知数加起来,减去这三个数的最小公倍数的整数倍。   

 

   N≡R1(mod d1) ≡R2(modd2)≡R3(mod d3)   

 

则N=k1*d2*d3*R1+k2*d1*d3*R2+k3*d1*d2*R3±d1*d2*d3*P   

 

其中

  

P为任意非负整数   

k1是满足k1*d2*d3≡1(mod d1)的最小正整数  

k2是满足k2*d1*d3≡1(mod d2)的最小正整数   

k3是满足k3*d1*d2≡1(mod d3)的最小正整数

 

C语言:Ki 求解:

for(i = 1; ; ++i)

{

      if(d2 * d3 * i % d1 == 1)

             break;

}

 

AC的代码:

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
 int p , e , in , day;
 int n = 1;
 int i;
 int num[3] = {0};

// freopen("a.txt","r",stdin);
    while(scanf("%d %d %d%d",&p,&e,&in,&day)!= EOF)
    {
         int sum = 0;
         if(p == -1 && e == -1&& in == -1&& day == -1)
              break;

         if(p == 0 && e == 0&& in == 0)
        {
               printf("Case%d: the next triple peak occurs in %d days.\n",n,21252-day);
              n++;
         }
        else
       {
               for(i = 1; ; ++i)
              {
                     if(28 * 33 * i % 23 == 1)
                    {
                            num[0] = i * 28 * 33;
                            break;
                     }
              }

             for(i = 1; ; ++i)
            {
                     if(23 * 33 * i % 28 == 1)
                     {
                            num[1] = i * 23 * 33;
                            break;
                     }
             }

             for(i = 1; ; ++i)
             {
                     if(23 * 28 * i % 33 == 1)
                    {
                           num[2] = i *23 * 28;
                           break;
                   }
            }

             sum = p *num[0] + e * num[1] + in * num[2];
            

            while( sum - day > 0)

           {
                    sum -= 21252;
            }
            sum+= 21252;
          printf("Case %d: the next triple peak occurs in %ddays.\n",n,sum-day);
            n++;
      }
    }

    return 0;
}

0 0
原创粉丝点击