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. slqplus 帮助手册

    1.查看sqlplus的帮助是否可用,必须登录了才可用. D:\app\product\\db_1\sqlplus\admin\help>sqlplus /nolog SQL :: Copyri ...

  2. python27读书笔记0.2

    # -*- coding:utf-8 -*- ##s.partition(d)##Searches string s for the first occurrence of some delimite ...

  3. 本地化web开发的一个例子-jquery.i18n.properties

    关键字:Web本地化, jquery,jquery.i18n.properties. 运行环境:Chrome, IE. 本文介绍使用jquery.i18n.properties对网站前端实现本地化,支 ...

  4. Amazon Alexa 语音识别1 : 简介

    Alexa是Amazon自家的语音识别技术,需要配合自家的Echo音箱使用.开发者可以在Amazon上建立自己的程序(Skill)来连接到自己的应用或是硬件.例如,用户家里有一套xx牌的智能灯,现在希 ...

  5. 如何使用Promise

    在说Promise之前,不得不说一下JavaScript的嵌套的回调函数 在JavaScript语言中,无论是写浏览器端的各种事件处理回调.ajax回调,还是写Node.js上的业务逻辑,不得不面对的 ...

  6. java.io.IOException: Cannot run program "bash": error=12, Cannot allocate memory

    java.io.IOException: Cannot run program , Cannot allocate memory 云服务器运行nutch报出的异常: 解决方案: http://daim ...

  7. ExtJS4.2学习(15)树形表格(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-27/185.html --------------- ...

  8. Portal相关技术及架构

    Portal以用户为中心,提供统一的用户登录,实现信息的集中访问,集成了办公商务一体的工作流环境.利用Portal技术,可以方便地将员工所需要的,来源于各种渠道的信息资料集成在一个统一的桌面视窗之内. ...

  9. ECSHOP安装或使用中提示Strict Standards: Non-static method cls_image:

    随着ECSHOP的不断发展,越来越多的人成为了ECSHOP的忠实粉丝.由于每个人的服务器环境和配置都不完全相同,所以ECSHOP也接二连三的爆出了各种各样的错误信息.相信不少新手朋友在ECSHOP安装 ...

  10. [转载]MongoDB学习 (五):查询操作符(Query Operators).1st

    本文地址:http://www.cnblogs.com/egger/archive/2013/05/04/3059374.html   欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 查询操作符(Quer ...