约瑟夫环问题(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:
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),
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:
n | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
f(n) | 1 | 1 | 3 | 1 | 3 | 5 | 7 | 1 | 3 | 5 | 7 | 9 | 11 | 13 | 15 | 1 |
, 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),
相关文章推荐
- 约瑟夫环问题两种情况解答
- 约瑟夫环问题的详细解答
- 约瑟夫循环c语言实现 (<<数据结构与算法>>-王曙燕 约瑟夫环问题解答)
- 解答约瑟夫环问题的几个方法
- 解答约瑟夫环问题的几个方法
- javascript中this做事件参数相关问题解答
- 竞赛选手问题的解答算法
- 约瑟夫环问题华为机试题
- java Synchronized一些列问题解答
- 开发者完成首款游戏前需解答的3个问题
- 关于一些 iOS 面试问题的解答
- 关于一些基础的Java问题的解答(六)
- poj3750-小孩报数问题(约瑟夫环)
- MyEclipse与sqlserver连接问题与解答详细
- leetcode 141:Linked List Cycle 问题与解答
- 约瑟夫环问题两解
- 无聊闲做,从使用PHP数组实现约瑟夫环问题谈性能
- [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.24
- Unix编程常见问题解答(FAQ / Frequently Asked Questions)(v1.37)(中文版 v0.1.0)
- 不同应用场景的10个Linux面试问题与解答