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. PatentTips - Hierarchical RAID system including multiple RAIDs

    BACKGROUND OF THE INVENTION The present invention relates to a storage system offering large capacit ...

  2. ubuntu14.04 配置网络

    ubuntu14.04 配置网络的练习 本文参考的资料: https://blog.csdn.net/liu782726344/article/details/52912797. 感谢作者的分享! 打 ...

  3. wait和waitpid函数

    来源:http://hohahohayo.blog.163.com/blog/static/120816010200971210230362/ wait(等待子进程中断或结束)表头文件     #in ...

  4. Remove Duplicates from Sorted List (链表)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  5. Servlet的调试

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/debugging.html: 测试/调试Servlet始终是困难的.Servlets往往涉及大量 ...

  6. springboot application.properties

    verify if you have this items: @Bean public CommonsMultipartResolver multipartResolver() { CommonsMu ...

  7. Python学习系列之面向对象

    概述 一.Python编程方式 面向过程编程:根据业务逻辑从上到下磊代码 面向函数编程:将某功能代码封装到函数中,将来直接调用即可,无需重新写 面向对象编程:对函数进行分类.封装 二.面向过程编程 w ...

  8. 5 shell命令之tr

    这是一个奇妙的命令. tr的全拼就是translate,即翻译.有趣的是我们能够制定规则进行翻译.使用方法例如以下: tr [option] set1  [set2] tr从标准输入接受输入.然后将结 ...

  9. vSphere,ESXi,vCenter之间的关系

    vSphere是什么? vSphere 是VMware公司公布的一整套产品包,包括类似于VMware ESXi hypervisor.VMware vCenter Server等产品 ESXi是什么? ...

  10. java7新特性之Try-with-resources (TWR)

    java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...