Joseph
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 50068   Accepted: 19020

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

非常小的时候就有的约瑟夫问题,就是一群人(人数为n)围成一桌,从1到n标上号,然后来一个数m,每次数到m的人就被淘汰,从下一个人開始再数m个数,数到m的再被淘汰,就这么淘汰去吧。

这题是有n个好人,n个坏人。

好人的标号是从1到n,坏人的标号是从n+1到2*n。题目要找一个m,把坏人都淘汰掉,好人一个都不淘汰。

这题的关键在于不要纠结与坏人的标号,不论人数还剩多少,好人的标号始终是1到n。坏人的标号始终在后面。淘汰一个坏人。仅仅需把剩余的人数减1,剩下的坏人把之前淘汰的坏人填补上,穿好他们的标号就好。所以举个样例

6个人:1 2 3 4 5 6

m=5

第一次从1開始数5位,淘汰5,剩余 1 2 3 4 5(6就往前移一位。穿上5的衣服,这样好人就还是标号1 2 3,坏人标号4 5。剩余5个人)

第二次从5開始数5位,淘汰4。剩余 1 2 3 4 (好人标号1 2 3,坏人标号4)

第三次从4開始数5位,淘汰4。剩余1 2 3 。游戏结束。

为什么不要纠结于坏人的标号呢?由于不easy得出公式啊,如今不计较坏人的标号的话,我得到的公式就是

kill_num=(kill_num+m-1)%rest

所以我记录一个kill的vector,仅仅要每次淘汰的标号大于n或是等于0,即符合标准,我就把它扔进去,什么时候kill的人数等于n了。说明找到的m是正确的。否则就m++,再找。

(找m)代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; int people[50];
vector <int> kill; int main()
{
int n,k=0;
while(cin>>n)
{
int result=n+1,rest=2*n,kill_num=1;
int n2=2*n; memset(people,0,sizeof(people));
kill.clear();
while(1)
{
if(kill.size()==n)
break;
if((result+kill_num-1)%rest==0)
{
kill_num=rest;
rest--;
kill.push_back(rest);
}
else if((result+kill_num-1)%rest<=n)
{
kill_num=1;
kill.clear();
rest=n2;
result++;
}
else
{
kill_num=(result+kill_num-1)%rest;
rest--;
kill.push_back(kill_num);
}
}
cout<<result<<endl;
} return 0;
}

终于打表代码:

#include <iostream>
using namespace std; int main()
{
int result[16];
int n; result[1] = 2;
result[2] = 7;
result[3] = 5;
result[4] = 30;
result[5] = 169;
result[6] = 441;
result[7] = 1872;
result[8] = 7632;
result[9] = 1740;
result[10] = 93313;
result[11] = 459901;
result[12] = 1358657;
result[13] = 2504881;
result[14] = 13482720; while(cin>>n && n)
{
cout<<result[n]<<endl;
}
return 0;
}

POJ 1012:Joseph的更多相关文章

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

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

  2. 九度oj题目1012:畅通工程

    题目1012:畅通工程 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6643 解决:2863 题目描述: 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇. ...

  3. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  4. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  5. 【九度OJ】题目1012:畅通工程 解题报告

    [九度OJ]题目1012:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1012 题目描述: 某省调查城镇交通状况 ...

  6. POJ 1012 Joseph 约瑟夫问题

    http://poj.org/problem?id=1012 早上去图书馆复习苦逼的复习....万恶的数逻.T T我还要自我安慰的说复习完了奖励回来刷水题~ 10点多的时候外面校运会大吼撑杆跳的那个. ...

  7. POJ 1012 Joseph

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

  8. POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2

    http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...

  9. poj 1012 Joseph (约瑟夫问题)

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47657   Accepted: 17949 Descript ...

随机推荐

  1. thinkphp5项目--企业单车网站(一)

    thinkphp5项目--企业单车网站(一) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  2. 如何在Ubuntu14.04中安装mysql

    接触过MySQL的小伙伴们都知道,在Windows下安装MySQL是一件让人十分头大的事情,但是在Ubuntu等其他Linux系统中安装MySQL就简单很多了,具体的教程如下.1.在Ubuntu的命令 ...

  3. 【2017 Multi-University Training Contest - Team 1 1011】KazaQ's Socks

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6043 [Description] 一个人壁橱里有n双袜子,每天早上取一双最小下标的袜子,然后晚上放 ...

  4. Vijos——T 1082 丛林探险

    https://vijos.org/p/1082 描述 东非大裂谷中有一片神秘的丛林,是全世界探险家的乐园,著名黄皮肤探险家BB一直想去试试.正好我国科学家2005年4月将首次对东非大裂谷进行科考,B ...

  5. 【J2EE】在项目中理解J2EE规范

             J2EE平台由一整套服务(Service),应用程序接口(API)和协议构成,它对开发企业级应用提供了功能支持.13个核心技术各自是JDBC, JNDI, EJB, RMI, JSP ...

  6. java同步包种ArrayBlockingQueue类的分析与理解

    前言: ArrayBlockingQueue类是一个堵塞队列.重要用于多线程操作的条件. 一,官方解释 一个建立在数组之上被BlockingQueue绑定的堵塞队列.这个队列元素顺序是先进先出.队列的 ...

  7. android启动模式对于体验的影响

    说到Android的启动模式.懂Android的人肯定都懂. 通过设置启动模式我们不仅能够节省内存的使用.还能达到更好的体验,比方我们打开一个应用,点击home键回到主界面的时候程序是没有被kill掉 ...

  8. js插件---tree(多级文件)插件如何使用

    js插件---tree(多级文件)插件如何使用 一.总结 一句话总结:还是一般的引入js和css后js调用的方式, 只不过tree调用的时候必须设置一个 HTML 模板(就是调用的那段html代码,别 ...

  9. windows WEB 高可用/可伸缩

    windows NLB+ARR实现Web负载均衡高可用/可伸缩   基于IIS的ARR负载均衡 基于NLB负载均衡 这两篇分别分ARR 和 NLB基本配置,下面我们讲讲,如何组合使用,搭配成高可用/可 ...

  10. JS --- 延迟加载的几种方式

    标题:JS延迟加载,也就是等页面加载完成之后再加载 JavaScript 文件.  JS延迟加载有助于提高页面加载速度. 1. defer 属性 HTML 4.01 为 <script>标 ...