Han Xin points soldiers

Description According to legend, Han Xin was extremely intelligent and never directly counted the number of his troops. He just asked the soldiers to change formations in a row of three, five, and seven, and he only glanced at the platoon of the team each time. You know the total number at the end. Input 3 non-negative integers a,b,c, indicating the number of people at the tail of each formation (a<3,b<5,c<7), output the minimum value of the total number of people (or report no solution). The total number of known people is not less than 10 and not more than 100.
enter
Enter 3 non-negative integers a,b,c, indicating the number of people at the tail of each formation (a<3,b<5,c<7). For example, enter: 2 4 5
output
Output the minimum value of the total number of people (or report no solution, that is, output No answer). Example, output: 89
sample input
2 1 6
Sample output
41
#include<iostream>
using namespace std;
int main() {
	int a, b, c;
	cin>>a>>b>>c;
    for(int i = 10; i <= 100; i++) {
    	if(i%3==a && i%5==b && i%7==c) {
    		cout<<i<<endl;
    		break;
		}
	}
    return 0;
}

However, although the enumeration is good, it is not clever enough. After some exploration, the following optimal solution is obtained:


Ancient Chinese scholars have studied this problem for a long time. For example, Cheng Dawei, a mathematician of the Ming Dynasty in my country, in his "Algorithm Tongzong" (1593), used four very popular formulas to suggest the solution to this problem:

Three people walk together for seventy years, 
five trees and one branch of plum blossoms, and 
seven sons are reunited for half a month, 
except one hundred and five to know.

"positive half moon" alludes to 15. The original meaning of "divide one hundred and five" is that when the obtained number is greater than 105, subtract 105 and 105 down to make it less than 105; 
this is equivalent to removing 105 to find remainder.


The four phrases imply that:

When the divisors are 3, 5, and 7, respectively,

  • Multiply 70 by the remainder of division by 3
  • Multiply 21 by the remainder of division by 5
  • Multiply 15 by the remainder of division by 7
  • Then add these three products
  • If the result of the addition is greater than 105, divide it by 105, and the remainder is the smallest positive integer solution that meets the requirements of the problem.

Take the test data 2 3 4 as an example:

According to the method implied by these four formulas, the number of soldiers in Han Xin's point can be calculated as follows:

70×2+21×3+15×4=263, 
263=2×105+53,

Therefore, there are at least 53 soldiers in this team. 
In this method, we see that the three numbers 70, 21, and 15 are very important. After a little research, we can find that their characteristics are:

  • 70 is the highest multiple of 5 and 7, and the remainder is 1 when divided by 3;
  • 21 is a multiple of 3 and 7, and the remainder is 1 when divided by 5;
  • 15 is a multiple of 3 and 5, and the remainder is 1 when divided by 7.
thus:
  • 70×2 is a multiple of 5 and 7, and the remainder is 2 when divided by 3;
  • 21×3 is a multiple of 3 and 7, and the remainder is 3 when divided by 5;

  • 15 × 4 is a multiple of 3 and 5, and the remainder is 4 when divided by 7.
  • code show as below:

#include<stdio.h>
int main() {
    int a,b,c,m;
    scanf("%d%d%d",&a,&b,&c);
    m=(a*70+b*21+c*15);
    if(m>105)
     m=m-105;
     printf("%d\n",m);
     return 0;
}

Finally, I quoted an article about the mathematical principle of Han Xin's military deployment. Interested friends can take a look:

Quote from: Junior Encyclopedia


The problem of Qin Wang's dark ordering of soldiers and Han Xin's random ordering of soldiers are a kind of storyization of the later generations' problem of not knowing the number of things. The problem  of not knowing the number of things comes
        from the ancient Chinese mathematics masterpiece "Sun Tzu's Suanjing" 1,600 years ago. The original title is: "I don't know how many things are there today, two of three or three, three of five or five, and two of seven or seven. How many are things? 
        " Pieces. If three pieces are counted with three pieces, there will be two pieces left; if five pieces are counted with five pieces, there will be three pieces left; if seven pieces are counted with seven pieces, there will be two pieces left. Question: This is How many items are there in the batch? 
        It becomes a pure mathematical problem: there is a number, divide the remainder by 3, divide the remainder by 5, and divide the remainder by 7. Find this number. 
        This problem is very simple: divide the remainder by 3 2. Dividing by 7 also has a remainder of 2, so the least common multiple of 3 and 7 is divided by 21, and the remainder is 2. When dividing the remainder by 21, we will first think of 23; 23 is exactly divided by 5 and the remainder is 3, so 23 is this question An answer to . 
        This problem is simple because of the speciality that division by 3 and division by 7 have the same remainder. Without this speciality, the problem is not so simple and much more interesting. 
        Let's change an example; Han Xin counts the number of soldiers in a team. The number of soldiers in a group of three is the remaining two, the group of five is the remaining three, and the group of seven is the remaining four. Question: How many soldiers are there at least in this group? 
        This question requires a positive number , make it divide the remainder by 3, divide the remainder by 5, divide the remainder by 3, and divide the remainder by 7, and hope that the number obtained is as small as possible. 
        If a classmate has never been exposed to such problems, he can also use the experimental addition The analytical approach adds conditions step by step to derive the answer. 
For example, we start with the condition that the remainder of 2 is divided by 3. The number that satisfies this condition is 3n+2, where n is a non-negative integer. 
To make 3n+2 still satisfy the condition of dividing 3 by 5, you can try to substitute n with 1, 2, 3,... 3, not in line with the meaning of the title; when n=2, 3n+2=8, 8 divided by 5 has a remainder of 3, and it can be seen that the number 8 satisfies the two conditions of dividing the remainder by 3 and dividing the remainder by 5. The 
last one The condition is that the remainder 4.8 when divided by 7 does not satisfy this condition. We want to get a number based on 8, so that it satisfies all three conditions at the same time. 
To this end, we thought that we can make the new number equal to 8 and a multiple of 3 and 5 Since the sum of 8 plus any integer multiple of 3 and 5 is divided by 3, there is still a remainder of 2, and when divided by 5, there is still a remainder of 3. So we let the new number be 8+15m, and set m=1, 2, ... Substitute it into the test. When m=3, you get 8+15m=53, 53 divided by 7 is exactly 4, so 53 meets the requirements of the title. 
Ancient Chinese scholars have studied this problem for a long time. For example, the mathematician Cheng of Ming Dynasty in China In his "Algorithm Tongzong" (1593), Dawei used four very popular formulas to suggest the solution to this problem: 
        three people walk together for seventy years, 
        five trees and plum blossoms and one branch, 
        seven sons reunite for half a month, 
        except One hundred and five will be learned. 
"positive half moon" implies 15. The original meaning of "divide one hundred and five" is that when the obtained number is greater than 105, 105 and 105 are reduced to make it less than 105; this is quite It is used to divide by 105 to find the remainder. 
These four formulas imply that when the divisors are 3, 5, and 7, multiply the remainder by 3 by 70, multiply by 21 by the remainder by 5, and use Multiply 15 by the remainder of the division by 7, and then add these three products. If the result of the addition is greater than 105, divide it by 105, and the remainder is the smallest positive integer solution that meets the requirements of the problem. 
Follow these four formulas Calculating the number of soldiers at Han Xin's point by the implied method can be: 
70×2+21×3+15×4=263, 
263=2×105+53, 
Therefore, there are at least 53 soldiers in this team. 
In this method, we see that the three numbers 70, 21, and 15 are very important. After a little research, we can find that their characteristics are: 
70 is a multiple of 5 and 7 , and the remainder is 1 when divided by 3; 
21 is a multiple of 3 and 7, and the remainder is 1 when divided by 5; 
15 is a multiple of 3 and 5, and the remainder is 1 when divided by 7. 
Therefore  ,
70 × 2 is a multiple of 5 and 7, using 3 is divided by 2; 
21×3 is a multiple of 3 and 7, and 5 is divided by 

; After adding a multiple of a, the number is divided by a, and the remainder is still b. Therefore, the result obtained by adding up 70×2, 21×3 and 15×4 can satisfy the requirements of “divide the remainder by 3, and divide the remainder by 5. Divide remainder 3, divide remainder 4 by 7". Generally, 
70m+21n+15k (1≤m<3, 1≤n<5, 1≤k<7) 
can simultaneously satisfy "divide remainder m by 3," Divide the remainder n by 5, divide the remainder by 7 and k". Divide by 105 and take the remainder is to find the smallest positive integer solution that meets the meaning of the question. 
We already know the properties and uses of the three numbers 70, 21, and 15 . , then, how do you find them? If you change the subject and the three divisors are no longer 3, 5, and 7, how should you find similar useful numbers? 
In order to find the multiples of 5 and 7 And the number that divides the remainder by 3, let's see if the least common multiple of 5 and 7 meets the requirements. The least common multiple of 5 and 7 is 5 × 7 = 35, 35 divided by 3 remainder 2, 2 times of 35 divided by 3 remainder Divide 2 times of 2,35 by 3 to get a remainder of 1, so we get "three people walking together seventy rare". 
In order to find the number that is a multiple of 3 and 7, divide 5 by 5 and have a remainder of 1, let's look at 3 Whether the least common multiple of 7 and 7 is satisfactory. The least common multiple of 3 and 7 is 3×7=21, 21 divided by 5 is exactly 1, so we get "five trees and plum blossoms and one branch". 
In order to find the number that is a multiple of 3 and 5 and the remainder of 1 is divided by 7, let's see if the least common multiple of 3 and 5 is satisfactory. The least common multiple of 3 and 5 is 3 × 5 = 15, 15 divided by 7 is exactly the remainder 1, so we get "Seven sons reunion positive half moon". 
The least common multiple of 3, 5, and 7 is 105, so "divide 
one hundred and five to know." Divide remainder 2 by 5 and remainder 5 by 7. 
We first find the number that is a multiple of 5 and 7 and the remainder of 1 is divided by 4; the least common multiple of 5 and 7 is 5×7=35, 35 divided by 4 and remainder 3, 3×3 is divided by 4 with remainder 1, so 35×3=105 divided by 4 with remainder 1, 105 is the number of multiples of 5 and 7 and the remainder of 1 divided by 4. 
We then find the multiple of 4 and 7 and the remainder of 1 divided by 5 Number; the least common multiple of 4 and 7 is 4×7=28, 28 is divided by 5 and the remainder is 3, and 3×7 is divided by 5 and the remainder is 1, so 28×7=196 is divided by 5 and the remainder is 1, so 196 is 4 and 7. The number of multiples and the remainder of 1 is divided by 5. 
Finally, the number of multiples of 4 and 5 and the remainder of 1 is divided by 7: The least common multiple of 4 and 5 is 4×5=20,20 divided by 7 and the remainder is 6,6× 6 is divided by 7 and the remainder is 1, so 20×6=120 is divided by 7 and the remainder is 1, so 120 is a multiple of 4 and 5 and the remainder is 1 when divided by 7. 
Using the three numbers 105, 196, and 120 can be found to match The problem requires 
105×3+196×2+120×5=1307. 
Since the least common multiple of 4, 5, and 7 is 4×5×7=140, 1307 is greater than 140, so 1307 is not the smallest solution that meets the requirements of the problem. Dividing 1037 by 140 yields a remainder of 47, and 47 is the smallest positive integer solution to the title.  In
general, 
105m+196n+120k (1≤m<4, 1≤n<5, 1≤k<7) 
is The remainder obtained by dividing the remainder m by 4, the remainder n by 5, and the remainder k by 7 (105m+196n+120k) divided by 140 is the smallest positive number that satisfies the above three conditions. 
Above, we obtained the characteristic number 105 in order to write the general expression of 105m+196n+120k. If it is only to answer our specific example, since 5×7=35 is both a multiple of 5 and 7 divided by 4 and If the remainder is 3, there is no need to find 105 and multiply it by 3. 
35+196×2+120×5=1027  is 
the number  that matches the meaning of the question. The smallest positive integer solution 47.  In the "Algorithm Tongzong", the characteristic numbers 70, 21, and 15, which play an important role in the problem of "the number of things do not know how many" are divided by 3, 5, and 7, are expressed in a few formulas Now, we can also compile the characteristic numbers 105, 196, and 120, which play an important role in the problem of divisors by 4, 5, and 7, as formulas. Leave it to the reader to compile it.  Any three divisors by two or two The coprime situation can be solved by the above method.  The theory on which the above method is based is called Sun Tzu's theorem in China, and foreign books are called the Chinese remainder theorem.





Guess you like

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