Joseph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2094    Accepted Submission(s): 1273

Problem 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
题解:F(i)=(F(i-1)+m-1)%(n-i+1)由于去的后k个所以当F小于k就不符合。需要打表,否则超时;
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int k;
int dp[15];
bool solve(int m){
int cur=0;
for(int i=1;i<=k;i++){
if((cur+m-1)%(2*k-i+1)<k)
return false;
cur=(cur+m-1)%(2*k-i+1);
}
return true;
}
int main(){
for(k=1;k<15;k++){
int i;
for(i=1;;i++){
if(solve(i))break;
}
dp[k]=i;
}
while(~scanf("%d",&k),k){
printf("%d\n",dp[k]);
}
return 0;
}

Joseph(约瑟夫环)的更多相关文章

  1. UVA 305 Joseph (约瑟夫环 打表)

     Joseph  The Joseph's problem is notoriously known. For those who are not familiar with the original ...

  2. 约瑟夫环(Joseph)的高级版(面向事件及“伪链表””)

    约瑟夫环问题: 在一间房间总共有n个人(下标0-n-1),只能有最后一个人活命. 按照如下规则去杀人: 所有人围成一圈 顺时针报数,每次报到q的人将被杀掉 被杀掉的人将从房间内被移走 然后从被杀掉的下 ...

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

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

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

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

  5. hdu 1443 Joseph【约瑟夫环】

    题目 题意:一共有2k个人,分别为k个好人和k个坏人,现在我们需要每隔m个人把坏人挑出来,但是条件是最后一个坏人挑出来前不能有好人被挑出来..问最小的m是多少 约瑟夫环问题,通常解决这类问题时我们把编 ...

  6. 小小c#算法题 - 12 - Joseph Circle(约瑟夫环)

    约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数(从1开始报数),数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又 ...

  7. hdu1443(约瑟夫环游戏的原理 用链表过的)

    Problem Description The Joseph's problem is notoriously known. For those who are not familiar with t ...

  8. 约瑟夫环(java面向对象实现)

    /* * 作者:zhaop * 功能:约瑟夫环 * */ public class Joseph { public static void main(String[] args) { // TODO ...

  9. POJ 3517 And Then There Was One( 约瑟夫环模板 )

    链接:传送门 题意:典型约瑟夫环问题 约瑟夫环模板题:n个人( 编号 1-n )在一个圆上,先去掉第m个人,然后从m+1开始报1,报到k的人退出,剩下的人继续从1开始报数,求最后剩的人编号 /**** ...

  10. 约瑟夫环问题详解 (c++)

    问题描述: 已知n个人(以编号0,2,3...n-1分别表示)围坐在一起.从编号为0的人开始报数,数到k的那个人出列:他的下一个人又从1开始报数,数到k的那个人又出列:依此规律重复下去,直到圆桌周围的 ...

随机推荐

  1. 论山寨手机与Android 【11】移动网络规范的合纵连横

    上一章我们讨论了SmartPhone BP部分的硬件系统,接下去我们将讨论SmartPhone BP部分的软件系统.所谓BP,指的是基带处理器(Baseband Processor),又称为通讯处理器 ...

  2. hibernate的3种状态

    hibernate的三种状态是瞬态.持久态.脱管态 瞬态:新new来的对象称为瞬态. 持久态:处于该状态的对象在数据库中有一条对应的记录,并拥有一个持久标识. 脱管态:当与某持久对象的session关 ...

  3. 解决 MySQL manager or server PID file could not be found! 的方法

    [root@centos var]# service mysqld stop MySQL manager or server PID file could not be found!       [F ...

  4. WinForm 窗体与窗体相互嵌套

    只要将要被潜逃的的窗体的TopLeve设置为Flase即可像普通的控件一样,被添加到另外一个窗体中,TopLeve:是否为顶级窗口,下面来看代码: public partial class TTFor ...

  5. Android消息推送的服务端

    2.Android消息推送 MQTT服务器采用mosquito  http://mosquitto.org/ PHP管理包采用phpmqttclient:https://github.com/toku ...

  6. bootstrap注意事项(一)

    1.移动设备优先 移动设备优先是 Bootstrap 3 的最显著的变化. 在之前的 Bootstrap 版本中(直到 2.x),您需要手动引用另一个 CSS,才能让整个项目友好的支持移动设备. 现在 ...

  7. 获取xml文件

    <?xml version="1.0" encoding="utf-8" ?><ArrayOfSystemRool xmlns:xsi=&qu ...

  8. SQL Server执行计划那些事儿(2)——查找和扫描

    接下来的文章是记录自己曾经的盲点,同时也透漏了自己的发展历程(可能发展也算不上,只能说是瞎混).当然,一些盲点也在工作和探究过程中慢慢有些眉目,现在也愿意发扬博客园的奉献精神,拿出来和大家分享一下. ...

  9. 第一个OC类、解析第一个OC程序

    01第一个OC 类 本文目录 • 一.语法简介 • 二.用Xcode创建第一个OC的类 • 三.第一个类的代码解析 • 四.添加成员变量 • 五.添加方法 • 六.跟Java的比较 • 七.创建对象 ...

  10. 根据老赵轻量级Actor进行修改的Actor模型

    学习了老赵轻量级Actor模型,并在实际中使用,效果不错. 老赵轻量级Actor模型: ActorLite:一个轻量级Actor模型实现(上) ActorLite:一个轻量级Actor模型实现(中) ...