hdu2925 约瑟夫环
来源:互联网 发布:思途旅游cms源码 编辑:程序博客网 时间:2024/06/07 08:25
Musical Chairs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1417 Accepted Submission(s): 808
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
5 3 47 4 2
思路:裸题,f(n)=(f(n-1)+m)%n ,编号1~n,最后值+1;
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define max_n 1000010 typedef long long LL;using namespace std;int main(){int n,m;while(scanf("%d %d",&n,&m) && n+m){int last=0;for(int i=1;i<=n;i++)last=(last+m)%i;printf("%d %d %d\n",n,m,last+1);}return 0;}
1 0
- hdu2925 约瑟夫环
- hdu2925 Musical Chairs && poj3517 And Then There Was One(约瑟夫环)
- hdu2925 Musical Chairs(约瑟夫问题)
- 约瑟夫问题、约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 约瑟夫环
- 7 天打造前端性能监控系统
- 主成分分析(PCA)原理详解
- Discuz!开发之将论坛首页设置为某个版块页面的方法
- HDU 1034 Candy Sharing Game
- POJ 1182 食物链
- hdu2925 约瑟夫环
- 黑马程序员 十六、 类加载器、代理
- org.hibernate.MappingException: Could not determine type for:
- leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405241b0 that was originally ad
- Backpack 解题报告 背包问题深入浅出
- 汇编作业
- 迟
- phpstorm 出现多个光标(phpstorm’即点即输’)功能禁用
- 【java】二分查找 求下界 求上界