HDU 2925Musical Chairs 约瑟夫环
来源:互联网 发布:炼数成金和黑马程序员 编辑:程序博客网 时间:2024/06/14 10:55
Musical Chairs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 798 Accepted Submission(s): 455
In an attempt to create a similar game on these days' game consoles, you modify the game in the following manner: N Children are seated on N chairs arranged around a circle. The chairs are numbered from 1 to N . Your program pre-selects a positive number D . The program starts going in circles counting the children starting with the first chair. Once the count reaches D , that child leaves the game, removing his/her chair. The program starts counting again, beginning with the next chair in the circle. The last child remaining in the circle is the winner.
For example, consider the game illustrated in the figure above for N = 5 and D = 3 . In the figure, the dot indicates where counting starts and × indicates the child leaving. Starting off, child #3 leaves the game, and counting restarts with child #4. Child #1 is the second child to leave and counting restart with child #2 resulting in child #5 leaving. Child #2 is the last to leave, and child #4 is the winner. Write a program to determine the winning child given both N and D .
The last line of the input file contains two 0's and is not part of the test cases.
N D W
Where N and D are as above, is a space character, and W is the winner of that game.
5 37 40 0
5 3 47 4 2
题意: N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的人的序号为5,4,6,2,3。最后剩下1号。
问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,
剩下的人继续从0开始报数。求胜利者的编号。
我们知道第一个人(编号一定是m-1 mod n) 出列之后,
剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m mod n的人开始):
k k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2 并且从k开始报0。
现在我们把他们的编号做一下转换: k --> 0
k+1 --> 1
k+2 --> 2
... ...
k-2 --> n-2
k-1 --> n-1
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:
例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!
变回去的公式很简单,相信大家都可以推出来:x'=(x+k) mod n 如何知道(n-1)个人报数的问题的解?
对,只要知道(n-2)个人的解就行了。(n-2)个人的解呢?
当然是先求(n-3)的情况 ---- 这显然就是一个倒推问题!好了,思路出来了,下面写递推公式:
令f表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f[n]
递推公式 f[1]=0; f=(f+m) mod i; (i>1)
有了这个公式,我们要做的就是从1-n顺序算出f的数值
,最后结果是f[n]。因为实际生活中编号总是从1开始,我们输出f[n]+1
#include<stdio.h>int main(){ int n,d; while(scanf("%d%d",&n,&d),n|d) { int s=0; for(int i=2;i<=n;i++) { s=(s+d)%i; } printf("%d %d %d\n",n,d,s+1); } return 0;}
- 约瑟夫环hdu 2925 Musical Chairs
- HDU 2925Musical Chairs 约瑟夫环
- HDU 2925 Musical Chairs(约瑟夫环)
- HDU 2925 Musical Chairs(约瑟夫环问题)
- HDU 2925 Musical Chairs(约瑟夫环问题)
- HDU-2925Musical Chairs
- HDU 2925 Musical Chairs
- HDU 2925 Musical Chairs
- bnuoj Musical Chairs 约瑟夫环非递归
- joj 2569: Musical Chairs (约瑟夫环 数学方法非迭代)
- Musical Chairs(约瑟夫问题高效算法)
- hdu2925 Musical Chairs(约瑟夫问题)
- hdu2925 Musical Chairs && poj3517 And Then There Was One(约瑟夫环)
- 杭电 hdu 2925 Musical Chairs 递推公式 和 容器
- Sicily 1635. Musical Chairs
- iOS开发资源:WSCoachMarksView、Musical Chairs、MBTileParser以及Chat H
- HDU 约瑟夫环
- HDU Joseph【数学&&约瑟夫环】
- Swing可视化开发工具
- start with.....connect by prior oracle中取出树形结构
- Base64编码
- ArcGIS Server中的各种服务
- Oracle 应用 删除重复数据
- HDU 2925Musical Chairs 约瑟夫环
- cocos2d-x 屏幕自适应解决办法
- 10大最实用的Raspberry Pi扩展设备推荐
- Java 理论与实践: 正确使用 Volatile 变量
- 成为算法达人1
- SecureCRT连接服务器断开连接
- git tag 相关操作
- 匈牙利命名 变量属性
- Jquery 插件的图片效果