poj 1012 Joseph (约瑟夫问题)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 47657 | Accepted: 17949 |
Description
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
Output
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 (约瑟夫问题)的更多相关文章
- POJ 1012 Joseph 约瑟夫问题
http://poj.org/problem?id=1012 早上去图书馆复习苦逼的复习....万恶的数逻.T T我还要自我安慰的说复习完了奖励回来刷水题~ 10点多的时候外面校运会大吼撑杆跳的那个. ...
- POJ 1012 Joseph
Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44650 Accepted: 16837 Descript ...
- POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2
http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...
- POJ 1012 Joseph(打表题)
题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- poj 1012——Toseph
提交地址:http://poj.org/problem?id=1012 ...
- Joseph POJ - 1012 约瑟夫环递推
题意:约瑟夫环 初始前k个人后k个人 问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...
- POJ 1012:Joseph
Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 50068 Accepted: 19020 Descript ...
- (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)
/* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...
随机推荐
- 解决SQL Server的TEXT、IMAGE类型字段的长度限制
更多资讯.IT小技巧.疑难杂症等等可以关注 艾康享源 微信公众号. 来自为知笔记(Wiz)
- 跟我学android-使用Eclipse开发第一个Android应用(三)
打开Eclipse,选择 File—New –Android Application Project Application Name 就是我们的 应用名称,也是我们在手机应用程序列表里看到的名称. ...
- js关于DOM和BOM
关于BOM和DOM BOM 下面一幅图很好的说明了BOM和DOM的关系 BOM提供了一些访问窗口对象的一些方法,我们可以用它来移动窗口位置,改变窗口大小,打开新窗口和关闭窗口,弹出对话框,进行导航以及 ...
- gitHub远程分支创建
1.在本地master上分出一个项目分支 git checkout -b bug1_dujie git checkout -b bug1_dujie origin/master 在远程添加分支 $gi ...
- 修改PHP的默认时区
每个地区都有自己的本地时间,在网上及无线电通信中,时间的转换问题显得格外突出.整个地球分为24个时区,每个时区都有自己的本地时间.在国际无线电或网络通信场合,为了统一起见,使用一个统一的时间,成为通用 ...
- thinkphp 文件下载实例 实现以及注意事项
#下载 function download() { $id=$_GET['id']; $file_name ...
- JS操作性能优化
1. 适当使用变量 Maybe document.getElementById("myField").style.backgroundColor = "#CCC" ...
- Java中堆内存(heap)和栈内存(stack)的区别
在Java代码中,常常会使用到这样的类的声明实例化: Person per = new Person(); //这其实是包含了两个步骤,声明和实例化 Person per = null; //声明一个 ...
- JMS集群部署问题 java.net.ConnectException: Connection refused; No available router to destination
1:本地spring配置如下 <!-- JndiTemplate --> <bean id="jndiTpl" class="org.springfra ...
- ionic list item-radio checked
<div class="list"> <label class="item item-radio" ng-repeat="k in ...