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的那个人又出列:依此规律重复下去,直到圆桌周围的 ...
随机推荐
- USB OTG学习
1. 概要 OTG设备使用插头中的ID引脚来区分A/B Device,ID接地被称作为A-Device,为连接时候的USB Host,A-Device始终为总线提供电力,ID悬空被称作为B-Devic ...
- Codeforces 241B
因为博客园的公式编辑有点坑,所以-- 原题
- 杭电oj1062 Text Reverse
Tips:使用一个临时数组c[1000] ,将输入的数据一边复制一边处理,碰到空格时就将前面的字符反向输出即可 #include<stdio.h> #include<string.h ...
- 业务需求那些事,使用WCF如何实现业务需求!
最近遇到一个新项目,需要与硬件结合,进行读取信息并保存在数据库中.业务要求也在昨天发布一个问题,当然感谢许多园内的朋友出谋划策,截图有真相! 关于这个问题,我做了如下假设.目前处于测试状态,代码比较简 ...
- 如何在Github Pages搭建自己写的页面?
教程一大堆,却没有几个能看懂的,问题一:90%的都在讲解如何搭建博客,和我想要将自己的网页部署到上面还是有点区别的.问题二:所有的教程都用到了Git,而我只知道Git是一个开源的分布式版本控制系统.完 ...
- sqlserver数据库差异比较
相信这个功能会对好多网友有帮助,不得不说vs的功能很强大,废话不多说,切入正题 我们可以使用 vs自带的工具比较两个数据库的架构差异和数据差异,这篇文章只是写了架构的差异,我相信看完以后, 如何进行数 ...
- Java 中 静态方法与非静态方法的区别
静态方法和实例方法的区别主要体现在两个方面: 在外部调用静态方法时,可以使用"类名.方法名"的方式,也可以使用"对象名.方法名"的方式.而实例方法只有后面这种方 ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
- Linux学习之常用技巧
▌基础 学习 Bash .你可以man bash来看看bash的东西,并不复杂也并不长.你用别的shell也行,但是bash是很强大的并且也是系统默认的.(学习zsh或tsch只会让你在很多情况下受到 ...
- tomcat异常
java.lang.UnsupportedClassVersionError: org/apache/lucene/store/Directory : Unsupported major.minor ...