Josephus Recursion Solution

problem

People are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed.
The problem ( given the number(n) of people and the number(m) to be skipped ) is to choose the position in the initial circle to avoid execution.

solution

Now, we assume that the survival is the number f ( n , m ) f(n,m) f(n,m). And the array of people is 0 , 1 , 2 , 3 , 4 , . . . n − 1 0, 1, 2, 3, 4, ... n-1 0,1,2,3,4,...n1. So the m − 1 m-1 m1 die at first. Then we mark the people anew:
m → 0 , m + 1 → 1 , . . . . . . , n − 1 → n − m − 1 , 0 → n − m , . . . . . . , m − 2 → n − 2 m \rightarrow 0,m+1\rightarrow 1,......,n-1\rightarrow n-m-1,0\rightarrow n-m,......,m-2\rightarrow n-2 m0,m+11,......,n1nm1,0nm,......,m2n2.
Because survival’s position doesn’t change, its sequence number changes to ( f ( n , m ) − m ) % n (f(n, m)-m)\%n (f(n,m)m)%n. And we will count from the next of the victim (now its sequence number is 0 0 0), So the winner will be the position as before.Therefore the survival’s sequence number of n − 1 n-1 n1 individual f ( n − 1 , m ) = ( f ( n , m ) − m ) % n f(n-1, m) = (f(n, m)-m)\%n f(n1,m)=(f(n,m)m)%n.
Finally, f ( n , m ) = ( f ( n − 1 , m ) + m ) % n f(n, m) = (f(n-1, m)+m)\%n f(n,m)=(f(n1,m)+m)%n.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值