HDU 2925 Musical Chairs(约瑟夫环问题)
来源:互联网 发布:数据共享的基础是什么 编辑:程序博客网 时间:2024/06/08 06:49
Musical Chairs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1201 Accepted Submission(s): 689
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 .
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.
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.
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
【思路分析】
该题是一个典型的约瑟夫环问题,可以用循环链表解决,我试了一下循环队列,可惜TLE。。。
着重提一下更简洁的数学表达式:
f[1] = 0;
f[i] = (f[i - 1] + m) % i;(i > 1)
其中f[i]表示i个人按顺序数到第m个出局的最后胜利者的编号(按0~(i-1)编号),推导如下:
假设有n个人从0~n-1编号,从0开始报数,到数为m-1时该人出局,即该人的编号k-1 = (m % n) - 1,则剩下的编号依次为: k,k + 1,k + 2,.....,n - 1/0,1,2,......,k - 2的n - 1个人又重新构成了新的小一个规模的约瑟夫环,注意到前一段编号的通项为(k + i),其中i的取值依次为0~(n - k - 1);后一段编号的通项为j,j的取值依次为0~(k - 2)。同时对上述编号重新简化:0,1,2,......,n - k + 1/,.......,n-2。设n个人,n - 1个人的最后胜利者为res1,res2,则res1 = (res2 + k) % n
代码如下
5 3 47 4 2
【思路分析】
该题是一个典型的约瑟夫环问题,可以用循环链表解决,我试了一下循环队列,可惜TLE。。。
着重提一下更简洁的数学表达式:
f[1] = 0;
f[i] = (f[i - 1] + m) % i;(i > 1)
其中f[i]表示i个人按顺序数到第m个出局的最后胜利者的编号(按0~(i-1)编号),推导如下:
假设有n个人从0~n-1编号,从0开始报数,到数为m-1时该人出局,即该人的编号k-1 = (m % n) - 1,则剩下的编号依次为: k,k + 1,k + 2,.....,n - 1/0,1,2,......,k - 2的n - 1个人又重新构成了新的小一个规模的约瑟夫环,注意到前一段编号的通项为(k + i),其中i的取值依次为0~(n - k - 1);后一段编号的通项为j,j的取值依次为0~(k - 2)。同时对上述编号重新简化:0,1,2,......,n - k + 1/,.......,n-2。设n个人,n - 1个人的最后胜利者为res1,res2,则res1 = (res2 + k) % n
代码如下
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;int main(){ int n,d; while(scanf("%d %d",&n,&d) != EOF && (n || d)) { printf("%d %d ",n,d); int f = 0; for(int i = 2;i <= n;i++) { f = (f + d) % i; } printf("%d\n",f + 1); } return 0;}
0 0
- HDU 2925 Musical Chairs(约瑟夫环)
- HDU 2925 Musical Chairs(约瑟夫环问题)
- HDU 2925 Musical Chairs(约瑟夫环问题)
- 约瑟夫环hdu 2925 Musical Chairs
- HDU 2925Musical Chairs 约瑟夫环
- Musical Chairs(约瑟夫问题高效算法)
- hdu2925 Musical Chairs(约瑟夫问题)
- HDU-2925Musical Chairs
- HDU 2925 Musical Chairs
- HDU 2925 Musical Chairs
- bnuoj Musical Chairs 约瑟夫环非递归
- hdu2925 Musical Chairs && poj3517 And Then There Was One(约瑟夫环)
- joj 2569: Musical Chairs (约瑟夫环 数学方法非迭代)
- 杭电 hdu 2925 Musical Chairs 递推公式 和 容器
- Sicily 1635. Musical Chairs
- iOS开发资源:WSCoachMarksView、Musical Chairs、MBTileParser以及Chat H
- hdu 4841 圆桌问题(用vector模拟约瑟夫环)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- H5页面,通配设置
- Ubuntu mongodb 安装 开发测试
- Linux磁盘分区简介及分区
- django-mysql 中的金钱计算事务处理
- iPad开发强制横屏
- HDU 2925 Musical Chairs(约瑟夫环问题)
- SuperMap iObjects .NET 制作2.5D数据详解(一)
- 杭电1022 Train Problem I
- 非常不错的NoSQL 数据库学习资料汇总
- 单兵应用行业
- hpu 1022&&nyoj 150 Train Problem I 【栈】
- hdu 1019 least common multiple
- Android 开发笔记——通过 Intent 传递类对象
- android app系统签名以及adb安装卸载等简单命令使用