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. 把DataSet转换成JSON

    /// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"> ...

  2. Angular ng-repeat

    <tr ng-repeat="(key,item) in tableData"> <td class="check hidden-xs"> ...

  3. SQL学习_时间函数

    最近测试报表需要统计不同时间段的列表记录,收集一些时间函数作为参考,原文地址:http://blog.csdn.net/lyzlyfok/article/details/6282509 sql ser ...

  4. controller.pp 各组件的安装顺序

    controller 属性:         admin_address => $controller_node_address,         public_address => $c ...

  5. codeforces 630K - Indivisibility

    K. Indivisibility 题意:给一个n(1 <= n <= 10^18)的区间,问区间中有多少个数不能被2~10这些数整除: 整除只需要看素数即可,只有2,3,5,7四个素数: ...

  6. awk的使用备忘

    [转]http://www.cnblogs.com/mydomain/archive/2012/09/24/2699467.html awk引用外部变量   一.用awk 有以下几种方法去调用变量: ...

  7. EntityFramework-DBFirst-重新生成后写的验证消失(解决办法)

    1.这是EF生成的类: namespace PMS.DataProvider.EntityFramework { using System; using System.Collections.Gene ...

  8. Citect:How do I translate Citect error messages?

    http://www.opcsupport.com/link/portal/4164/4590/ArticleFolder/51/Citect   To decode the error messag ...

  9. Mac下无法推出硬盘

    Q:刚刚mac使用移动硬盘,使用后推出时弹出无法推出,提示:Finder正在使用中. 解决:打开#活动监视器#(找不到可以在spotlight中搜索),找到Finder应用,双击,强制退出后再启动Fi ...

  10. ubuntu使用中的一些问题

    ubuntu如何修改计算机名? sudo gedit /etc/hostname 如何查看ubuntu操作系统位数 方法1: #查看long的位数,返回32或64 getconf LONG_BIT 方 ...