Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 50596   Accepted: 19239

Description

The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

Output

The output file will consist of separate lines containing m corresponding to k in the input file.

Sample Input

3
4
0

Sample Output

5
30

Source

THINKING

   本题是约瑟夫环变形 先引入Joseph递推公式,设有n个人(0,...,n-1),数m,则第i轮出局的人为f(i)=(f(i-1)+m-1)%(n-i+1),f(0)=0;

  f(i) 表示当前子序列中要退出的那个人(当前序列编号为0~(n-i));

  拿个例子说:K=4,M=30;

  f(0)=0;

  f(1)=(f(0)+30-1)%8=5; 序列(0,1,2,3,4,5,6,7)中的5

  f(2)=(f(1)+30-1)%7=6; 序列(0,1,2,3,4,6,7)中的7

  f(3)=(f(2)+30-1)%6=5; 序列(0,1,2,3,4,6)中的6

  f(4)=(f(3)+30-1)%5=4; 序列(0,1,2,3,4)中的4

  假设当前剩下i个人(i<=n),显然这一轮m要挂(因为总是从1开始数).经过这一轮,剩下的人是:1 2 3 ... m- 1 m + 1 ... i, 我们将从m+1开始的数映射成1, 则m+2对应2, n对应i - m, 1对应成i - m + 1  m - 1对应i - 1,那么现在的问题变成了已知i - 1个人进行循环报数m,求出去的人的序号。假设已经求出了i- 1个人循环报数下最后一个出去的人的序号X0,那么它在n个人中的序号X1=(X0+ m - 1) % n + 1,  最初的X0=1 ,反复迭代X0和X1可以求出.

  接下来说说m的取值范围:我们考察一下只剩下k+1个人时候情况,即坏人还有一个未被处决,那么在这一轮中结束位置必定在最后一个坏人,那么开始位置在哪呢?这就需要找K+2个人的结束位置,然而K+2个人的结束位置必定是第K+2个人或者第K+1个人,这样就出现两种顺序情况:GGGG.....GGGXB 或  GGGG......GGGBX (X表示有K+2个人的那一轮退出的人)所以有K+1个人的那一轮的开始位置有两种可能即第一个位置或K+1的那个位置,限定m有两种可能:t(k+1) 或 t(k+1)+1; t>=1; 若遍历每一个m必定超时,避免超时则需要打表和限制m的范围。

const Joseph:array [..] of  longint=(,,,,,,,,,,,,,,);
var x:longint;
begin
while true do
begin
readln(x);
if x= then halt;
writeln(Joseph[x]);
end;
end.

[POJ1012]Joseph的更多相关文章

  1. poj1012.Joseph(数学推论)

    Joseph Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 493  Solved: 311 Description The Joseph's prob ...

  2. 【poj1012】 Joseph

    http://poj.org/problem?id=1012 (题目链接) 半年前的考试题..任然清晰的记得那次差10分就AK... 题意 约瑟夫环,有前k个好人,后k个坏人,要求使得后k个坏人先死的 ...

  3. Joseph(JAVA版)

    package Joseph;//约瑟夫环,m个人围成一圈.从第K个人开始报数,报道m数时,那个人出列,以此得到出列序列//例如1,2,3,4.从2开始报数,报到3剔除,顺序为4,3,1,2publi ...

  4. Hdu 1443 Joseph

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. 一道模拟题:改进的Joseph环

    题目:改进的Joseph环.一圈人报数,报数上限依次为3,7,11,19,循环进行,直到所有人出列完毕. 思路:双向循环链表模拟. 代码: #include <cstdio> #inclu ...

  6. POJ 1012 Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44650   Accepted: 16837 Descript ...

  7. poj1012

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52097   Accepted: 19838 Descript ...

  8. hdu 1443 Joseph (约瑟夫环)

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. UVa 1363 (数论 数列求和) Joseph's Problem

    题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分 ...

随机推荐

  1. -----IT男生涯————初始篇

    大家好,我是kuuga,一名普通大学的在读生.其实,当时我不知道为什么会选择计算机这个学院,而且还选择了网络工程这个坑爹的专业.为什么说坑爹呢?因为几年学生生涯中编程已经占了很多时间和课程,至于我的专 ...

  2. php新特性--持续更新

    命名空间 在其他语言中不算新鲜事,但php是5.3.0中引入,具体定义就不复述了,其主要作用是 封装和组织相关php类 .命名空间被引入之前php主要是通过Zend方式组织代码,这种方式带来的问题是类 ...

  3. CentOS 根据命令查所在的包

    在工作中经常会遇到想用某个命令机器没装却又不知道命令在哪个包(源码编译不再本文范围内),下面介绍个比较笨的方法可以帮助我们搞定这个问题. 说明:蓝色=命令名称       浅绿=命令参数       ...

  4. CODEVS 2102 石子归并 2

    [题目描述 Descriptin] 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分.试设计出1个算法, ...

  5. windows store app Lifecycle

    1.Activated 2.Suspended 3.Resumed 4.Terminated 对应的 js代码: (function () { "use strict"; WinJ ...

  6. 关于 js 2个数组取差集怎么取

    关于 js 2个数组取差集怎么取? 例如求var arr1 = [1]; var arr2 = [1,2];的差集方法一: Array.prototype.diff = function(a) { r ...

  7. 28 个必备的 Linux 命令行工具

    dstat & sar iostat, vmstat, ifstat and much more in one. slurm 网络流量图形化工具 vim & emacs 这个没人不知道 ...

  8. css 多行显示省略号....

    CSS属性如下: white-space: nowrap; overflow: hidden; text-overflow: ellipsis;

  9. Ubuntu下与菜单和图标相关的几个文件夹

    转自UBUNTU下与菜单和图标相关的几个文件夹 /usr/share/icons  系统图标文件夹 /usr/share/applications  系统菜单文件夹,要在左上角的应用程序菜单中添加一项 ...

  10. 转一贴,今天实在写累了,也看累了--【Python异步非阻塞IO多路复用Select/Poll/Epoll使用】

    下面这篇,原理理解了, 再结合 这一周来的心得体会,整个框架就差不多了... http://www.haiyun.me/archives/1056.html 有许多封装好的异步非阻塞IO多路复用框架, ...