# POJ 1012：Joseph

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

6个人：1 2 3 4 5 6

m=5

kill_num=(kill_num+m-1)%rest

(找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;
}

• 本文已收录于以下专栏：

## POJ1012Joseph解题报告

• Dafang_Xu
• 2015年10月22日 21:19
• 842

## POJ 1012 Joseph题解与心得

POJ 1012 Joseph题解与心得   2010-06-06 13:15    这几天都在收集一些DP的资料，但读了一些入门的资料后我发现DP比我想象中的还要难入门，主要是DP问题各概念都比较抽...
• leesofte
• 2010年06月06日 15:57
• 663

## POJ-1012-Joseph-暴力/模拟

• viphong
• 2015年11月10日 18:45
• 175

## poj 1012 Joseph

• fallenfall
• 2013年09月29日 14:12
• 449

## Poj 1012 Joseph

import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public ...
• dyhu083
• 2013年10月30日 18:36
• 293

## POJ 1012 Joseph

#includeint Check(int k, int m){    int i, start, len;    len = 2*k;    start = 0;    for(i=1; ik; i...
• luxuejuncarl
• 2007年04月29日 00:25
• 1120

## POJ - 1012 Joseph

• u011345136
• 2013年11月23日 16:17
• 631

## poj 1012 Joseph解题报告

• xiancaifu
• 2010年08月09日 13:51
• 453

## [POJ][1012]Joseph

Description The Joseph's problem is notoriously known. For those who are not familiar with the or...
• oqqShaw123
• 2014年05月14日 22:42
• 808

## Joseph - POJ 1012 打表

Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47228   Accept...
• u014733623
• 2014年06月05日 12:13
• 585

举报原因： 您举报文章：POJ 1012：Joseph 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)