Josephus: numbering starts at 0, the first people out there who are (k-1)% n, renumbered people out there who's next numbered 0, and so on, the last one out of a certain number of people to 0, f [1] = 0; when the first person out, the remaining n - 1 numbers out individual f [9] = (k - 1)% (n - 1), the queue restore the original number (f [n - 1] + k)% (n - 1 + 1);
- Numbering starts at 0
- Each person went out a renumbering
- Restore the original arrangement of the formula: f [x] = (f [x] + k)% (x + 1) (+ k Sagamihara continue until the original number of +1 the number of + 1 = n);
The initial value was
n individuals, the number of number k
The first out: left people n; Initial value: f [n] = (k - 1)% n;
The second out: remaining n - 1 person; Initial value: f [n - 1] = (k - 1)% (n - 1);
Third out: remaining n - 2 persons; Initial value: f [n - 2] = (k - 1)% (n - 2);
。。。
Restore original arrangement number
With the initial value, then you can restore the number
First out: n left people without reduction
The second out: remaining n - 1 person, f [n - 1] = (f [n - 1] + k)% n;
Third out: remaining n - 2 persons, f [n - 2] = (f [n - 2] + k)% (n - 1), f [n - 2] = (f [n - 2] + k)% n;
。。。
Is to keep the number of mold + k + 1; until the person to n
Code
#include <cstdio> #define N 100001 int n-, K; int F [N]; int main () { Scanf ( " % D% D " , & n-, & K); for ( int I = n-; I> = . 1 ; - I) { F [I] = (K - . 1 )% I; for ( int J = I + . 1 ; J <= n-; ++ J) F [I] = (F [I] + K )% J; the printf ( " % D " , F [I] + . 1 ); // output number +1, as numbered from 0 } return 0; }