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

本文介绍了约瑟夫环问题,当每第2个人被处决时,通过递归公式分析求解问题。当人数为2的幂时,幸存者位置为奇数;通过动态规划方法解决一般情况,运行时间为O(n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 k\neq 2, 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:

f(2n)=2f(n)-1.\,

         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:

f(2n+1)=2f(n)+1.\,

         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 0\leq l<2^m, then f(n)=2 \cdot l+1. 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 0\leq l<2^m, 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 n/2 = 2^{m_1}+l_1 and 0\leq l_1 < 2^{m_1}. 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 (n-1)/2 = 2^{m_1}+l_1 and 0\leq l_1 < 2^{m_1}. 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:

                f(n,k)=(f(n-1,k)+k) \bmod n,\text{ with }f(1,k)=1,\,

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),



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值