您的位置:首页 > 编程语言 > Go语言

约瑟夫环问题(1)----Wikipedia上的原文解答

2011-11-13 22:50 218 查看
Josephus Problems:

       There are people standing in a
circle waiting to be executed. After the first person is executed, a certain number of people are skipped and one person is executed. Then, again, people are skipped and a person is executed.
The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

       let n means the number of people in the cycle, k means when the people count k, then he/she is executed.

Solutions:  when k = 2 



       We explicitly solve the problem when every 2nd person will be killed, i.e. k = 2. (For the more general case


, we outline a solution below.) We express the solution recursively. Let f(n) denote the position of the survivor when there are initially
n people (and k = 2). The first time around the circle, all of the even-numbered people die. The second time around the circle, the new 2nd person dies, then the new 4th person, etc.; it's as though there were no first time around the circle. If the initial
number of people was even, then the person in position x during the second time around the circle was originally in position 2x − 1 (for every choice of x). So the person in position f(2n) was originally in position 2f(n) − 1. This gives us the recurrence:


         If the initial number of people was odd, then we think of person 1 as dying at the end of the first time around the circle. Again, during the second time around the circle, the new 2nd person dies, then the new 4th person, etc. In this case, the
person in position x was originally in position 2x + 1. This gives us the recurrence:


         When we tabulate the values of n and f(n) we see a pattern:

n12345678910111213141516
f(n)1131357135791113151
          This suggests that f(n) is an increasing odd sequence that restarts with f(n) = 1 whenever the index n is a power of 2. Therefore, if we choose m and l so that n = 2m + l and


, then


. It is clear that values in the table satisfy this equation. Or we can think that after l people are dead there are only
2m people and we go to the 2l + 1th person. He must be the survivor. So f(n) = 2l + 1. But mathematics demands exact proof. Below, we give a proof by induction.

         Theorem: If n = 2m + l and

, then f(n) = 2l + 1.

          Proof: We use
strong induction on n. The base case n = 1 is true. We consider separately the cases when n is even and when n is odd.

         If n is even, then choose l1 and m1 such that

and


. Note that l1 = l / 2. We have f(n) = 2f(n / 2) − 1 = 2((2l1) + 1) − 1 = 2l + 1, where the second equality follows from
the induction hypothesis.

         If n is odd, then choose l1 and m1 such that

and


. Note that l1 = (l − 1) / 2. We have f(n) = 2f((n − 1) / 2) + 1 = 2((2l1) + 1) + 1 = 2l + 1, where the second equality
follows from the induction hypothesis. This completes the proof.

The General Case:       

             The easiest way to solve this problem in the general case is to use

dynamic programming. This approach gives us the recurrence:

               


This can be seen from the following arguments:

When starting from position k(mod n) instead of 0, the number of the last remaining person also shifts by k positions
After the first round of the n person problem one eliminates the person on position kmod n and is left with an n − 1 person problem. The person (k + 1)mod n (in the n person case) is the first of the next k counts and thus has the label 1 in the n − 1 problem.
We must therefore shift the outcome of the n − 1 problem by (k + 1 − 1) positions to get the answer for the case with n persons.

              This approach has
running time O(n),
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  express table go