Cattle off to prove safety net offer 46 questions - (number of circles in the last remaining) child's play

topic:

Children's Day each year, cow-off will prepare some small gifts to visit the orphanage children, this year is also true. As an experienced veteran off HF cattle, naturally we prepared some games. Among them, there is a game like this: First, let the children surrounded by a large circle. He then randomly assigned a number m, so that number of children 0 gettin number. Cried every time m-1 the children sing a song to be out of the line, then any of the gift box in choosing a gift, and not return to the circle, starting with his next child, continue 0 ... m -1 number of packets .... go on .... until the last remaining child, you can not perform, and get the cattle off the rare "Detective Conan" Collector's Edition (limited places oh !! ^ _ ^). You want to try next, which the children will get the gifts it? (NOTE: The number of children is from 0 to n-1)
If there are no children, return -1.
Ideas: The essence of this question is: Josephus problem! ! !
Ideas are as follows:

For ease of discussion, the first problem will be described in accordance with the original intent of the language of mathematics.

Problems: The number of 0 ~ (N-1) arranged in a circle which N individuals, the number of packets in the clockwise start from 0, M-1 human report circular queue exit, the remainder of the number of packets continues from zero ,repeatedly. Finally, find the first number in the column by a circular queue.

Listed first below 0 ~ (N-1) N This original individual numbered as follows:

The foregoing process was found derived, the first column of a person's number must be (M-1)% n. For example, in 41 individuals, 3 people report if the column is a first column of a person must be a number (3-1) 2 = 41%, number noted here is zero, so the actual number 2 1 corresponds to the number 3 as a starting point. The foregoing description, a front element of m (M-1) has a column, a list of the people that are listed as follows:

According to the rules, when it was a column, and from the next to the position number of packet 0 is started, the above list is adjusted (i.e., the start position in the form of M, then N-1 followed by the 0,1,2 ...... form a cyclic):

Arranged in order are renumbered above, the following correspondence relationship obtained:

That is, the one on the data row would be to reorganize the list 0 ~ (N-2) N-1 individuals were, seeking to continue the n-1 participants, the number of packets according to an M-1 column i.e., Solution the last number in the first column by a circular queue.

What the law does not see? Yes, at one time treatment, the scale of the problem is reduced. That is, the number N of individuals reported problems may be solved first decomposed into (N-1) number of sub-problems of the individual packets; whereas for the sub-questions (N-1) number of individual packets, a first order to be decomposed [(N -1) sub-problems. -1] number of people reported .......

The minimum size of the problem is what? That is, when only one person (N = 1), reported that the number to (M-1) out of the line of people, who then finally the column is? Of course, only the number is 0 this person. Accordingly, the following functions may be provided:

Then, when N = 2, the number of packets to (M-1) column of a person, who last column of the? Should get the number reported only one person out of the last column of numbers plus M, M-1 report because people have been out of the line, only 2 people, the other is the last person out of the line of columns, can be formulated as the following form:

By calculating the above formula, the result F (2) may exceed the value of N (total number). For example, set N = 2, M = 3 (i.e. 2 individuals, the number of packets to the column 2), the calculated value of the press is obtained:

A total of only two people involved, No. 3 who apparently did not. How to do? Because it is reported that the number of cyclic, so after two complete packet number, numbered from 0 and from the number of packets then began. According to this principle, the total number of values ​​can be obtained N modulo operation, namely:

5.5.4 mathematically Josephus Solution (2)

I.e., N = 2, M = 3 (i.e., there are two individuals, who reported that the number of the column to 3-1), the cycle number of the last dequeued packets numbered 1 person (numbering begins with 0). We look to project, as shown below, when the number of cycles of the two packets numbered 0, the number of people reported a number of 0 to 2 and 0, when the register 2 (M-1), the column number 0 , the last remaining person number 1, so the number of people out of 1 last column.

 

The above derivation can be easily deduced, when N = 3 at formula:

Similarly, we can also deduce when the number of participants is N, the last column personnel numbered formula:

In fact, this is a recurrence formula, the formula contains the following two formulas:

 

 

(Many people here do not understand why + M, for example, we consider only two people numbered 0,1, M is equal to 3. Because the last one out of the queue number must be 0. So the penultimate out queue numbers must be 1, M plus the nature of that the M-1 first bound to the column, so that the M-th to the last column, then consider the relationship circular queue, so take the remainder!)

 

With this recursion formulas, again very simple design program can be used to design a program recursive methods, specific code as follows:

  1. #include <stdio.h> 
  2. int main(void)  
  3. {  
  4.     int n,m,i,s=0;  
  5.     the printf ( "Enter the number of columns involved in the position M and N values ​​= ');  
  6.     scanf('%d%d',&n,&m);  
  7.       
  8.     printf ( 'the last person out of the line of initial location is% d \ n', josephus (n, m));  
  9.     getch();  
  10.     return 0 ;  
  11. }  
  12.  
  13. int josephus(int n,int m)  
  14. {  
  15.     if(n==1)  
  16.         return 0;  
  17.     else  
  18.         return (josephus(n-1,m) m)%n;  
  19. }  

In the above code, Josephus defines a recursive function (), and then calls the function in the main function operation.

Compiler performing the above procedure, the input values ​​of N and M, can quickly obtain the final column number of persons, the input N = 8, M = 3, Figure 5-19 shows the results obtained (note numbering begins with 0) .

Recursive function will take up more computer memory, when a recursive hierarchy is too deep may cause the program can not be executed, therefore, the program may be written directly into the following recursive form:

  1. #include <stdio.h> 
  2. int main(void)  
  3. {  
  4.     int n,m,i,s=0;  
  5.     the printf ( "Enter the number of columns involved in the position M and N values ​​= ');  
  6.     scanf('%d%d',&n,&m);  
  7.     for (i=2; i<=n; i )  
  8.         s=(s m)%i;  
  9.     printf ( 'the last person out of the line of initial location is% d \ n', s);  
  10.     getch();  
  11.     return 0 ;  
  12. }  

The results of this recursive procedure code execution and the execution results are identical.

As can be seen, after some mathematical derivation and, finally, a simplified procedure law, dozens of lines of code reduced to a few lines. More importantly, the efficiency of program execution have greatly advanced, eliminating a lot of repetitive cycles, even solving large N and M values, it will not be a problem

Guess you like

Origin www.cnblogs.com/shaonianpi/p/12551193.html