Joseph Question, Joseph Ring

Source: Internet
Author: User

The question of Joseph (sometimes also called Josephus permutation) is a problem that appears in computer science and mathematics. In computer programming algorithms, a similar problem is called Joseph Ring. Also known as "drop handkerchief Problem".)

1Problem HistoryEditIt is said that the famous Jewish historian Josephus had the following story: After the Romans occupied Chotapat, 39 Jews and Josephus and his friends hid in a hole, 39 Jews decided to prefer to die not to be caught by the enemy, so decided a suicide, 41 people into a circle, By the 1th person starts to count, each count to the 3rd person The person must commit suicide, and then again by the next re-count, until everybody commits suicide to die. However, Josephus and his friends did not want to follow. First begins with a person, crosses the k-2 individual (because the first person has been crossed), and kills the kPersonal. Then, crossing the k-1 individual, and killing the first kPersonal. The process continues along the circle until only one person remains in the end, and the person can continue to live. The question is, given and, where do you stand at the outset to avoid being executed? Josephus to his friends to pretend to obey, he put friends and himself in the 16th and 31st position, so escaped the game of death.[1]The 17th century French mathematician Gaspar in the "number of game questions" to tell a story: 15 believers and 15 non-believers in the deep sea distress, must be half of the people into the sea, the rest of the people can survive, and then think of a way: 30 people in a circle, starting from the first person to count off, Each number to the nineth person throws him into the sea, so that the cycle lasts until only 15 people. Ask how to arrange the law, so that every time into the sea is non-believers. * Problem analysis and algorithm design Joseph's problem is not difficult, but there are many ways to solve it, and there are many variations of the topic. Here is a way to implement it. 30 people in a circle, so that inspired us to use a loop chain to represent, you can use a structure array to form a loop chain. There are two members in the structure, one of which is the pointer to the next person to form the chain of rings, and the other is the sign that the person is thrown into the sea, and 1 is still on board. From the first person began to count the people who have not left the sea, every number to 9 o'clock, the structure of the mark to 0, indicating that the person has been thrown overboard. This cycle counts until 15 people are thrown overboard.General FormEditJoseph's problem is a well-known problem: N people in a circle, from the first start to count, the number of M will be killed, the last one, the rest will be killed. For example N=6,m=5, the order of being killed is: 5,4,6,2,3,1. Workaround method One: With a linked list ring: First construct the chain list ring, then delete the M element, and then continue to the number. Use shoot to represent 1~m loops.
int josephus1 (int n,int m) {if (n<1| | m<1) {return-1;} ListNode *head=creat (n); int shoot=1; ListNode *p=head,*s=p;  while (length (p) >1) {if (shoot++==m) {s=p->next;  Remove (Head,p->val);  cout<<p->val<< "and";  Shoot=1; P=s;             } else {head=p; p=p->next; }} cout<
Method Two: Use the C++,list container to simulate the ring, where there is no next. Implemented with the Iterator + +, the code is as follows:
int josephus2 (int n,int m) {if (n<1| | m<1) {return-1;} list<int> Li; for (int i=1;i<=n;i++) {li.push_back (i);} list<int>:: Iterator Iter=li.begin (); while (Li.size () >1) {for (int i=1;i<m;i++) {iter++; if (Iter==li.end ()) {Iter=li.begin ();}} list<int>:: I Terator Next=++iter; if (Next==li.end ()) {Next=li.begin ();}--iter;  cout<<* (ITER) << "--"; Li.erase (ITER); Iter=next; } cout<<* (ITER) <<endl; return * (ITER); }


Method Three: Whether using a linked list or an array implementation has one thing in common: to simulate the entire game process, not only the program is more annoying to write, And the time complexity is up to O (nm), the space complexity is O (n), when the n,m is very large (for example, million, tens of millions), there is almost no way to produce results in a short period of time. We noticed that the original problem was merely to ask for the serial number of the last winner, rather than to mock the whole process. Therefore, if we want to pursue efficiency, we must break the routine and implement a mathematical strategy. In order to discuss the convenience, first change the problem slightly, does not affect the original intention: The problem description: N Individual (number 0~ (n-1)), starting from 0 count, check-in (m-1) exit, the remainder continues to count from 0. The winner's number. We know the first person (the number must be (m-1) mod n) after the dequeue, the remaining n-1 individuals make up a new Joseph ring (starting with the person numbered k=m mod n): K k+1 k+2 ... n-2,n-1,0,1,2,... k-2 and reported from K to 0. So according to the above table to turn this x back is not just the solution of n personal situation?! The formula to change back is very simple, I believe everyone can push out: X ' = (x+k) mod n How to Know (n-1) The solution of the problem of the personal count? Yes, as long as you Know (n-2) the individual's solution. (n-2) A personal solution? Of course, the first thing to ask (n-3)----This is obviously a backward problem! Well, the idea came out, the following write the recursive formula: Make F to show I personally play the game reported M exit the last winner's number, the final result is naturally f[n] recursive formula f[1]=0;f= (f+m) mod i; (i>1) With this formula, all we have to do is to calculate the value of F from the 1-n order, and the final result is f[n]. Because the actual life of the numbering always starting from 1, we output f[n]+1 because it is progressive recursion, do not need to save each F, the program is also exceptionally simple: The code is as follows:
int Josephus (int n,int m) {if (n<1| | m<1) {return-1;} int f=0; for (int i=1;i<=n;i++) {  f= (f+m)%i;} return f+1;}
The time complexity of this method is O (n) and the spatial complexity is O (1). The disadvantage is that the process output cannot be implemented, only the value of the last element can be evaluated.
The complete code is as follows, where int josephus2 (int n,int m) and int josephus3 (int n,int m) are two of the same ideas that are implemented using the list container, with different operating codes.
#include <iostream> #include <list> #include <iterator >using namespace Std;struct listnode{int val; ListNode *next;}; int Josephus (int n,int m); int josephus1 (int n,int m); ListNode *creat (int n); ListNode *remove (ListNode *head,int val); int Length (listnode* head); int josephus2 (int n,int m); int josephus3 (int n,int m); void main () {int n=41;int M=3;cout<<josephus (n,m) <<endl;cout<<josephus1 ( n,m) <<endl;cout<<josephus2 (n,m) <<endl;cout<<josephus3 (n,m) <<endl;} int Josephus (int n,int m) {if (n<1| | m<1) {return-1;} int f=0; for (int i=1;i<=n;i++) {f= (f+m)%i;} return f+1; } int josephus1 (int n,int m) {if (n<1| | m<1) {return-1;} ListNode *head=creat (n); int shoot=1; ListNode *p=head,*s=p;  while (length (p) >1) {if (shoot++==m) {s=p->next;  Remove (Head,p->val);  cout<<p->val<< "and";  Shoot=1; P=s;             } else {head=p; p=p->next; }} cout<
The operating structure is as follows:

Joseph Question, Joseph Ring

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.