题意:

约瑟夫问题的变式。先指定第m个人必须死,然后每隔k个人死一个。求最后那个死的人的编号是什么。

题目

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
2019

分析

首先不考虑第一个必须死的人是m的情况。我们把n个人编号为[0,n-1]。那么第一轮出局的就是编号为k-1的人,剩下的人的编号是[0,k−2]∪[k,n−1]。然后我们从编号为k的人开始,循环一圈给所有人重新分配编号

0----->i

1----->i+1

2----->i+2

… …

k-2----->n-2

k-1(杀死)

k----->0///倒推,从这里最容易看出来dp[i]=(dp[i-1]+k)%i;(此时i==n)

k+1----->1

… …

n-2----->i-2

n-1----->i-1

其实这个问题就是问如何用存活者在i-1个人中的编号求出存活者在i个人中的编号。我们知道,从原问题推到子问题其实是把所有人的编号-k,那么从子问题推到原问题就是把人的编号+k,但要注意,此时+k可能会大于当前人的规模i,所以要对i取膜。

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e4+10;
int dp[M];
int n,m,k;int main()
{while(~scanf("%d%d%d",&n,&k,&m)&&(m||n||k)){memset(dp,0,sizeof(dp));for(int i=2;i<n;i++)dp[i]=(dp[i-1]+k)%i;dp[n]=(dp[n-1]+m)%n;printf("%d\n",dp[n]+1);}return 0;
}

And Then There Was One POJ - 3517(变形约瑟夫环+规律)相关推荐

  1. poj 3517

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

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

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

  3. (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)

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

  4. poj 2240 Bellman-Flod 求环

    http://poj.org/problem?id=2240 深刻体现了自己代码能力有问题外加改模板能力有问题.外加Debug有问题.以后做到: 1.算法原理能够轻易弄出来. 2.代码模板自己收集各种 ...

  5. poj 1012 Joseph(约瑟夫环求每次出圈人的序号)

    http://poj.org/problem?id=1012 有k个好人和k个坏人,前k个是好人后k个是坏人.模拟约瑟夫环,每次数到m的数要被杀死,然后他后面的人从1开始报数.重复这个过程.要求输出最 ...

  6. 2018年全国多校算法寒假训练营练习比赛(第一场)D. N阶汉诺塔变形(找规律)

    链接:https://www.nowcoder.com/acm/contest/67/D 来源:牛客网 题目描述 相信大家都知道汉诺塔问题.那么现在对汉诺塔问题做一些限制,成为一个新的玩法. 在一个底 ...

  7. 1875 丢手绢 约瑟夫环变形 枚举

    1875 丢手绢 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 六一儿童节到了,小朋友们在玩丢手绢的游戏.总共有C个小朋友,编号从1到C,他们站成一个 ...

  8. 约瑟夫环 poj 3750 小孩报数问题 模拟

    Language: Default 小孩报数问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accept ...

  9. POJ 1012 Joseph 题解

    POJ 1012 Joseph 题解 题目解读: The Joseph's problem is notoriously known. For those who are not familiar w ...

最新文章

  1. 结合项目实例 回顾传统设计模式(二)观察者模式
  2. 基于Spring AOP的JDK动态代理和CGLIB代理
  3. python求三个整数最大值_怎么用python比较三个数大小
  4. 用c语言设计一个任意20个数升序排列,编写一个用选择法对一维数组升序排序的函数,并在主函数中调用该排序函数,实现对任意20个整数的排序。...
  5. c语言竖等于意思,C语言竖式问题
  6. 腾讯首度披露基础架构演进史:“海量之道”进化“生而为云”
  7. 信息学奥赛一本通(1153:绝对素数)
  8. FPGA不积跬步(目录篇)
  9. 有关emoji表情以及utf-16编码
  10. maven 打包java项目_如何使用maven打包java项目?
  11. 原始套接字与抓包过滤规则setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, ...)
  12. HDU2085 核反应堆【递推】
  13. windows脚本bat批处理 关闭显示器 关闭显示器脚本bat
  14. unity中Rotation * Position的意义
  15. 统计学习基础(ESL)中文版
  16. word文档怎么批量解除锁定_解除锁定的word文档的方法
  17. chmod 755 与chmod +x的区别
  18. 微分几何笔记(1)——参数曲线、内积、外积
  19. linux 系统中/etc/fstab 文件各字段功能,Linux系统 /etc/fstab各个字段含义解释
  20. python三重积分_蒙特卡罗方法。三重积分。Python。“+”的操作数父级不受支持...

热门文章

  1. Android之Fatal Exception: org.greenrobot.greendao.DaoException: Could not init DAOConfig
  2. linux之安装Clion和运行使用总结
  3. python3 爬虫第三步 本文包你学会正则 不会就来锤我
  4. android 网卡监听,Android实时监听网络的变化
  5. wpsppt流程图联系效果_风险隐患排查的手段—HAZOP 与检查表的区别及应用效果
  6. whitelabel error page什么意思_什么是RESTful API?总算能说清楚了
  7. 魔性十足的数学动态图,这种东西都拿出来分享?
  8. excel函数中if android,在Android中阅读Excel
  9. echo字符集 shell_Xshell 能显示中文 但输入的中文都变成了问号
  10. python多线程 不在main_从python线程(不是main)启动pyQt线程有什么不...