Current location - Training Enrollment Network - Mathematics courses - Mathematical cycle
Mathematical cycle
This problem can be solved mathematically.

There are n people (number 0~(n- 1)), start counting from 0, sign in (m- 1) and quit, and the rest will continue counting from 0? (When solving mathematically, pay attention to numbering from 0, because the remainder will get a 0 solution. )

Essentially, it is a kind of recursion, and there is a recursive relationship between the serial number of the left-behind personnel in N and the serial number of the left-behind personnel in n- 1.

Assuming that the kth person is removed, then

0, 1,2,3, ..., k-2, k- 1, k, ..., n-1/original sequence (1).

0, 1, 2, 3, ...,k-2,? ,k,...,n- 1? //Remove the kth person, that is, the person who removes the serial number k- 1 (2)

K, K+ 1, ..., N- 1, 0, 1 ..., K-2// Starting from the serial number k, report 0? (3)

0, 1, ...,n-k- 1,n-k,n-k+ 1,...,n-2? //quantity conversion, when the queue is n- 1 person (4)

After the transformation, it completely becomes a sub-problem of (n- 1) personal number. Note that (1) and (4) are the same question, and the difference is only the number of people. Comparing (4) and (3), it is not difficult to see that 0+k=k, 1+k=k+ 1, ..., the number after' 0' in (3), ((n-3)+k)% n = k-3. ( 1+k)%n=k+ 1,? Therefore, we can draw such a rule:

Let one number in (3) be x' and the corresponding number in (4) be x, then there is: x' = (x+k)% n.

When X is the last number of people left, when there is only 1 person left in the queue, obviously X = 0;; At this time, you can go back to the serial number of X for 2 people, the serial number of X for 3 people … until the serial number of X for N people, which is what you want.

The recursive method is expressed as follows:

# Contains? & ltstdio.h & gt

int? Master ()

{

int? m,N,s = 0;

scanf("%d%d ",& ampm & amp; n);

For what? (int? I = 2; I<= M;; ++i)

s =(s+N)% I;

printf("%d\n ",s+ 1);

Return? 0;

}