remainder theorem

Chinese remainder theorem

There is a question in Sun Tzu's Sutra : " Now there are things that I don't know how many. If three or three are counted, two are left, five or five are left three, and seven or seven are left two. How many things are there? "

Translated into the current mathematical problem is  x%3 == 2 , x%5 == 3 , x%7 == 2, find the value of ;

Encountering such a problem, many C language beginners can't help but think that it can be calculated by violence. Why do you need such a theorem?

What if the data is fairly large? Computers will make calculations quite difficult. However, this problem was solved by the grandson early.

 Find the least common multiple lcm  in pairs of 3, 5, and 7 , k*lcm and another number mod equal to 1 ( find a qualified k );

The remainder of the equation     with k*lcm* another number not in lcm [ (a bit rounded) is  lcm(3,5), the other number is the remainder of in the equation where x%7==2  is ie find this k*lcm ( 3,5 ) *2]

Find the method (thought of the remainder theorem) :

  Lcm(3,5) = 15; // lcm is the least common multiple 

  Lcm(3,7) = 21;

  Lcm(5,7) = 35;

  a*15%7 == 1;

  b*21%5 == 1;

  c*35%3 == 1;

The values ​​of a, b, and c   can be obtained as 1, 1, 2 ;

  We get 15%7 == 1 , 21%5 == 1 , 70%3 == 1 ;

  Years = (15*2 + 21*3 + 70*2)% lcm(3,5,7);  

  Years = 23;

Add another example :

A number divided by 3 has a remainder of 1 , divided by 4 and a remainder of 2 , and divided by 5 with a remainder of 4. What is the smallest number?

 The three numbers 3 , 4 , and 5 in the question are relatively prime in pairs. Then [ 4 , 5 ] = 20 ; [ 3 , 5 ] = 15 ; [ 3 , 4 ] = 12 ; [ 3 , 4 , 5 ] = 60 .  

In order to divide 20 by 3 with a remainder of 1 , use 20 × 2=40 ; to divide 15 by 4 with a remainder of 1 , use 15 × 3=45 ; to divide 12 by 5 with a remainder of 1 , use 12 × 3=36 .   

Then, 40 × 1 + 45 × 2 + 36 × 4=274 , 

Because, 274>60 ;

So, 274%60 = 34 , which is the desired number.

 

Stick a Residual Theorem Problem POJ  1006  http://poj.org/problem?id=1006    

The topic has Chinese meaning in the upper corner

The solution to this problem is: 

It is known that (ans+d)%23=a; (ans+d)%28=b; (ans+d)%33=c 
       make 33×28×X divided by 23 and the remainder 1, use 33×28×8 = 5544; 
       Divide 23×33×Y by 28 with a remainder of 1, use 23×33×19 = 14421; 
       make 23×28×Z divide by 33 with a remainder of 1, use 23×28×2 = 1288.

      So X==8, Y==19, Z==2; 
      thus (5544×a + 14421×b + 1288×c)% lcm(23,28,33) =ans + d 

And 23, 28, 33 are coprime, that is, lcm(23, 28, 33) = 21252;
      so there is ans = (5544×a+14421×b+1288×cd)% 21252

AC code

#include<iostream>  
#include<stdio.h>  
using namespace std;  
intmain()  
{  
    int a, b, c, d,cnt=1;  
    int years;  
    while (scanf("%d%d%d%d", &a, &b, &c, &d)!=EOF&&a!=-1)  
    {  
        years = (a * 5544 + b * 14421 + c * 1288) % 21252;  
        years -= d;  
        if (years<=0) years += 21252;  
        if(years>21252) years=21252;  
        printf("Case %d: the next triple peak occurs in %d days.\n", cnt++, ans);  
    }  
    return 0;  
}

Violence can handle this

Violent AC Code

#include<iostream>  
#include<stdio.h>  
using namespace std;  
intmain()  
{  
    int a, b, c, d,cnt=1,i;  
    while (scanf("%d%d%d%d", &a, &b, &c, &d)!=EOF&&a!=-1)  
    {  
        for(i=1; ; i++)  
            if((i-a)%23==0&&(i-b)%28==0&&(i-c)%33==0&&i>d)  
            {  
                break;  
            }  
            printf("Case %d: the next triple peak occurs in %d days.\n", cnt++, i-d);  
    }  
    return 0;  
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469016&siteId=291194637