Post: Joseph's Problem

Source: Internet
Author: User
Tags floor function
The original Article address and original author are unknown.

1. Origin of the problem

The Joseph us problem is named after Flavius Joseph PHUs, a famous historian in the 10th century. It is said that if Joseph had no mathematical skills, he would not be famous when he was alive! During the war between Jews and the ancient Romans, he was one of 41 Jewish rebels trapped in the Rome trap. the rebels would rather die than take prisoners. They decided to make a circle around the circle and kill all the 3rd remaining people until no one left. however, it is silly for Joseph and a non-informant to commit suicide, so he quickly calculates where he and his friends should stand in this vicious circle. so they survived... 2. ordinary Solution
 
We use a loop table to simulate their behavior. To save trouble, I directly found a java code:
 
Class Joseph PHUs
{
Static class Node
{
Int val; Node next;
Node (int v) {val = v ;}
}
Public static void main (String [] args)
{
Int N = Integer. parseInt (args [0]);
Int M = Integer. parseInt (args [1]);
 
Node t = new Node (1 );
Node x = t;
 
For (int I = 2; I <= N; x = (x. next = new Node (I ++ )));
X. next = t;
 
While (x! = X. next)
{
For (int I = 1; I <M; I ++) x = x. next;
X. next = x. next. next;
}
Out. println ("specified VOR is" + x. val );
}
}
 
3. Recursive Formula
 
Friends who like this question certainly don't satisfy the above method and want to know more simple algorithms.
In fact, there is indeed a recursive formula for the sequence in the Joseph's problem. But the derivation of Recursive Formulas
If it is troublesome, I will give the result directly. For details about the process, refer to relevant materials.
 
Suppose there are n people, and each time the m person is killed, k is the k person killed...
 
J1: x <-k * m
J2: if (x <= n) Input result x
J3: x <-floor (m * (x-n)-1)/m-1), goto j1
 
The C language is implemented as follows:
 
Unsigned Joseph PHUs (unsigned m, unsigned n, unsigned k)
{
Unsigned x = km;
While (x <= n) x = (m * (x-n)-1)/m-1 );
Return x;
} Where m is 2
 
Consider a special case where m is 2.
At this time, there is a simpler recursive formula:
 
X = 2 * n + 1-(2 * n + 1-2 * k) * 2 ^ log2 (2 * n) /(2 * n + 1-2 * k ))
 
Here, log2 (2 * n)/(2 * n + 1-2 * k) is calculated as (2 * n) /(2 * n + 1-2 * k) Base on 2 logarithm,
Returns an integer down.
 
Contact 2 ^ log2 (2 * n)/(2 * n + 1-2 * k) /(2 * n + 1-2 * k) Down
Returns the power of 2. In some cases, this operation is called a floor function. We define it as flp2. below is
C language implementation:
 
Unsigned flp2 (unsigned x)
{
Unsigned y;
Do {y = x; x & = X-1;} while (x );
Return y;
}
Where the x & = X-1; statement is to change the rightmost 1 of the x binary to 0 each time until the leftmost 1.
This method can also be used to calculate the number of 1 in x binary, when the number of 1 in x binary is relatively small
The algorithm efficiency is very high. Implementation of code with m 2: unsigned Joseph phus2k (unsigned n, unsigned k)
{
Unsiged t = (n <1)-(k <1) + 1;
Return (n <1) + 1-t * flp2 (n <1)/t );
} In the case where m is 2 and k is n, this problem is generally calculated based on the location of the last person killed.
Now we can consider the case where m is 2 and k is n. The formula that allows k = n to simplify the front edge m = 2: x = 2 * n + 1-(2 * n + 1-2 * n) * 2 ^ log2 (2 * n)/(2 * n + 1-2 * n ))
That is, x = 2 * n + 1-2 ^ log2 (2 * n) can be understood:
Shift n to 1 (that is, multiply by 2), and set the rightmost end to 1 (Add 1 ),
Finally, set the value of 1 on the left end to 0 (the power of 2 x n is subtracted from the power of 2 x n ). A simpler description is to move the Binary Expression of n to the right of a loop!
For example, if n is 1011001-> 0110011-> 110011, the code is unsigned Joseph phus2n (unsigned n)
{
Return (n-flp2 (n) <1) | 1;
}============================== Class Joseph
{
Static class Node
{
Int val; Node next;
Node (int v) {val = v ;}
}
Public static void main (String [] args)
{
Int N = Integer. parseInt (args [0]);
Int M = Integer. parseInt (args [1]); Node t = new Node (1 );
Node x = t; for (int I = 2; I <= N; x = (x. next = new Node (I ++ )));
X. next = t; while (x! = X. next)
{
For (int I = 1; I <M; I ++) x = x. next;
X. next = x. next. next;
}
Out. println ("specified VOR is" + x. val );
}
} Unsigned Joseph PHUs (unsigned m, unsigned n, unsigned k)
{
Unsigned x = km;
While (x <= n) x = (m * (x-n)-1)/m-1 );
Return x;
} Unsigned flp2 (unsigned x)
{
Unsigned y;
Do {y = x; x & = X-1;} while (x );
Return y;
} Unsigned Joseph phus2n (unsigned n)
{
Return (n-flp2 (n) <1) | 1;
} Unsigned Joseph phus2k (unsigned n, unsigned k)
{
Unsiged t = (n <1)-(k <1) + 1;
Return (n <1) + 1-t * flp2 (n <1)/t );
} Refer to knuth related books # include <iostream>
Using namespace std;
Typedef struct Joseph
{
Int value;
Joseph * next;
} JS, * pJS;
Int main ()
{
PJS l, p, q;
L = new JS;
L-> value = 1;
L-> next = l;
Q = l;
For (int I = 2; I <= 2000; I ++)
{
P = new JS;
P-> value = I;
P-> next = l;
Q-> next = p;
Q = q-> next;
} While (q-> next! = Q)
{
P = q-> next;
Q-> next = q-> next;
Q = q-> next;
Delete p;
}
Cout <q-> value <endl;
System ("pause ");
Return 0;} result: 1952

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.