Description of the problem: a circle of known n individuals (denoted by number 1,2,3...N, respectively). From the number of people who numbered 1, the man who counted to M was out of the way, and his next man counted off from 1, and the person counting to M was out of the way, repeating the law, asking for the last person's number (until everyone is out, simulating the process)?
Workaround: This problem can be implemented with an array or linked list, which can be simplified mathematically (without the simulation process), or it can be simulated by the problem process.
Mathematical Analysis: 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, and time complexity of up to O (nm), when the n,m is very large (such as millions, tens of thousands), there is almost no way to produce results in a short period of time. The problem with Wakahara is simply to ask for the serial number of the last winner, not to simulate 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, the problem is changed slightly, does not affect the original intention:
Problem description: N Person (number 0~ (n-1)), starting from 0 count, reporting (M-1) exit, the remainder continues to count off from 0. The winner's number.
We know that the first person (the number must be m%n-1) after the dequeue, the rest of the n-1 individuals formed a new Joseph Ring (starting with the person numbered k=m%n):
K k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2 and reported 0 from K.
Now let's do a conversion of their numbers:
K--0
K+1-1
K+2-2
...
...
K-2-N-2
K-1-N-1
After the transformation has completely become the (n-1) personal count of the sub-problem, if we know the solution of this sub-problem: for example, X is the final winner, then according to the above table to turn this x back is not exactly the solution of n personal situation?! The formula to change back is very simple, I believe everyone can push out: X ' = (x+k)%n
How to Know (n-1) The solution of the problem of personal count off? 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! Okay, here's the idea, here's the recursive formula:
Make f[i] means I personally play the game reported M exit the last winner's number, the final result is naturally f[n]
Recursive formulas
f[1]=0;
f[i]= (f[i-1]+m)%i; (i>1)
With this formula, all we have to do is calculate the value of F[i] from the 1~n order, and the final result is f[n]. Because real life numbers always start from 1, we output f[n]+1
Because it is a step-by-step recursive, do not need to save each f[i], the program is also exceptionally simple.
int Josephus (int m,int n) { int result=0; for (int i=2;i<= n;i++) result= (result + m)% i; return result+1;}
Simulation Process Code:
int Josephus2 (int m,int n) { bool a[n+1]={0};//0 is represented in the ring int f=0,cnt=0,itr=0;//itr used to traverse, CNT count, F record out number while ( F<n) { ++itr; if (ITR > N) itr=1;//ring if (!a[itr]) cnt++;//in ring, Count if (cnt = = m) { cout<< "out--" <<itr< < "\ t"; A[ITR] = 1;//kicks out cnt = 0; f++; } } cout<<endl; return ITR;}
Joseph Question (Josephus)