G - And Then There Was One (约瑟夫环变形)
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 (约瑟夫环变形)的更多相关文章
- 【约瑟夫环变形】UVa 1394 - And Then There Was One
首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...
- Poj 3517 And Then There Was One(约瑟夫环变形)
简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...
- HDU 5643 King's Game | 约瑟夫环变形
经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- F - System Overload(约瑟夫环变形)
Description Recently you must have experienced that when too many people use the BBS simultaneously, ...
- tc 147 2 PeopleCircle(再见约瑟夫环)
SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...
- LightOJ - 1179 Josephus Problem(约瑟夫环)
题目链接:https://vjudge.net/contest/28079#problem/G 题目大意:约瑟夫环问题,给你n和k(分别代表总人数和每次要数到k),求最后一个人的位置. 解题思路:因为 ...
- hdu 4841 圆桌问题(用vector模拟约瑟夫环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4841 圆桌问题 Time Limit: 3000/1000 MS (Java/Others) M ...
- POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)
题意 n个人顺时针围成一圈玩约瑟夫游戏 每一个人手上有一个数val[i] 開始第k个人出队 若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人 val[k ...
随机推荐
- 2018/2/18 SpringCloud Eureka的学习和spirng ribbon的部分源码追踪
昨天对于Eurake大致做了一个介绍,今天就来说说具体怎么配置和使用吧. 首先,我们创建一个服务注册中心 这是它的配置文件 注意,因为我等下还会弄一个Eureka注册中心,所以这里service-ur ...
- 【NOIP2017练习&BZOJ4998】星球联盟(强联通分量,并查集)
题意: 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流. 但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...
- POJ 3281_Dining
题意: FJ准备了F种食物和D种饮料,每头牛都有喜欢的食物和饮料,并且每头牛都只能分配一种食物和饮料.问如何分配使得同时得到喜欢的食物和饮料的牛数量最多. 分析: 首先想到将牛与其对应的食物和饮料匹配 ...
- JSP基础教程:tutorialspoint-jsp
来自turorialspoint的JSP基础教程(英文),官网:https://www.tutorialspoint.com/jsp/index.htm 这个教程在国内已经被翻译成中文(不过是属于机器 ...
- c++11中的线程、锁和条件变量
void func(int i, double d, const string& s) { cout << i << ", " << d ...
- 踩坑录- Spring Boot - CORS 跨域 - 浏览器同源策略
1.解决办法,创建一个过滤器,处理所有response响应头 import java.io.IOException; import javax.servlet.Filter; import javax ...
- Maven+Mybatis+Spring+SpringMVC实现分页查询(附源代码)
以下小宝鸽将分享一篇Mybatis分页功能的博文,以下将给出具体的步骤.跟着博主的节奏肯定能实现.另外最后还会附上整个project的源代码.假设是没有使用过maven的猿友可自行下载相关的jar包就 ...
- PHP + Socket 发送http请求进而实现站点灌水
本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...
- 网站访问分析对SEO的好处
统计剖析,应该说是每个SEO都必需要擅长的技艺.至于网站统计的剖析,根据自己的一些经验,与大家分享一下相关技巧.(发表于 2012-3-24 23:12) 申请一个统计帐号很容易,现在有很多的统计服务 ...
- 【bzoj4592】[Shoi2015]脑洞治疗仪
由于脑洞的序列不会改变,考虑用线段树维护区间内sum,左边0的个数,右边0的个数,区间内最大脑洞.对于查询l~r最大脑洞可以将l~r分成logn个区间,总复杂度O(nlogn). #include&l ...