poj 1012 Joseph Joseph problems (★★ ☆☆☆)

http://poj.org/problem?id=1012

Joseph origins of the problem: 

  It is said that the famous Jewish historian Josephus had the following story: After the Roman occupation Qiaotapate, 39 Jews and Josephus and his friends hid in a cave, 39 Jews decide would rather die and do not get caught enemies and decided a suicide, 41 individuals arranged in a circle, the first personal Countin, every third number reported to the people who have to commit suicide, then a re-count by the newspaper, until all suicide killed so far. However, Josephus and his friends did not want to follow, Josephus wanted his friend to pretend to comply, he will arrange with his friends in the 16th and 31st position, then escaped this death game.

 

  This question is similar to this one Description: 17th-century French mathematician Gaspar told such a story in the "number of games Problems": 15 Christians and 15 non-believers in distress on the deep sea, half of them must be put into the sea, the rest of the people in order to survive, so the thought of a way: 30 people surrounded by a circle, sequentially from the first person count off each number to the ninth individual will he thrown into the sea, and so on until So far 15 people are down. Q. How platoon law, in order to make each into the sea are non-believers.

 

  For now this question, that is, k k a bad guy and good guy stand in a circle, the first k are good, from 1 Countin, reported m shot, the next number off again from the beginning, and so on! Seeking a number m, k when the rest of the people, they are good people meet

solution:

  Since 0 <k <14, so that, no matter what value when k, there will be a special case is that "only a bad guy" i.e., the remaining number n = k 1 +, the analysis of this particular we can.

   When n = k + 1, the next number of packets certain number of packets from the first 1 or k + 1 individual. Suppose the number of packets start from the first person, then there are m = (k + 1) * x, x> = 1; and when the number of packets from the first k + 1 of an individual, m = (k + 1) * x + 1, x> = 1.

Sample Input

3
4
0

Sample Output

5
30

 

Source Code
#include <iostream>
using namespace std;

int r[14];
bool solve(int k, int i){
	int n=k*2,m=i,x=0;
	while(n>k){
		x=(x+m-1)%n;
		if(x<k) return false;
		n--;
	}
	return true;
}
int main(){
	//freopen("C:\\Documents and Settings\\user01\\桌面\\in.txt","r",stdin);
	//freopen("C:\\Documents and Settings\\user01\\桌面\\out.txt","w",stdout);
	int i,j,k;
	for(k=1;k<14;k++){
		for(i=k+1;;i+=k+1){
			if(solve(k,i)){
				r[k]=i;
				break;
			}
			else if(solve(k,i+1)){
				r[k]=i+1;
				break;
			}
		}
	}

	while(cin>>k,k){
		cout<<r[k]<<endl;
	}
	return 0;
}

Reproduced in: https: //www.cnblogs.com/pcwl/archive/2011/04/26/2029188.html

Guess you like

Origin blog.csdn.net/weixin_34360651/article/details/92846578