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 ;
}

UVALive3882-And Then There Was One-约瑟夫问题-递推的更多相关文章

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

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

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

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

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

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

  4. 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...

  5. hdu 3089 (快速约瑟夫环)

    Josephus again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. [CSP-S模拟测试60]题解

    回去要补一下命运石之门了…… A.嘟嘟噜 给定报数次数的约瑟夫,递推式为$ans=(ans+m)\% i$. 考虑优化,中间很多次$+m$后是不用取模的,这种情况就可以把加法变乘法了.问题在于如何找到 ...

  7. HDU 3089 (快速约瑟夫环)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...

  8. hiho一下 第九十四周 数论三·约瑟夫问题

    数论三·约瑟夫问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个 ...

  9. 约瑟夫问题-Josephus--及实例说明

    //---我保证所有的代码都已经通过测试---// 类似约瑟夫的问题又称为约瑟夫环.又称“丢手绢问题”. 这个问题来自于这样的一个关于著名犹太历史学家 Josephus传说: 在罗马人占领乔塔帕特后, ...

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

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

随机推荐

  1. C 函数参数 char **s与char *s[]

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/126 先来看一个小例子 : 编写函数遍历一个整型数组的元素,数组 ...

  2. bzoj 4765: 普通计算姬

    Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能计算数列区间和,而普通计算姬能计算树中 ...

  3. jingtai ip

    BOOTPROTO=staticONBOOT=yesIPADDR=192.168.1.109NETMASK=255.255.255.0GATEWAY=192.168.1.1 HWADDR=00:e0: ...

  4. MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN

    ALTER COLUMN 语法: ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 作用: 设置或删除列的默认值.该操作会直接修 ...

  5. Nginx集群之WCF分布式消息队列

    目录 1       大概思路... 1 2       Nginx集群之WCF分布式消息队列... 1 3       MSMQ消息队列... 2 4       编写WCF服务.客户端程序... ...

  6. Python爬虫——城市公交、地铁站点和线路数据采集

    本篇博文为博主原创,转载请注明. 城市公交.地铁数据反映了城市的公共交通,研究该数据可以挖掘城市的交通结构.路网规划.公交选址等.但是,这类数据往往掌握在特定部门中,很难获取.互联网地图上有大量的信息 ...

  7. 标准模式 怪异模式 盒模型 doctype

    在页面顶部设置 doctype是为了统一标准 浏览器有标准模式和怪异模式 而这两种模式最大区别就是 盒模型的解析不同 ============================== 图片摘自网络 === ...

  8. SQL Server 禁用扩展存储过程

    概述 扩展存储过程是 SQL Server 实例可以动态加载和运行的 DLL.扩展存储过程是使用 SQL Server 扩展存储过程 API 编写的,可直接在 SQL Server 实例的地址空间中运 ...

  9. Java中ArrayList与LinkedList的区别

    Java中ArrayList与LinkedList的区别 一般大家都知道ArrayList和LinkedList的区别: 1. ArrayList的实现是基于数组,LinkedList的实现是基于双向 ...

  10. 一个Flask运行分析

    当我们安装好Flask环境之后,创建好项目,就会自动生成下面这段代码: from flask import Flask app = Flask(__name__) @app.route('/') de ...