Biorhythms Chinese remainder theorem

  

Meaning of the questions :
  Who from birth have physical, emotional and intellectual three menstrual cycles, respectively 23, 28 and 33 days. A day in the peak period, on this day, the best man in the performance of the corresponding aspects (physical, emotional or mental). This is usually the peak period of three will not be the same day. Now given three dates, corresponding to the date of physical, emotional, intellectual peak appearance. Then gives a start date, required From this day, calculates the number of days least another three peaks occur simultaneously.
 
 
This problem is Chinese remainder theorem ultimate water problem (even templates are not really) but the Chinese remainder theorem has been a pain in my heart. .
 

Chinese remainder theorem

  There is such a problem in the "Sun Tzu Suan Jing" said: "Today there was an unknown number three number three of the remaining two (remainder divided by 32), fifty-five number of remaining three (3 divided by more than 5) Weeks the number of the remaining two (divided by more than 72), asked the geometric objects? "this problem is called" grandson problem ", called on the international general solution to the problem," Chinese remainder Theorem. " Specific solution in three steps:

    1. Find three numbers: 7 to find the minimum number of division remainder from a common multiple of 15 3 and 5, in addition to 5 to find the minimum number of I 21 from a common multiple of 3 and 7, 5 and 7 from the final in addition to find the smallest common multiple of the number of 70 I 31.
    2. 15 is multiplied by 2 (two for the final result is divided by 7), multiplied by 21 3 (3 is the final result is divided by the number 5). Similarly, 70 multiplied by 2 (the final result is divided by 2 to 3 the remainder), and the three products are added 15 * 2 + 21 * 3 + 70 * 2 15 * 2 + 21 * 3 70 * 2 + 233 and obtained.
    3. With the number 233 divided by the least common multiple of 105 3,5,7 three, the remainder of division 23, i.e. 233 % 105 = 23 233% 105 = 23. The remainder 23 is in line with the minimum number of conditions.

 

  So that 33 * 28 * a% 23 = 1, to obtain a = 6; 33 * 28 * 6 = 5544; 

  So that 23 * 33 * b% 28 = 1, have b = 19; 23 * 33 *  19 = 14421;
  make 23 * 28 * c% 33 = 1, to give c = 2; 23 * 28 *  2 = 1288.

  Then x = 5544 * p + 14421 * e + 1288 * i
  

 
#include<stdio.h>
 
#define MAX 21252
 
int main()
{
    int p, e, i, d, n, count = 0;
    
    while( scanf("%d%d%d%d", &p, &e, &i, &d) != EOF )
    {
                count++;
        if(p == -1 && e == -1 && i == -1 && d == -1)
        {
            break;
                }
 
        n = ( 5544 * p + 14421 * e + 1288 * i - d ) % MAX;
        
        if( n <= 0 )   // 范围限制 
        {
            n += 21252;
                }
        
        printf("Case %d: the next triple peak occurs in %d days.\n", count, n );
    }
    return 0;
}
View Code

 

 

 

 

Guess you like

Origin www.cnblogs.com/bxd123/p/10964501.html