题目链接:http://poj.org/problem?id=3517

And Then There Was One
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5014   Accepted: 2685

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

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以后每次数到k个数删除一次,求最后一个被杉树的数
题解:是约瑟夫环问题的变形,距离上次学习约瑟夫环有一定时间了,上次不理解,这次理解递推的数学过程了:
当第n次,第f(n)个人出局了的时候剩下的n-1个人就构成了一个新的约瑟夫环问题,那么现在如果知道了第n-1次是第f(n-1)个人出局了,那么反着想,但是转化的时候是要从k+1个人开始计数,那么相当是吧k+1-->0;
k+2-->1.......所以在要想知道了f(n-1)想求f(n)就要用f(n) = [k+1+f(n-1)]%n;现在好了递归函数出来了那么就可以写一个普通约瑟夫环的代码:
 #include<cstdio>
using namespace std;
int main()
{
int n,m,i,s = ;
printf("N M =");//n个数,每次数m个数出列
scanf("%d%d",&n,&m);
for(i = ; i <= n; i++)
{
s = (s + m) % i;
}
printf("\n The winner is %d\n",s+);
}


这个题,要注意是从m开始计数的,但是由于递归的时候每次操作都是f(n) = (f(n-1)+k)%n;

但是实际上第一次的时候是删除掉了第m个数,而且编号是从1开始编号的,所以正常的将最后一组重新编号的时候f`(n) = [f(n-1)+m+1]%n; 所以最后答案应该是f`(n) = (m-k+1+f(n))%n;

ac代码:

 #include<cstdio>
using namespace std;
const int maxn = ;
int f[maxn]; int main()
{
int n, k, m;
while(~scanf("%d%d%d",&n,&k,&m)){
if(n==) return ;
f[] = ;
for(int i = ; i <= n; i++)f[i] = (f[i-]+k)%i;
int ans = (m-k++f[n])%n;
if(ans<=) ans+=n;//必须注意边界情况因为最后出现了-k所以要考虑负数的情况
printf("%d\n",ans);
}
return ;
}
 

And Then There Was One(约瑟夫问题变形)的更多相关文章

  1. 【约瑟夫环变形】UVa 1394 - And Then There Was One

    首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...

  2. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  3. Poj 3517 And Then There Was One(约瑟夫环变形)

    简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...

  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. G - And Then There Was One (约瑟夫环变形)

    Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbe ...

  7. UVA1452|LA4727-----Jump------经典的约瑟夫公式的变形(DP)

    本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...

  8. poj 1091 跳蚤

    跳蚤 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8482   Accepted: 2514 Description Z城 ...

  9. 2016 Multi-University Training Contest 10

    solved 7/11 2016 Multi-University Training Contest 10 题解链接 分类讨论 1001 Median(BH) 题意: 有长度为n排好序的序列,给两段子 ...

随机推荐

  1. ios学习——键盘的收起

    在开发过程中,我们经常会用到UITextField.UITextView等文本框,然后这些文本框在点击之后会自动成为第一响应者(FirstResponder),并自动弹出软键盘.然而,没有自动定义好的 ...

  2. VM虚拟机连Linux黑屏问题

    在尝试了关闭VM的加速3D图形后,若仍黑屏(但是挂起时却能显示),可以尝试在以管理员身份cmd中输入netsh winsock reset,重启后可以恢复正常.这个问题似乎与网络某个端口有关,我上次打 ...

  3. scala写算法-用小根堆解决topK

    topK问题是指从大量数据中获取最大(或最小)的k个数,比如从全校学生中寻找成绩最高的500名学生等等. 本问题可采用小根堆解决.思路是先把源数据中的前k个数放入堆中,然后构建堆,使其保持堆序(可以简 ...

  4. MySQL in or效率对比

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/60 考虑如下两个sql: select * from table ...

  5. Chrome调试折腾记_(1)调试控制中心快捷键详解!!!

    转载:http://blog.csdn.net/crper/article/details/48098625 大多浏览器的调试功能的启用快捷键都一致-按下F12;还是熟悉的味道;  或者直接 Ctrl ...

  6. 利用伪元素:after清除浮动

    一.代码 html代码 <div class="clearfix"></div> css样式 .clearfix{ zoom:1;/*这个属性是为了兼容IE ...

  7. 动态求区间K大值(权值线段树)

    我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...

  8. MySQL一对一:一对多:多对多: 实例!!!!

    学生表和课程表可以多对多 一个学生可以学多门课程 一门课程可以有多个学生: 多对多 *** 一个学生对应一个班级 一个班级对应多个学生: 一对多 *** 一个老师对应多个学生 多个学生对应一个老师:一 ...

  9. MicroPython教程之TPYBoard开发板DIY红外寻迹小车

    智能小车现在差不多是电子竞赛或者DIY中的主流了,寻迹,壁障,遥控什么的,相信大家也都见得很多了,这次就大家探讨一下寻迹小车的制作方法,不同于以往的是这次的程序不用C语言写,而是要使用python语言 ...

  10. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...