https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4140
Originally was to write this study code yesterday, and then last night bedroom and power off, can not help to spit groove here, well, the bedroom every day power off.
Test instructions is the input n,k,m three number, n the number of rows into a circle, the first delete m, after each number of K number deleted once, the last one to be deleted.
To the point, previously wrote a list of Joseph problem, but here must be timed out. Later read some reference, finally understand how to do.
The number of n from the 0 array, the first time after the deletion of the number of M, that is, the array subscript m-1 is deleted, at this time, the array is labeled as the number of M re as position No. 0, re-form a ring, then the subscript labeled M of the number of the subscript changed to 0.
The next step is the inverse derivation process, x ' = (x+m)%n.
Using an array to record where the winner is, a[1] means the place where the winner is, A[2] is the position of the winner where there are two people left, from above, a[1]=0.
Each subsequent deletion is to repeat the last process, the final left of the person must be in the position No. 0, then you can always push up to find out where the winner begins.
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 Const intMAXN =10005;6 intA[MAXN];7 8 intMain ()9 {Ten intN, K, m; One while(Cin >> n >> k >> m && n && k &&m) A { -Memset (A,0,sizeof(a)); - for(inti =2; I < n; i++) theA[i] = (A[i-1] + k)%i; - intAns = (A[n-1] + m)%N; -cout << ans+1<<Endl; - } + return 0; -}
UVa 1394 The deformation of the Joseph problem