• The Josephus problem


    Legend has it that Josephus wouldn't have lived to become famous without his mathematical talents. During the Jewish-Roman war, he was among a band of 41 Jerish rebels trapped in a cave by Romans. Preferring suicide to capture, the rebels decide to form a circle, and proceeding around it, to kill every third remaining person until no one was left. But Josephus, among with an unindicted co-conspirator, wanted none of this suicide nonsense; so he quickly calculated where he and his friend should stand in the vicious circle.

    In our variation, we start with  n people numbered 1 to n around a circle, and we eliminate every second remaining person until ONLY one survives. For example, here's the starting configuration for n=10.

    The elimination order is 2, 4, 6, 8, 10, 3, 7, 1, 9, so 5 survives. The problem: determine the survivor's number J(n).

    Suppose that we have 2n people originally. After the first goround, we are left with 1, 3, 5, 7, ... , 2n-1. and 3 will be the next to go. this is just like starting out with n people, except that each person's number has been doubled and decreased by 1. That is,

    J(2n)=2J(n)-1,n geq 1

    But what about the odd case? with 2n+1 people, it turns out that person number 1 is wiped out just after person number 2n, and we are left with 3, 5, 7, 9, ..., 2n+1

    Again we almost have teach original situation with n people, But this time their number is doublede and increased by 1. Thus 

    J(2n+1)=2J(n)+1,n geq 1

    Noticing that J(1)=1.

    Now we seek a closed form, because that will be even quicker and more informative. After all, that is a matter of life or death.

    Our recurrence makes it possible to build a table of small values very quickly. Perhaps we'll be able to splot a pattern and guess the answer.

    1

    2

    3

    4

    5

    6

    7

    8

    1

    1

    3

    1

    3

    5

    7

    1

    It seems we can group by power of 2. J(n) is always 1 at the beginning of group and it increases by 2 within a group. So if we write n in the form n=2^m+l, where 2^m is the largest power of 2 not exceeding n and where l is what's left, the solution to our recurrence seems to be

    J(2^m+l)=2l+1,(m geq 0,0 leq l leq 2^m)

    We must now prove this function.The induction step has two parts, depending on whether l is odd or even. If m>0 and 2^m+l=2n, then l is even and

    J(2^m+l)=2J(2^{m-1}+l/2)-1=2(2l/2+1)-1=2l+1

    A similar proof works in the odd case, when 2^m+l=2n+1. We might also note that J(2n+1)-J(2n)=2.

  • 相关阅读:
    队列(queue)、优先队列(priority_queue)、双端队列(deque)
    20150720工作总结
    Spring使用远程服务之Hessian
    iBaits中SqlMapClientTemplate的使用
    java中常见的异常类
    java笔试面试中的坑
    java面试中常用的排序算法
    IBatis和Hibernate区别
    单例和多线程
    ThreadLocal
  • 原文地址:https://www.cnblogs.com/taokongcn/p/4034352.html
Copyright © 2020-2023  润新知