The Josephus Problem
2008-09-26 16:31
429 查看
I recently find a class of questions called the josephus problem,all those questions is similar with the following:
long long ago,there was a king,he is a mathematician but crue and brutall ,and he always invited many people to his palace,and let them sat around a table,and assigned everyone a number,assume there were 20 people,the numner is form 1 to 20.then the king counted them from nunber 1,when the total reached to 3,he will kill the person,and continue this from the next person ,and the result from 1,and when there were only 1 person, he will get 1 billion coins.if you were one of those people,which number you will sit to alive and get the 1 billion coins?
This type of problem is called a "Josephus problem", after a story about a historian of the first century, Flavius Josephus, who survived the Jewish-Roman war perhaps due to his mathematical talents. In his book The Jewish Wars Flavius tells that he was one out of 41 Jewish rebels trapped by the Romans. His companions preferred suicide to escape, so they decided to draw lots to see who would kill whom so that they could avoid both capture and the sin of suicide. The idea that they decided to form a cycle and to kill every third person and to proceed around the circle until no one was left is probably a myth. According to the problem Josephus wasn't excited by the idea of killing himself, so he calculated where he has to stand to survive the vicious circle. The general Josephus problem; involves how many people will stand around a circle and how many shall be passed over before the next one is killed.
We can consider this question in this way: if there were only one person,he is the winner(he will get the 1 billion coins or not dead),his number is 1 in the circle,and we assume there were 2 people,the winner's number is 2 in this circle.
Suppose there have n people,we list them beginning with 0 as the following:
0 1 2 3 ....n-2 n-1 we assume the step is m which means we will kick the m person off.then his number must be (m-1)%n,and then left n-1 people,we also assume his number is k,and then the list is 0 1 2 ...k-1 k+1 ...n-2 ;we can change its order to k+1 k+2..1 2 ...n-2,now we assume again,the winner's number is x,What is the number of the winner in preceding cirle? I think this is very easy,the number equals to (k+x+1)%n,we know k=(m-1)%n,so the number of winner also equals to (x+m)%n.
From preceding deducing,we can get the winner's number of n people circle from n-1 people circle.and the 1 people circle winner's is number 0 according to the preceding deducing.So this is a recursion:
the number we count is 3,which is also the m of the preceding paragraph.
total index number
1 0 1
2 1 2
3 1 2
4 0 1
5 3 4
The programm in C++ programming language is the following:
#include<iostream.h>
void main()
{
int total,step,i,result=0;
cin>>total>>step;
for(i=2;i<=total;i++)
{
result = (result+step)%i;
}
cout<<"the winner is:"<<result+1<<endl;
}
Date:2008-09-26
Place:Neusoft F3
long long ago,there was a king,he is a mathematician but crue and brutall ,and he always invited many people to his palace,and let them sat around a table,and assigned everyone a number,assume there were 20 people,the numner is form 1 to 20.then the king counted them from nunber 1,when the total reached to 3,he will kill the person,and continue this from the next person ,and the result from 1,and when there were only 1 person, he will get 1 billion coins.if you were one of those people,which number you will sit to alive and get the 1 billion coins?
This type of problem is called a "Josephus problem", after a story about a historian of the first century, Flavius Josephus, who survived the Jewish-Roman war perhaps due to his mathematical talents. In his book The Jewish Wars Flavius tells that he was one out of 41 Jewish rebels trapped by the Romans. His companions preferred suicide to escape, so they decided to draw lots to see who would kill whom so that they could avoid both capture and the sin of suicide. The idea that they decided to form a cycle and to kill every third person and to proceed around the circle until no one was left is probably a myth. According to the problem Josephus wasn't excited by the idea of killing himself, so he calculated where he has to stand to survive the vicious circle. The general Josephus problem; involves how many people will stand around a circle and how many shall be passed over before the next one is killed.
We can consider this question in this way: if there were only one person,he is the winner(he will get the 1 billion coins or not dead),his number is 1 in the circle,and we assume there were 2 people,the winner's number is 2 in this circle.
Suppose there have n people,we list them beginning with 0 as the following:
0 1 2 3 ....n-2 n-1 we assume the step is m which means we will kick the m person off.then his number must be (m-1)%n,and then left n-1 people,we also assume his number is k,and then the list is 0 1 2 ...k-1 k+1 ...n-2 ;we can change its order to k+1 k+2..1 2 ...n-2,now we assume again,the winner's number is x,What is the number of the winner in preceding cirle? I think this is very easy,the number equals to (k+x+1)%n,we know k=(m-1)%n,so the number of winner also equals to (x+m)%n.
From preceding deducing,we can get the winner's number of n people circle from n-1 people circle.and the 1 people circle winner's is number 0 according to the preceding deducing.So this is a recursion:
the number we count is 3,which is also the m of the preceding paragraph.
total index number
1 0 1
2 1 2
3 1 2
4 0 1
5 3 4
The programm in C++ programming language is the following:
#include<iostream.h>
void main()
{
int total,step,i,result=0;
cin>>total>>step;
for(i=2;i<=total;i++)
{
result = (result+step)%i;
}
cout<<"the winner is:"<<result+1<<endl;
}
Date:2008-09-26
Place:Neusoft F3
相关文章推荐
- 约瑟夫问题 The Josephus Problem 非递归算法求解
- The Josephus Problem
- The Secret To 10 Million Concurrent Connections -The Kernel Is The Problem, Not The Solution
- A1031 The 3n+1 problem (3n+1 问题)
- Error: Socket descriptor not found. Hint: the problem might be solved applyi...
- hdoj1048 The Hardest Problem Ever(字符转换)
- uva100 The 3n + 1 problem
- poj 3100 && zoj 2818 ( Root of the Problem ) (睡前一水)
- 机器学习基石-01-the learning problem
- Office 2007 输入中文 或 打开故障 The setup controller has encountered a problem during install
- The ? 1 ? 2 ? ... ? n = k problem
- hackerrank The Coin Change Problem(dp)
- The problem of English OS show chinese.
- UVa Problem 10195 The Knights of the Round Table (圆桌骑士)
- The 2012 ACM-ICPC Asia Changchun Regional Contest(problem E)
- hdu 3666 THE MATRIX PROBLEM (差分约束)
- The 1 Pixel Rounding Error Problem
- UVa Problem 10215 The Largest/Smallest Box... (最大/最小的盒子)
- The 2012 ACM-ICPC Asia Changchun Regional Contest(problem B)
- The 3n+1 Problem(求各位高手修改)