Joseph(约瑟夫环)
Joseph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2094 Accepted Submission(s): 1273
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 file consists of separate lines containing k. The last line in
the input file contains 0. You can suppose that 0 < k < 14.
4
0
30
#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(约瑟夫环)的更多相关文章
- UVA 305 Joseph (约瑟夫环 打表)
Joseph The Joseph's problem is notoriously known. For those who are not familiar with the original ...
- 约瑟夫环(Joseph)的高级版(面向事件及“伪链表””)
约瑟夫环问题: 在一间房间总共有n个人(下标0-n-1),只能有最后一个人活命. 按照如下规则去杀人: 所有人围成一圈 顺时针报数,每次报到q的人将被杀掉 被杀掉的人将从房间内被移走 然后从被杀掉的下 ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- Joseph POJ - 1012 约瑟夫环递推
题意:约瑟夫环 初始前k个人后k个人 问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...
- hdu 1443 Joseph【约瑟夫环】
题目 题意:一共有2k个人,分别为k个好人和k个坏人,现在我们需要每隔m个人把坏人挑出来,但是条件是最后一个坏人挑出来前不能有好人被挑出来..问最小的m是多少 约瑟夫环问题,通常解决这类问题时我们把编 ...
- 小小c#算法题 - 12 - Joseph Circle(约瑟夫环)
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数(从1开始报数),数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又 ...
- hdu1443(约瑟夫环游戏的原理 用链表过的)
Problem Description The Joseph's problem is notoriously known. For those who are not familiar with t ...
- 约瑟夫环(java面向对象实现)
/* * 作者:zhaop * 功能:约瑟夫环 * */ public class Joseph { public static void main(String[] args) { // TODO ...
- POJ 3517 And Then There Was One( 约瑟夫环模板 )
链接:传送门 题意:典型约瑟夫环问题 约瑟夫环模板题:n个人( 编号 1-n )在一个圆上,先去掉第m个人,然后从m+1开始报1,报到k的人退出,剩下的人继续从1开始报数,求最后剩的人编号 /**** ...
- 约瑟夫环问题详解 (c++)
问题描述: 已知n个人(以编号0,2,3...n-1分别表示)围坐在一起.从编号为0的人开始报数,数到k的那个人出列:他的下一个人又从1开始报数,数到k的那个人又出列:依此规律重复下去,直到圆桌周围的 ...
随机推荐
- SQL Server 分离与附加数据库
前期准备: 创建数据库 create database Studio on primary (name ='Studio',filename ='E:\Studio.mdf'), ...
- struts2摘记
阐述struts2的执行流程. Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件.核心控制器FilterDi ...
- J2SE知识点摘记(一)
1. 数组的声明时无法指定数组的长度. 2. 一维数组的声明和内存的分配 "数据类型 数组名[]; //声明一维数组 数组名= new 数据类型[ ...
- HttpContext.Cache和Application的区别
原文:HttpContext.Cache和Application的区别 (转载) 应用程序级的Cache和Application用户会话级的Session application的缺点是在读取时最 ...
- 一步一步学习SignalR进行实时通信_6_案例
原文:一步一步学习SignalR进行实时通信_6_案例 一步一步学习SignalR进行实时通信\_6_案例1 一步一步学习SignalR进行实时通信_6_案例1 前言 类的定义 各块功能 后台 上线 ...
- egret随笔-egret浅入浅出
•不知道有多人跟笔者一样,喜欢学各种技术,但是都不精,但也有一两项算是精的. 自从踏上了egret游戏开发的道路,就不得不学习各种技术了,因为,要精通egret,首先必须要会TypeScript,其次 ...
- 一张图告诉你如何优化web 性能
- 支付宝“订单交易失败 ALI64” 报错的原因
移动快捷支付,往往需要集成支付宝的sdk,集成的过程相对简单,只要按照支付宝的文档,进行操作一般不会出问题. 下面主要说明一下,集成sdk后报 "订单交易失败 请稍后再试(ALI64)&qu ...
- 大数据Lambda架构
1 Lambda架构介绍 Lambda架构划分为三层.各自是批处理层,服务层,和加速层.终于实现的效果,能够使用以下的表达式来说明. query = function(alldata) 1.1 批处理 ...
- C# linq to xml
XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes") ...