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


Problem Description
In the traditional game of Musical Chairs, N + 1 children run around N chairs (placed in a circle) as long as music is playing. The moment the music stops, children run and try to sit on an available chair. The child still standing leaves the game, a chair is removed, and the game continues with N children. The last child to sit is the winner.

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 .
 

Input
Your program will be tested on one or more test cases. Each test case specifies two positive integers N and D on a single line, separated by one or more spaces, where N, D < 1, 000, 000 .

The last line of the input file contains two 0's and is not part of the test cases.
 

Output
For each test case, write the winner using the following format:


N D W


Where N and D are as above, is a space character, and W is the winner of that game.
 

Sample Input
5 37 40 0
 

Sample Output
5 3 47 4 2
 

Source
2008 ANARC
 

Recommend
lcy

暂时理解的不是很透彻,看看大神的题解吧。

题意:      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;}



原创粉丝点击