Current location - Training Enrollment Network - Mathematics courses - Why don't some values encountered in closed-loop method need to be rotated?
Why don't some values encountered in closed-loop method need to be rotated?
When the closed-loop method (also known as Joseph ring) is used to solve the Joseph ring problem, it is sometimes unnecessary to rotate the numerical value. This is because josephus problem is a divisor problem, that is, the $n$ th element is deleted from a sequence at a time. If the position of the current element is $k$, then the position of the next element to be deleted is $(k+n- 1)%n+ 1$ (where $% $ stands for modular operation).

Suppose there are currently 5 elements, the deletion interval is 3, and the sequence to be deleted is 3, 6, 2, 5, 1 (note that the number here starts from 1). If you delete in this order, you will find that some elements have not been transferred. For example, after the first round of deletion, the third element will be deleted, but it will not be turned around. This is because after the first round of deletion, the position of the third element becomes the second position, so it will not rotate in the next round of deletion.

Similarly, if there are currently $n$ elements and the deletion interval is $n$, no elements will be transferred, because the positions of all elements will remain the same after each round of deletion.

If we want to find the last remaining element, we can use the following code:

int n,m;

int findLast(int n,int m) {

int RES = 0;

for(int I = 2; I < = n; i++) {

RES =(RES+m)% I;

}

Return to res

}

Where n represents the number of elements and m represents the deletion interval. The return value of this function is the position of the last remaining element (note that it is numbered from 0).

For example, if there are five elements and the deletion interval is 3, the position of the last remaining element is findlast (5,3) and the return value is 3.

I hope these contents can help you!