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 ;
}
随机推荐
- 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence
// 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...
- 进程通信之一 使用WM_COPYDATA C++及C#实现(转)
进程间通信最简单的方式就是发送WM_COPYDATA消息.本文提供C++及C#程序相互通信的二种实现方式.这样消息的接收端可以用C++实现,发送端可以用C++或C#实现. 发送WM_COPYD ...
- linux shell 实现node-webkit的自动跨平台打包
今天下午发现了个好玩的东西(node-webkit),这东西有一直是我想实现的功能:使用html编写桌面应用,实现跨平台: 具体实现方法:结合chrome浏览器内核和node.js搭建一个跨平台的应用 ...
- [HIve - LanguageManual] Transform [没懂]
Transform/Map-Reduce Syntax SQL Standard Based Authorization Disallows TRANSFORM TRANSFORM Examples ...
- 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子
一:自定义实现InputFormat *数据源来自于内存 *1.InputFormat是用于处理各种数据源的,下面是实现InputFormat,数据源是来自于内存. *1.1 在程序的job.setI ...
- JavaScript——对this指针的新理解
一直以来对this的理解只在可以用,会用,却没有去深究其本质.这次,借着<JavaScript The Good Parts>,作了一次深刻的理解.(所有调试都可以在控制台中看到,浏览器F ...
- Spark SQL应用
Spark Shell启动后,就可以用Spark SQL API执行数据分析查询. 在第一个示例中,我们将从文本文件中加载用户数据并从数据集中创建一个DataFrame对象.然后运行DataFrame ...
- C:移位运算符
1在向右移位时,空出的位是由0填充,还是由符号位的副本填充? 如果被移位的对象是无符号数,那么空出的位将被0填充.如果被位移的对象是有符号数,那么C语言实现既可以用0填充空出的位,也可以用符号位的副本 ...
- ASP.NET中身份验证的三种方法
Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活.Forms 验证方式对基于用户的验证授权 ...
- 转载 SQL Server中索引管理之六大铁律
转载原地址 http://jingyan.baidu.com/article/48a42057c03bd7a924250429.html 索引是以表列为基础的数据库对象.索引中保存着表中排序的索引列, ...