Classic Joseph Ring problem. A little deformation. Enclose n people with a ring (Number 1 ~ N), starting from the M-th individual, the reporter reports the number once every k people, and the reporter leaves the ring.
Find the number of the remaining person.
Mathematical recursive solution of Joseph's problem:
(1) the number of the first objects to be deleted is (m-1) % N.
(2) assuming that the starting Number of the second round is K, the number of n-1 is K, k + 1, K + 2, K + 3 ,....., k-3, K-2. Perform a simple ing.
K -----> 0
K + 1 ------> 1
K + 2 ------> 2
...
...
K-2 ------> N-2
This is a problem of n-1 people. If a recursive formula can be obtained from the solution of n-1 people's problems, the problem will be solved. If we already know that the number of the winner in n-1 is X and the ing relationship is used for reverse pushing, we can conclude that the number of the winner in N is (x + k) % N. K is equal to M % N. (X + k) % n <=> (x + (M % N) % n <=> (X % N + (M % N) % n <=> (X % N + M % N) % n <=> (x + M) % N
(3) The second row is deleted (m-1) % (n-1 ).
(4) assume that the starting Number of the third round is O, and the number of N-2 is O, O + 1, O + 2 ,...... o-3, o-2 .. Continue with the ing.
O -----> 0
O + 1 ------> 1
O + 2 ------> 2
...
...
O-2 ------> n-3
This is a problem for n-2 people. Assume that the final winner is Y. When n-1 is selected, the winner is (Y + O) % (n-1), where o is M % (n-1 ). (Y + M) % (n-1)
To get a solution for n-1 people, you only need to get the solution for n-2 people and push it down. If there is only one person, the winner is numbered 0. The recursive formula is given below:
F [1] = 0;
F [I] = (F [I-1] + M) % I; (I> 1)
Joseph problem details Stamp: http://blog.csdn.net/wuzhekai1985/article/details/6628491
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
With the above recursive formula, do not rush to code it, ac ~
# Include <cstdio> # include <algorithm> using namespace STD; int main () {int n, m, K; while (~ Scanf ("% d", & N, & K, & M), M | K | N) {int S = 0; for (INT I = 2; I <= n-1; I ++) S = (S + k) % I; // you do not need to open an array. Printf ("% d \ n", (S + M) % N + 1);} return 0 ;}