And Then There Was One

Time limit: 3.000 seconds

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: 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

题意就是约瑟夫问题,看人家的博客吧。

传送门1:http://www.cnblogs.com/shanyr/p/5213631.html

传送门2:http://blog.csdn.net/hyczms/article/details/43817799

公式写上就可以A了。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N];
int main(){
int n,k,m,cnt;
while(~scanf("%d%d%d",&n,&k,&m)&&n){
a[]=;
for(int i=;i<=n;i++)
a[i]=(a[i-]+k)%i;
cnt=(a[n]+m-k+)%n;
if(cnt<=) cnt+=n;
printf("%d\n",cnt);
}
return ;
}

UVALive 3882.And Then There Was One-约瑟夫问题(递推)的更多相关文章

  1. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  2. UVALive 3882 - And Then There Was One【约瑟夫问题】

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  3. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  4. LA 3882 And Then There Was One[约瑟夫问题的变形]

    And Then There Was One UVALive - 3882 Sample Input   Sample Output //设f[i]为(原约瑟夫问题)第i次要删除的标号 #includ ...

  5. LA 3882 经典约瑟夫环问题的数学递推解法

    就是经典约瑟夫环问题的裸题 我一开始一直没理解这个递推是怎么来的,后来终于理解了 假设问题是从n个人编号分别为0...n-1,取第k个, 则第k个人编号为k-1的淘汰,剩下的编号为  0,1,2,3. ...

  6. 「模拟8.23」one递推,约瑟夫

    前置芝士约瑟夫问题 这样大概就是板子问题了 考场的树状数组+二分的60分暴力??? 1 #include<bits/stdc++.h> 2 #define int long long 3 ...

  7. LA 3882 - And Then There Was One(约瑟夫 递归)

    看题传送门 题目大意: N个数排成一圈,第一次删除m,以后每k个数删除一次,求最后一被删除的数. 如果这题用链表或者数组模拟整个过程的话,时间复杂度都将高达O(nk),而n<=10000,k&l ...

  8. UVALive - 3882:And Then There Was One

    约瑟夫环 f[i]表示有i个人先处理第k个人,最后被处理的人是谁 #include<cstdio> #include<cstdlib> #include<algorith ...

  9. UVALive - 6577 Binary Tree 递推+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48421 Binary Tree Time Limit: 3000MS 问题描述 Binary Tree is ...

随机推荐

  1. 【bzoj1452】[JSOI2009]Count 二维树状数组

    题目描述 输入 输出 样例输入 样例输出 1 2 题解 二维树状数组 一开始没看到 1≤c≤100 ,想到了主X树和X块,结果发现c的范围那么小... 二维树状数组水题,和一维的一样,向上修改,向下查 ...

  2. DataTable定义

    DataTable是一个临时保存数据的网格虚拟表(表示内存中数据的一个表.).DataTable是ADO dot net 库中的核心对象.它可以被应用在 VB 和 ASP 上.它无须代码就可以简单的绑 ...

  3. Mac Java配置JAVA——HOME

    命令行中输入:  export JAVA_HOME=$(/usr/libexec/java_home) 

  4. [洛谷P4722]【模板】最大流 加强版 / 预流推进

    会$TLE$... C++ Code:(HLPP) #pragma GCC optimize(3) #pragma GCC optimize("unroll-loops") #in ...

  5. [Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. 用boost::lexical_cast进行数值转换

    在STL库中,我们可以通过stringstream来实现字符串和数字间的转换: int i = 0;    stringstream ss; ss << "123";  ...

  7. Substrings Sort string 基本操作

    You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...

  8. 使用mysqldump命令备份恢复MySQL数据库

    1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump file] 上述命令将指定数据库备份到某dump文 ...

  9. (转)Django常用命令

    转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .

  10. js实现快速排序的方法

    因为面试面到了这个问题,所以写一下,加深印象,有两种方法 第一种是通过两个for循环,每一次对比相邻两个数据的大小,小的排在前面,如果前面的数据比后面的大就交换这两个数的位置,这个方法就是比较次数太多 ...