Joseph

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

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

比赛时候差一点就推出来了  自己比赛时太容易紧张了  尤其是跟队友一起时,唉  还是心态不行

题意:总共有2*k个人站成一个环,前k个人是好人,后k个人是坏人,现在要处决这2*k个人要求后k个坏人要在好人之前死,让找一个最小的m(按照圆圈数m个人第m个人处死,然后从这个处死的人的下一位为起点继续数m个以此类推,直至杀死所有人)使得坏人先死

题解:当前要处死的人为kill=(kill+m-1)%n   (n为当前活着的人的数量)如果kill等于0 证明是要处死第n个人所以kill=n

我直接用函数求解超时了  ,因为数据不大 所以我直接吧14个数打表输出了

#include<stdio.h>
#include<string.h>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 1100
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int f[20];
int s[20]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,13482720};
int fun(int k,int m)
{
int kill,i,j,n;
n=2*k;
kill=1;
for(i=1;i<=k;i++)
{
kill=(kill+m-1)%n;
if(kill==0)
kill=n;
else if(kill<=k)
return 0;
n--;
}
return 1;
}
int main()
{
int n,m,j,i,t,k;
while(scanf("%d",&k),k)
{
// for(i=1;i<=14;i++)
// {
// for(j=1;;j++)
// {
// if(fun(i,j))
// {
// f[i]=j;
// break;
// }
// }
// }
printf("%d\n",s[k]);
}
return 0;
}

  

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

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

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

  2. hdu 1443 Joseph【约瑟夫环】

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

  3. HDU 3089 (快速约瑟夫环)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...

  4. Hdu 1443 Joseph

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

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

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

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

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

  7. HDU 5643 King's Game 【约瑟夫环】

    题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...

  8. hdu 4841 圆桌问题(用vector模拟约瑟夫环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4841 圆桌问题 Time Limit: 3000/1000 MS (Java/Others)    M ...

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

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

随机推荐

  1. vi编辑器基本用法介绍

    vi是Linux系统中编写文件的工具 如果vi出现乱码情况,需要升级vi,命令如下: sudo apt-get install vim  //升级vi vi的启动方式有两种,直接使用vi命令和在vi命 ...

  2. Topcoder SRM 630 (500 floyed 暴力 _builtin_popcount())

    题意:给n个点,保证图联通,给点相连的距离,求一个最多的点,这些点之间的距离都是相同的. 分析: 下面的代码是我们房间第一的大神的,写的很简洁,我的思路和他的一样,但是我不知道错哪了. 思路是暴力枚举 ...

  3. velocity加减运算注意格式 ,加减号的左右都要有空格

    velocity加减运算注意格式 ,加减号的左右都要有空格 #set( $left= $!biz.value - $vMUtils.getReturnMoney($!biz.billBuy) )

  4. Qt之自定义界面(实现无边框、可移动)

    简述 UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性.有品位,还要让软件的操作变得舒适简单.自由,充分体现软件的定位和特点. 爱美之心人皆有之.其实软件 ...

  5. UVa 11995 I Can Guess the Data Structure!

    做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...

  6. WIN7开无线

    可以的,WIN7自身就带有这个功能的,不过默认是不开启的,也可以下载一个软件来弄.1.请却仍您使用的操作系统是微软的Windows 7或者Windows server 2008 R2,正版盗版皆可.( ...

  7. 四、oracle基本sql语句和函数详解

    一.oracle常用数据类型 一.  数据定义语言(ddl) 数据定义语言ddl(data definition language)用于改变数据库结构,包括创建.更改和删除数据库对象. 用于操纵表结构 ...

  8. XTUOJ 1252 Defense Tower 贪心

    题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252 思路:考虑每条边对玩家的伤害 假设连接的节点是u,v,破坏 ...

  9. Apache OFBiz 学习笔记 之 实体引擎

    1.概述     entity engine和常见的ORM有一点很大的不同,他的mapping object只有一个 GenericEntity,称它的entity engine 为adaptive ...

  10. IOS 屏幕截图 UIScrollview

    //截图UIView:截全图 -(UIImage*)captureView:(UIView *)theView{ CGRect rect = theView.frame; if ([theView i ...