//////////////////////////////////////// ////////////
// 1088 Joseph's Problem
// Here I use simplifiedAlgorithm, Refer to the following description, but it is a bit different to delete the first one, starting from the second,
// Loop n-1 times (because the first entry has been removed), and the first entry ends.
# Include <iostream>
Using namespace STD;
Int main ()
{
Int N;
While (CIN> N & n! = 0)
{
Int I;
Int ans;
For (I = 2; I <= 32767; I ++)
{
Ans = 0;
Int J;
For (j = 2; j <n; j ++) // corresponds to this for (I = 2; I <= N; I ++) S = (S + M) % I; Difference
Ans = (ANS + I) % J;
If (ANS = 0) // It is equivalent to the end when ANS + 1 = 1
{
Cout <I <Endl;
Break;
}
}
}
Return 0;
}
/* Online copy problem Algorithm
Optimization of Joseph's ring Problem
Both linked list implementation and array implementation have one thing in common: to simulate the entire game processProgramWriting is annoying, and the time complexity is as high as O (Nm). When n and m are very large (for example, millions or tens of millions, there is almost no way to get results in a short period of time. We noticed that the original problem only requires the serial number of the final winner, instead of simulating the whole process. Therefore, if efficiency is to be pursued, we must break the regular rules and implement some mathematical strategies.
To facilitate the discussion, change the problem slightly without affecting the original intention:
Problem description: N people (No. 0 ~ (N-1), reporting starts from 0, reporting ends from m-1), and reporting continues from 0. Calculate the number of the winner.
We know that after the first person (number must be M % N-1) is listed, the remaining n-1 person forms a new Joseph ring (starting with a person numbered K = m % N ):
K + 1 K + 2... N-2, n-1, 0, 1, 2,... K-2
And 0 is reported from K.
Now let's convert their numbers:
K --> 0
K + 1 --> 1
K + 2 --> 2
...
...
K-2> N-2
K-1 --> n-1
After the transformation, it will completely become a subproblem of (n-1) the number of individual reports. If we know the solution of this subproblem: for example, X is the final winner, so it's just n people's situation to change X back Based on the table above ?!! The formula for changing back is very simple. I believe everyone can introduce it: x' = (x + k) % N
How can I solve the problem of (n-1) number of personal reports? Yes, as long as you know (n-2) the individual's solution. (N-2) What about personal solutions? Of course it is the first (n-3) situation ---- this is obviously a reverse push problem! Well, the idea is coming out. The recursive formula is as follows:
F [I] indicates that I personally play the game and report m to exit the number of the final winner. The final result is f [N].
Recurrence Formula
F [1] = 0;
F [I] = (F [I-1] + M) % I; (I> 1)
With this formula, we need to calculate the value of F [I] From the 1-N order, and the final result is f [N]. Because the number in real life always starts from 1, we Output F [N] + 1
Because it is a step-by-step recursion, it does not need to save every f [I], and the program is also very simple:
# Include <stdio. h>
Main ()
{
Int n, m, I, S = 0;
Printf ("n m ="); scanf ("% d", & N, & M );
For (I = 2; I <= N; I ++) S = (S + M) % I;
Printf ("the winner is % d \ n", S + 1 );
}
The time complexity of this algorithm is O (n), which has greatly improved compared with the simulation algorithm. N, m is equal to 1 million, and 10 million is not a problem. It can be seen that the proper use of mathematical strategies not only makes programming simple, but also improves Algorithm Execution efficiency exponentially.
Note: m = 2 Joseph's question is introduced in knuth's book specific mathematics. It adopts a more efficient method and can express the result in an algebraic way. Similar to the recursive formula I have introduced m> 2, but it cannot be converted into a single algebraic formula, and it may be difficult to implement the program.
* Improved recursive formula
(1) I <m when f [I] = (F [I-1] + M) % I;
(2) When I> = m, I = km + R (0 <= r <m) then f [I] = f [km + R] = f [K (m-1) + R]-R + floor (f [m-1) + R]-R-1) m-1 ))
*/