POJ 1012 Joseph 题解

POJ 1012 Joseph 题解

题目解读:

The Joseph’s problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, …, n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

本道题目的背景是约瑟夫环问题。约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3,4…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依次规律重复下去,直到圆桌周围的人全部出列。通常解决这类问题时我们把编号从0—n-1,最后结果+1即为原问题的解。
在本道题中对约瑟夫环问题作了一个简单的变形,通过求解最小的报数m,来使得所有的bad guy先于good guy被淘汰。

解决算法

我们在这里假设环的编号从0开始,以下的计算与公式推导均满足这一点。
注意观察到报数m具有延续性,对于n个人而言,第一轮约瑟夫环淘汰了一个人后,剩下的n-1个人其实又组成了一个新的约瑟夫环,唯一区别的就是两个约瑟夫环中的成员编号是不同的。因此,我们可以理解为这里需要解决的是不同编号序列之间的一个映射关系,即将第i+1轮中被淘汰的人的编号映射到第i轮中对应的编号序列。这与动态规划思想中状态转移的思想有相近的地方,因此考虑使用DP算法。现在先给出状态转移方程:

ans[0]
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值