The story of a monkey's peach is broadly described in two ways:
Description 1: Five monkeys divide the peach. In the middle of the night, the first monkey got up first, it divided the peach into five equal numbers, one more; So, it ate one, took a bunch. The second monkey up a look, only four piles of peach, so the four together, divided into equal five piles, and one more; then it ate one and took a bunch. The rest of the three monkeys are divided as well. Q: How many are there at least in this pile of peaches?
Description 2: There is a pile of peaches on the beach and five monkeys. The first monkey divided the pile of peaches into five, and one more, and the monkey threw one more into the sea and took a copy. The second monkey divided the remaining peaches evenly into five pieces, and one more, it also threw a lot of one into the sea, took a copy. Third, the fifth and the other monkeys are doing this, ask the beach at least how many peaches?
Analysis
Program apes generally look at the problem: in short, the number of Peaches is an integer, me from 1 to try, and then to the monkeys, if you can according to the requirements of the sub-method (remove 1 and then the average 5 parts, leaving 4 parts) divided 5 monkeys can not? That's a really mindless thinking program. But this is really a universal solution, and for the subject, the program will not run long.
Now look at the problem from a non-procedural ape's point of view. The main point here is to capture their number relationships.
Suppose the second monkey took a x2 peach and a third monkey took x3 one, then there was this relationship: 4 x2 = 5 x3 + 1 This is a similar 4 a = 5 b + 1 equation. No doubt, it's a b all integers. 4 a = 5 b + 1 = 4 b + (b + 1), then imaginable b + 1 = 4 k , so there are:
a = 5 k - 1 b = 4 k - 1
Because,,, (x1, x2) (x2, x3) (x3, x4) (x4, x5) all satisfy the similar 4 a = 5 b + 1 formula, also naturally satisfies the above a, b relation. Assuming that the corresponding K is k1 k2 k3 k4 , according x2 = 4 k1 - 1 = 5 k2 - 1 to, can be drawn k1 : k2 = 5 : 4 , so there will be:
k1 : k2 = 5 : 4 k2 : k3 = 5 : 4 k3 : k4 = 5 : 4
k1 k2 k3 k4are integers, so it is not difficult to find the smallest k1 is 5x5x5, of course, can add any multiple. So the total number of peaches that comes out should bez = 5 x1+1 = 5 (5 k1 - 1) + 1 = 3125 k - 4 ,(k ∈ N)
Answer
If you know the formula x = 3125 k - 4 , the answer is not to take out the calculator and then calculate the problem of hmm! The first 10 results are:
3121 6246 9371 12496 15621 18746 21871 24996 28121 31246
Programming
The idea of a program ape says, starting from 1, try, until the results of satisfying conditions come out. Here are the procedures for calculating the minimum number of peaches:
1 intMonkeyintN)2 {3 intRes, left, count;//Total peaches, number of peaches left, number of monkeys to divide4 5left = Res =1;6Count =0;7 while(1){8 if(Left-1) %5==0){//you can split a monkey .9++count;Tenleft = (left-1) /5*4; One}Else if(count! = N) {//You can't split the monkeys again. Aleft = + +Res; -Count =0; - } the if(count = = N)//meet the total number of monkeys, okay, found the - returnRes; - } -}
On the basis of the above program is easy to modify, and then to find a number of results, the following is a simple extension, max is used to limit the number of peaches:
1 intMonkey2 (intNintMaxint*sum)2 {3 intRes, left, count;//Total peaches, number of peaches left, number of monkeys to divide4 inti =0;//The number of solutions found5 6left = Res =1;7Count =0;8 while(1){9 if(Left-1) %5==0){//you can split a monkey .Ten++count; Oneleft = (left-1) /5*4; A}Else if(count! = N) {//You can't split the monkeys again. -left = + +Res; -Count =0; the } - if(count = = N) {//meet the total number of monkeys, okay, found the -sum[i++] =Res; -left = + +Res; +Count =0; - } + if(Res >max) A returni; at } -}
Ref
The monkey divided the Peach