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

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

约瑟夫问题的变形,開始对题意就理解了好久。这类题目主要要找到中间的公式;这道题就要知道 当前出局的位置 = (前一个出局的位置+m-1)%(2k-(k-1);

是从0開始数。这就是一个通式;明确了这个公式这个题相当于就做出了一半;这中间另一个判别的条件,就是假设当前的位置假设<k,说明这样的情况就不满足了;

第一种方法:就直接能够对m的值进行枚举。利用判别条件,找出合适的m值;

以下是代码;这样的思路好像200ms能够过。

//暴力+枚举
#include <cstdio>
#include <cstring>
int main()
{
int k;
int ans[20];//储存当前的位置
int Joseph[20]={0}; //储存符合条件的m的值
for(k=1;k<14;k++)
{
int m=1;
memset(ans,0,sizeof(ans));
for(int i=1;i<=k;i++)
{
ans[i]=(ans[i-1]+m-1)%(2*k-i+1); //递推公式
if(ans[i]<k)
{
i=0;
m++;
}
}
Joseph[k]=m;
}
while(scanf("%d",&k)&&k!=0)
{
printf("%d\n",Joseph[k]);
}
return 0;
}

要想0ms过的,直接得出结果数组,然后再提交。

//直接枚举 0ms
#include <cstdio>
#include <cstring>
int main()
{
int k;
int Joseph[]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,1245064};
while(scanf("%d",&k)&&k!=0)
{
printf("%d\n",Joseph[k]);
}
return 0;
}

另外一种方法,要挖掘题目中的隐含条件,我看了好久都没看出来,代码是參考别人的,这样的46ms就能够过;优化了不少。

题目中的隐含条件;仅仅剩下一个坏人的时候,下一个报数的人要么是第1个人,要么是第k+1个人。所以间隔就是m=s*(k+1)或者是 m=s*(k+1)+1;

粗略的证明一下。參考别人的:(这里我也还有点没懂)

仅剩一个坏人,圈长为k+1;设下一个报数人为第一个人时的时间间隔为m1;下一个报数人为第k+1个人时的时间间隔为m2。

由 (1+m1)%(k+1)=1 得出 m1= s*(k+1);

   (k+1+m2)%(k+1)=1  得出 m2=s*(k+1)+1;

这样我们就能够对m的枚举优化剪枝,m是(k+1)的倍数或者是他的倍数+1;

#include <cstdio>
#include <cstring>
bool Joseph(int k,int m)
{
int n=2*k,x=0;
while(n>k)
{
x=(x+m-1)%n;//递推公式,算出当前出局的位置
if(x<k) //判别条件
return false;
n--;
}
return true;
}
int main()
{
int k;
int result[20]={0};
for(k=1;k<14;k++)
{
for(int i=k+1;;i+=k+1) //对m枚举进行优化
{
if(Joseph(k,i)) //m是k+1的倍数或者倍数+1
{
result[k]=i;
break;
}
else if(Joseph(k,i+1))
{
result[k]=i+1;
break;
}
}
}
while(scanf("%d",&k)&&k)
{
printf("%d\n",result[k]);
}
}

总结一下:对于这一类题型要读懂题目的题意。挖掘题目的隐含条件,多构思。理清思路才開始动手写。







poj 1012 Joseph (约瑟夫问题)的更多相关文章

  1. POJ 1012 Joseph 约瑟夫问题

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

  2. POJ 1012 Joseph

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

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

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

  4. POJ 1012 Joseph(打表题)

    题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...

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

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

  6. poj 1012——Toseph

    提交地址:http://poj.org/problem?id=1012                                                                 ...

  7. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  8. POJ 1012:Joseph

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

  9. (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)

    /* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...

随机推荐

  1. B/S 獲取客戶端Mac地址

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx. ...

  2. Linux sed命令删除指定行

    一.删除包含匹配字符串的行## 删除包含baidu.com的所有行sed -i '/baidu.com/d' domain.file 二.删除匹配行及后所有行## 删除匹配20160229的行及后面所 ...

  3. C#异常处理表、类、SQL

    表SQL /****** Object: Table [dbo].[IError] Script Date: 09/05/2012 17:00:41 ******/ SET ANSI_NULLS ON ...

  4. iOS: TableView如何刷新指定的cell 或section

    //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:in ...

  5. JavaScript--模拟网络爬虫

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. openMPI小集群安装

    经过一天的努力,终于完成了openMPI的多节点安装,即小集群安装.本文使用的是openmpi-1.6.5,下载地址见:http://www.open-mpi.org/software/ompi/v1 ...

  7. pip install robotframework-sshlibrary提示: Microsoft Visual C++ 9.0 is required

    win7下 pip install robotframework-sshlibrar时提示: error: Microsoft Visual C++ 9.0 is required (Unable t ...

  8. [jQuery编程挑战]002:实现一个转盘大抽奖

    body { background-color: #F2F2F2; text-align: center; } .container { position: relative; width: 500p ...

  9. 蟠桃记 AC 杭电

    蟠桃记 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  10. 清橙A1484

    http://www.tsinsen.com/ViewGProblem.page?gpid=A1484### 题解: 在线插入并不好做,我们将所有操作离线,变为删除操作. 每次询问的时候对于当前B串所 ...