Main topic:
n the number of rows in a circle, the first time to delete m, after each k number deleted once, the last one to be deleted.
If the problem is to simulate the whole process with a linked list or an array, the time complexity will be up to O (NK), and the n<=10000,k<=10000 will be directly tle.
So is there any other way? The answer is yes.
We ignore the M first, analyze the number of each k deleted once, that is the classic Joseph problem.
Then, each number (1~n) is numbered sequentially 0~n-1
The number of the first deleted number is x, then x= K%n-1 (note is number, really deleted number is number + 1)
Then the remaining number of n-1 can form a new Joseph ring.
What is the number now? Obviously: (Make x+1=y, that is, y= k%n)
Y, y+1, y+2 ... n-1, 0, 1 ... y-2
Putting y in the first order is the next time you start counting from it.
Re-start number of k numbers.
You said re-, uh. Then you can renumber it like this:
Y-0
Y+1->1
Y+2->2
...
...
Y-2-N-2
Now it becomes the n-1 number (numbered from 0~n-2) of the Joseph Question!
Assuming that Z is the number left by the last n-1 number, then Z ' is the number left by the n person, then the z ' = (z+y)% n is clearly
How do you know the n-1 of a solution? Just give it a hand, you know n-2.
So, there are:
ans [1]=0;
Ans [n] = (ans[n-1]+k)%n;
(one might ask: Is it not z ' = (z+y)% n?) Now how did you become K? Because y= k%n, modulo operation)
Then, the answer is +1 (number and number)
So what about the first time this question is M?
Also very simple, we move k every time, there are n number, then the answer is Ans[n]
But the first move is M, so the trailing movement has a constant gap (K-M)
So the answer is: (ans[n]– (k–m) + 1)% n (note may be less than 0, this time to add N)
#include <iostream>#defineSize 1000005using namespacestd;intn,m,k;intF[size];intMain () {CIN>>n>>k>>m; f[1]=0; for(intI=2; i<=n;i++) {F[i]= (f[i-1]+K)%i; } intAns= (m-k+1+f[n])%N; if(ans<1) ans+=N; cout<<ans; return 0;}
Variant LA3882 of the Joseph question