UVALive3882-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-约瑟夫问题-递推的更多相关文章
- Joseph POJ - 1012 约瑟夫环递推
题意:约瑟夫环 初始前k个人后k个人 问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...
- LA 3882 经典约瑟夫环问题的数学递推解法
就是经典约瑟夫环问题的裸题 我一开始一直没理解这个递推是怎么来的,后来终于理解了 假设问题是从n个人编号分别为0...n-1,取第k个, 则第k个人编号为k-1的淘汰,剩下的编号为 0,1,2,3. ...
- 「模拟8.23」one递推,约瑟夫
前置芝士约瑟夫问题 这样大概就是板子问题了 考场的树状数组+二分的60分暴力??? 1 #include<bits/stdc++.h> 2 #define int long long 3 ...
- 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...
- hdu 3089 (快速约瑟夫环)
Josephus again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [CSP-S模拟测试60]题解
回去要补一下命运石之门了…… A.嘟嘟噜 给定报数次数的约瑟夫,递推式为$ans=(ans+m)\% i$. 考虑优化,中间很多次$+m$后是不用取模的,这种情况就可以把加法变乘法了.问题在于如何找到 ...
- HDU 3089 (快速约瑟夫环)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...
- hiho一下 第九十四周 数论三·约瑟夫问题
数论三·约瑟夫问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个 ...
- 约瑟夫问题-Josephus--及实例说明
//---我保证所有的代码都已经通过测试---// 类似约瑟夫的问题又称为约瑟夫环.又称“丢手绢问题”. 这个问题来自于这样的一个关于著名犹太历史学家 Josephus传说: 在罗马人占领乔塔帕特后, ...
- UVALive 3882 - And Then There Was One【约瑟夫问题】
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- iOS 视频开发学习
原文:浅谈iOS视频开发 这段时间对视频开发进行了一些了解,在这里和大家分享一下我自己觉得学习步骤和资料,希望对那些对视频感兴趣的朋友有些帮助. 一.iOS系统自带播放器 要了解iOS视频开发,首先我 ...
- PDO绑定含IN的SQL语句的参数注意事项
PDOStatement::bindParam(),表示将PDO::prepare()语句中的占位符用参数替换掉.注意,在绑定含有IN的SQL多参数语句时要额外小心,比如$stmt = $db-> ...
- Python数据分析工具
1.Numpy 安装:pip install numpy [root@kvm work]# cat numpy_test.py #!/usr/bin/env python #coding:utf-8 ...
- Design Patterns笔记
一些笔记. strategy : facilitates the switch of the different but related algorithms/behaviors observer p ...
- WebDriver API 大全
访问某网页地址:driver.get(url) 或 driver.navigate().to(url) 访问上一个访问的网页(模拟单击浏览器的后退按钮)driver.navigate().back ...
- 阿里云Centos7 apache配置
其实很简单,主要是有坑. 首先填坑,在阿里云安全策略上开放要访问的端口,然后配置firewall添加对应端口开放. firewall-cmd --zone=public --add-port=8011 ...
- Android studio Error:org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to
http://blog.csdn.net/FlyRabbit_1/article/details/74536317 Error:org.gradle.api.internal.tasks.Defaul ...
- thinkinginjava学习笔记02_对象
对象 1. 对象通过一个引用来操作,但是java中的对象是按值传递的,基本上可以在操作中认为对象本身,在内部结构中仍然要记得是对象实体的引用:如:String s = "abcd" ...
- \Process(sqlservr)\% Processor Time 计数器飙高
计数器" \Process(sqlservr)\% Processor Time",是经常监测,看看SQL Server如何消耗CPU资源.sqlserver是如何利用现有的资源; ...
- python字符集的转换(mysql数据乱码的处理)
本文参考:http://blog.csdn.net/crazyhacking/article/details/39375535 chardet模块:http://blog.csdn.net/tianz ...