Other algorithms -046- child's play - the last remaining circle number (Josephus)

Article Directory

Title Description

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 countin ... 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.

analysis

This is a typical 约瑟夫环problem:

  • Method a: Method modeled as circular list, N number of N individual considered list node, node 2 points to node 1, node 2 points to node 3, ......, N-1 node points to the node N, node points to the node N 1, thus forming a ring. Then down the number off 1, 2, 3 ...... node from the beginning, every report M, put that node is removed from the ring. Then the next node number from 1 start packets. The final list a remaining node. It is the ultimate winner. The time complexity of O (mn).
    Here Insert Picture Description

  • Method Two: formula, Josephus is a classic mathematical problem, we can easily find this number in turn reported, it seems that there is a pattern. To this end the following definitions:

    • Problem : N Personal No. 1,2, ......, N, followed by report number, every report M, kill that person, find the last winner of numbers.
    • Recurrence formula : f ( N , M ) = ( f ( N 1 , M ) + M ) % N f(N,M) = (f(N-1,M)+M)\%N
      • f ( N , M ) f(N,M) represents, N number of individuals reported to kill that person every report M, the final winner of numbers.
      • f ( N 1 , M ) f(N−1,M) represents, N-1 personal digital newspaper, kill that person every report M, the final winner of numbers.
    • Explanation :
      • We use the figures represent 11 people in a row, assuming 3 per person report to be killed: 1 2 3 4 5 6 7 8 9 10 11 1、2、3、4、5、6、7、8、9、10、11
      • In the beginning, the first one is number 1, he began to count off from the first round is the number of people killed 3.
      • No. 4 reported that the number of people starting from 1 again, this time we can say that this one is No. 4 team head. The second round is the number of people being killed 6.
      • 7 Number of people began to count off again, this time we can say that this one is No. 7 team head. The third round is the number of people killed 9.
      • ……
      • When the ninth round, No. 2 man began to count off again, this time we can say that this one is number 2 team head. This is the number of rounds being killed 8 people.
      • The next person to person or number 2, he reported that the number starting with 1, unfortunately he was killed in this round.
      • The final winner is the number of people 7.
        As shown in the illustrated procedure :
        Here Insert Picture Description
    • Each row of the table above as an array, described this formula is: Survivors index position in this round,
      • f ( 1 , 3 ) f(1,3) : only one person, and that person is the winner, he subscript position is 0
      • f ( 2 , 3 ) = ( f ( 1 , 3 ) + 3 ) % 2 = 3 % 2 = 1 f(2,3)=(f(1,3)+3)\%2=3\%2=1 : When there are two individuals, the subscript position is 1 winner
      • f ( 3 , 3 ) = ( f ( 2 , 3 ) + 3 ) % 3 = 4 % 3 = 1 f(3,3)=(f(2,3)+3)\%3=4\%3=1 : When there are three people, and the subscript position is 1 winner
      • f ( 4 , 3 ) = ( f ( 3 , 3 ) + 3 ) % 4 = 4 % 4 = 0 f(4,3)=(f(3,3)+3)\%4=4\%4=0 : when there are four people, and the subscript position winner 0
      • ……
      • f ( 11 , 3 ) = 6 f(11,3)=6
    • Question 1 Suppose we already know 11 people, subscript position winner of six. That when the next round of 10 individuals, winner of the subscript position is how much?
    • A. After the fact it, delete the first round of the No. 3 man, after people have moved three Wang Qianmian, this victory also moved forward three, so his position by the subscript 6 becomes 3.
    • Question 2 Suppose we already know 10 people, subscript position winner of three. That when the next round of 11 individuals, winner of the subscript position is how much?
    • A. This can be wrong is the reverse of the previous question, we all moved back three, so f ( 11 , 3 ) = f ( 10 , 3 ) + 3 f(11,3)=f(10,3)+3 . However, there may be an array out of bounds, the number of the last die on the current number of people so f ( 11 , 3 ) = f ( 10 , 3 ) + 3 % 11 f(11,3)=(f(10,3)+3)\%11
    • Question 3 is now replaced by the number changed to N, when they reported M, to kill the man, then the array is how to move?
    • A. Each kill a person, the next person to be the head, which is equivalent to an array of M bits moving forward. If N-1 is known individual, bit winner subscript F (N-1, M), when the N individual, is moved backward to M, (as it is possible array bounds, the parts may be more than to the head, so even mode N), both f ( N , M ) = ( f ( N 1 , M ) + M ) % n f(N,M)=(f(N−1,M)+M)\%n

NOTE understand the recursive core concern is how subscript position is the winner of change. 每杀掉一个人,其实就是把这个数组向前移动了M位. Then reverse over, you can get this recursive formula. Because the calculated result is an array 下标, the final number also 加1.

Code

# -*- coding:utf-8 -*-
class Solution:
    def LastRemaining_Solution(self, n, m):
        # write code here
        if n<1 or m<1:
            return -1
        
        last = 0
        for i in range(2, n+1):
            last = (last + m) % i
            
        return last
Published 219 original articles · won praise 85 · Views 140,000 +

Guess you like

Origin blog.csdn.net/z_feng12489/article/details/103612254