hdu 1443 Joseph (约瑟夫环)

Posted 非我非非我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1443 Joseph (约瑟夫环)相关的知识,希望对你有一定的参考价值。

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 (约瑟夫环)的主要内容,如果未能解决你的问题,请参考以下文章

约瑟夫环(Joseph)编程内容

Joseph_Circle(约瑟夫环)

Joseph POJ - 1012 约瑟夫环递推

POJ 1012 Joseph(打表题)

北大ACM(POJ1012-Joseph)

Java-POJ1012-Joseph