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

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

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

Source

题目描述 : n个数排成一圈,第一次删除,以后每数k个数删除一次。求最后一次被删除的数。
假设数字标号为0,1,2,3,,,n-1,。第一次删除的数是k,那么还剩0,1,2,3,,,k-1,k+1,k+2,,,,n-1;
那么问题就转化为求这n-1个数,最后一次被删除的数?,最优子结构,定义状态f[n]代表对n个数进行操作,最后一次被删除的数。
我们需要重新对这n-1个数重新编号,k+1,k+2,k+3,,,n-1,0,1,2,3,4, ,,k-1,重新编号为,0,1,2,3,4,5,,,,n-1.
f[n]与f[n-1]有什么关系呢?f[n]=(f[n-1]+k)%n;因为只是重新编号,所以我们只需将n-1个数所求的最后一个数的序号转化为n个数要求的最后一个数的序号.
题目要求第一次删除的是m,那么我们考虑-k+1,开始数k个数,那么第一次删除的就是0号元素,而且如果0号元素是m的话,那么f[n]号元素就为f[n]+m.
int answer=(m-k+1+f[n])%n;
if(answer<=0)
answer+=n;
不能写成(answer+n)%n,因为answer==0,n%n==0.
#include <iostream>
#include <cstdio>
//#include <strng>
#include <cstring>
using namespace std; int n,m,k;
int f[];
void init()
{
memset(f,,sizeof(f));
} void solve()
{ for(int i=;i<=n;i++)
f[i]=(f[i-]+k) % i;
int answer;
answer=(m-k++f[n]) % n;
if(answer<=)
answer=(answer+n)%n; //不能这么写,如果answer==0,答案就为0了
printf("%d\n",answer); } int main()
{ // freopen("test.txt","r",stdin);
while(~scanf("%d%d%d",&n,&k,&m))
{
if(n== && m== && k==)
break;
init();
solve();
} return ;
}
 

poj 3517(约瑟夫环问题)的更多相关文章

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

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

  2. (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)

    /* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...

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

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

  4. POJ 3517 And Then There Was One( 约瑟夫环模板 )

    链接:传送门 题意:典型约瑟夫环问题 约瑟夫环模板题:n个人( 编号 1-n )在一个圆上,先去掉第m个人,然后从m+1开始报1,报到k的人退出,剩下的人继续从1开始报数,求最后剩的人编号 /**** ...

  5. POJ 2359 Questions(约瑟夫环——数学解法)

    题目链接: http://poj.org/problem?id=2359 题意描述: 输入一个字符串 按照下面的规则,如果剩下的最后一个字符是'?',输出"Yes",如果剩下的最后 ...

  6. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  7. POJ 2886 Who Gets the Most Candies?(线段树&#183;约瑟夫环)

    题意  n个人顺时针围成一圈玩约瑟夫游戏  每一个人手上有一个数val[i]   開始第k个人出队  若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人   val[k ...

  8. poj 3517

    题目链接  http://poj.org/problem?id=3517 题意        约瑟夫环  要求最后删掉的那个人是谁: 方法        理解递推公式就行了  考虑这样一组数据  k ...

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

随机推荐

  1. [NOIP2001] 提高组 洛谷P1026 统计单词个数

    题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保 证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包含的 ...

  2. Python数据分析常用的库总结

    Python之所以能够成为数据分析与挖掘领域的最佳语言,是有其独特的优势的.因为他有很多这个领域相关的库可以用,而且很好用,比如Numpy.SciPy.Matploglib.Pandas.Scikit ...

  3. 洛谷——P2434 [SDOI2005]区间

    P2434 [SDOI2005]区间 题目描述 现给定n个闭区间[ai, bi],1<=i<=n.这些区间的并可以表示为一些不相交的闭区间的并.你的任务就是在这些表示方式中找出包含最少区间 ...

  4. 纯Java Web项目下的Session共享方案收集(待实践)

    1.使用filter方法存储 这种方法比较推荐,因为它的服务器使用范围比较多,不仅限于tomcat ,而且实现的原理比较简单容易控制. 可以使用memcached-session-filter 官方网 ...

  5. Gerrit实现代码审计(code review)

    1.Gerrit是个啥? 实现代码审计,比git(gitlab.github)多了代码审计的功能. 2.两类角色 角色1:我是代码编写者,只能写代码和提交给审计者代码,不能直接把代码合并到线上发布 角 ...

  6. 破解电信光猫华为HG8120C关闭路由功能方法

    昨天电信的工作人员来安装了电信的光纤宽带,使用的是华为HG8120C这款光电转换器与路由器一体机 这导致下级路由无法直接使用PPPOE拨号连接到互联网,且无法使用端口映射来实现外网访问 而华为开放给用 ...

  7. spring mvc 整理

    spring mvc 整理

  8. performSelector调用和直接调用的区别

    今天在准备出笔试题的过程中随便搜了一下其他的笔试题,看到其中一个就是关于performSelector与直接调用的区别. 个人感觉这其实是一个陷阱题,因为大部分应用场景下,用哪一种都可以,可以说是没有 ...

  9. 浅谈 ZipArchive 类

    Microsoft .NET Framework 4.5 新增了 ZipArchive 类 Microsoft Windows 8 Consumer Preview 操作系统已经内置了 Microso ...

  10. bootstrap中固定table的表头

    前端时间鼓捣的一个简单的手机站点,今天又拿出来弄一弄 由于主要是给手机訪问.用的是bootstrap响应式布局,今天的任务是做一个数据展示页面 可是因为数据的列比較多.所以横向显示不下,为了较好的显示 ...