And Then There Was One
http://poj.org/problem?id=3517
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 4805 | Accepted: 2546 |
Description
Let’s play a stone removing game.
Initially, n stones are arranged on a circle and numbered 1, …, n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m. In step 2, locate the k-th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k − 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1.
Initial state |
Step 1 |
Step 2 |
Step 3 |
Step 4 |
Step 5 |
Step 6 |
Step 7 |
Final state |
Figure 1: An example game
Initial state: Eight stones are arranged on a circle.
Step 1: Stone 3 is removed since m = 3.
Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8.
Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case.
Steps 4–7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7.
Final State: Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1.
Input
The input consists of multiple datasets each of which is formatted as follows.
n k m
The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions.
2 ≤ n ≤ 10000, 1 ≤ k ≤ 10000, 1 ≤ m ≤ n
The number of datasets is less than 100.
Output
For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output.
Sample Input
8 5 3
100 9999 98
10000 10000 10000
0 0 0
Sample Output
1
93
2019
#include"string.h"
int f[];
int main()
{
int n,k,m,i,j;
while(scanf("%d%d%d",&n,&k,&m)==&&n)
{
f[]=;
for(i=;i<n;i++) //重新编号,即编号映射
f[i]=(f[i-]+k)%i;
f[n]=(f[n-]+m)%n;
printf("%d\n",f[n]+);
}
return ;
}
随机推荐
- 叉积判断 POJ1696
// 叉积判断 POJ1696 #include <iostream> #include <algorithm> #include <cstring> #inclu ...
- Tkinter教程之Text篇(3)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811348 '''Tkinter教程之Text篇(3)''''''14.自定义tag的两个内置 ...
- cocosbuilder学习汇总
目前与cocos2d-x-2.14版本对应的cocosbuilder版本为cocosbuilder-3,目前为alpha-5.稳定版本为cocosbuilder2.1,但与cocos2d-x不匹配(C ...
- Google AppEngine 创建的例子
1.guestbook: http://chenyy-gac-test.appspot.com/ 2.time clock: http://chenyy-gac-20150922.appspot.co ...
- git使用中遇到的常见问题
.gitignore 中添加的文件不能被忽略掉 这是因为我们误解了 .gitignore 文件的用途,该文件只能作用于 Untracked Files,也就是那些从来没有被 Git 记录过的文件(自添 ...
- Struts2类型转换器
概述 A .从一个HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串. –HTTP没有"类型" 的概念.每一项表单输入只可能是一个字符串或一个字符串数组. ...
- Struts2零碎点整理
1. 关于 Struts2 请求的扩展名问题 1). org.apache.struts2 包下的 default.properties 中配置了 Struts2 应用的一些常量 2). struts ...
- HDU 5744 Keep On Movin (贪心)
Keep On Movin 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5744 Description Professor Zhang has k ...
- mysql数据库中查询时间
项目中要对数据按时间处理,在数据库中,时间处理的格式如 2014-12-09 06:30:17 时间查询出来如下所示: 现在要查询具体有哪天的数据,应用substring函数,SQL如下: ) as ...
- UVaLive 7270 Osu! Master (统计)
题意:给定 n 个元素,有的有一个值,如果是 S 那么是单独一个,其他的是一个,求从 1 开始的递增的数量是多少. 析:那么S 是单独的,要统计上,既然是从 1 开始递增的,那么再统计 1 的数量即可 ...