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. 关于 java.io.IOException: open failed: EACCES (Permission denied)

    今天解决了一个问题,不得不来和大家分享.就是关于 java.io.IOException: open failed: EACCES (Permission denied)的问题,网上也有很多人把这个问 ...

  2. 安装程序时出现错误代码0x80070422

    通过win10应用商店,下载应用,安装时出现错误代码0x80070422. 需要打开services.msc,将windows update服务打开.

  3. TensorFlow 深度学习笔记 Logistic Classification

    Logistic Classification Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地 ...

  4. SQL Server 数据的创建、增长、收缩

    第一步: create database Studio         on primary          (name = 'Studio',filename='E:\DB\Studio.mdf' ...

  5. node.js介绍

    官网说明: Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable ...

  6. Linux学习笔记1:配置Linux网络和克隆虚拟机并更改配置

    一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候会报network is unreachable 并且怎么也找不到问 ...

  7. res://ieframe.dll/acr_error.htm 纯手动解决方法

    res://ieframe.dll/acr_error.htm 引起这个原因的是,你IE浏览器的扩展组件的问题,因为不知道是具体哪个组件出了问题,所以要采用一刀切的方法来处理: 在开始/或开始键+R, ...

  8. nginx源代码分析--配置文件解析

    ngx-conf-parsing 对 Nginx 配置文件的一些认识: 配置指令具有作用域,分为全局作用域和使用 {} 创建其他作用域. 同一作用域的不同的配置指令没有先后顺序:同一作用域能否使用同样 ...

  9. [置顶] java 通过classloader加载类再通过classforname实例化

    自己写一个web服务器,发现class.forName用不了,找不到类,原因是找不到类路径, 可以过加载需要的类的文件路径,然后再通过ClassLoader来加载所需要的类.其中的原 理就不多说了,下 ...

  10. Java面试题之weblogic相关问题

    WebLogic是美国Oracle公司出品的一个application server确切的说是一个基于JAVAEE架构的中间件,BEA WebLogic是用于开发.集成.部署和管理大型分布式Web应用 ...