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 khops 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
解题思路:有n个石头围成一圈,第一次移走第m个石头,然后从第m+1个石头从1开始数,以后每次数到k就移走一个石头,第k+1个石头又从1开始数,依此规律重复下去,求最后一个移走的石头编号。做法:假设编号为0~n-1的n个石头围成一圈,从0开始每k个石头移走一个,最后留下的编号记为f[n]。因为第一次移走第k-1个石头是从第0个石头开始数的,而第一次移走第m-1个石头是从第m-k个石头开始数的,所以只需将原来0~n-1重新编号后可以得到:最终剩下一个石头的编号为(f[n]+m-k)%n,因为f[n]加上偏移量后可能为负或者超过n,所以最后应该加上n再取模n,即((f[n]+m-k)%n+n)%n,这就是第一次移走第m个石头的最终结果。
AC代码:
 #include<iostream>
#include<cstdio>
using namespace std;
int n,k,m,s;
int main(){
while(~scanf("%d%d%d",&n,&k,&m)&&(n+k+m)){
s=;//只有一个石头,移走的编号为0
for(int i=;i<=n;++i)s=(s+k)%i;
s=((s+m-k+n)%n+n)%n;
cout<<(s+)<<endl;//因为计算是从0开始的,所以最终的编号要加1
}
return ;
}
												

G - And Then There Was One (约瑟夫环变形)的更多相关文章

  1. 【约瑟夫环变形】UVa 1394 - And Then There Was One

    首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...

  2. Poj 3517 And Then There Was One(约瑟夫环变形)

    简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...

  3. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  4. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  5. F - System Overload(约瑟夫环变形)

    Description Recently you must have experienced that when too many people use the BBS simultaneously, ...

  6. tc 147 2 PeopleCircle(再见约瑟夫环)

    SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...

  7. LightOJ - 1179 Josephus Problem(约瑟夫环)

    题目链接:https://vjudge.net/contest/28079#problem/G 题目大意:约瑟夫环问题,给你n和k(分别代表总人数和每次要数到k),求最后一个人的位置. 解题思路:因为 ...

  8. hdu 4841 圆桌问题(用vector模拟约瑟夫环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4841 圆桌问题 Time Limit: 3000/1000 MS (Java/Others)    M ...

  9. POJ 2886 Who Gets the Most Candies?(线段树&#183;约瑟夫环)

    题意  n个人顺时针围成一圈玩约瑟夫游戏  每一个人手上有一个数val[i]   開始第k个人出队  若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人   val[k ...

随机推荐

  1. 洛谷P1186 玛丽卡

    题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...

  2. 一练Splay之维修数列第一次

    平衡树并不是之前没写过,觉得有必要把平衡树变成考场上能敲的东西,也就是说,考一道诸如“维修数列”这样的送分题,要能拿满分. 维修数列.给定一个数列支持以下操作: 输入的第1 行包含两个数N 和M(M ...

  3. Linux下汇编语言学习笔记40 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  4. codevs 3164 质因数分解

    3164 质因数分解  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description (多数据)给出t个数,求出它的质因子个 ...

  5. Redis的集群方案之Sentinel(哨兵模式)(待实践)

    哨兵模式是主从切换的一种方案,但是可以借助此方案实现集群,达到高可用. 先收集教程,待实践. 参考: https://redis.io/topics/sentinel(官方文档) http://ife ...

  6. Linux下搭建maven私服Nexus 3.2.1-01

    1. 私服介绍私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构建.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则, ...

  7. JSTL简单入门学习实例

    Maven依赖: <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</ ...

  8. for in、for和EnumerateObjectsUsingBlock遍历的区别

    1.对于集合中对象数很多的情况下,for in 的遍历速度非常之快,但小规模的遍历并不明显(还没普通for循环快) 2. 如果在for in 循环里,对这个数组进行了修改的话,无论是增,删,修改数组元 ...

  9. 关于使用Xshell远程连接启动tomcat导致图片不显示,报错Could not initialize class sun.awt.X11GraphicsEnvironment解决方案

    如果您是使用xshell远程启动tomcat,导致二维码.验证码,头像等各种图片不显示,并且打开图片链接报错Could not initialize class sun.awt.X11Graphics ...

  10. ZOJ 1806 (小数高精度)

    题意:八进制小数转化成十进制的小数. 0.d1d2d3 ... dk [8] = 0.D1D2D3 ... Dm [10] 例: 0.75 [8] = 7*8^-1+5*8^-2 = ( 5/8 + ...