Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1012
I tried ACM for the first time today and picked out the seemingly simplest Joseph question. Who knows the process is rough? I submitted it n times and finally got it done. First, I often use a circular linked list to do the following code: # Include <iostream> <br/> # include <vector> <br/> using namespace STD; </P> <p> class node <br/> {<br/> Public: <br/> int data; <br/> node * next; <br/> node () {}< br/> node (int d = 0, node * n = NULL): Data (D), next (N) {}< br/> ~ Node () {Delete next ;}< br/>}; </P> <p> bool execute (int m, int K) <br/>{< br/> int I; <br/> int kcnt = 0; <br/> node * head = new node (1 ); <br/> node * P = head; <br/> node * s; <br/> for (I = 2; I <= 2 * k; I ++) <br/>{< br/> P-> next = new node (I); <br/> P = p-> next; <br/>}< br/> P-> next = head; </P> <p> while (kcnt <K) // start to kill <br/>{< br/> for (I = 0; I <m; I ++) <br/>{< br/> S = P; <br/> P = p-> next; <br/>}< br/> S-> next = p-> next; <br/> kcnt ++; <br /> If (P-> data) <= k) <br/> return false; <br/>}< br/> return true; <br/>}</P> <p> int Joseph (int K) // choose the minimum m that satisfy the requirements <br/>{< br/> int m; <br/> for (M = k + 1; m ++) <br/> {<br/> If (execute (M, k )) <br/> break; <br/>}< br/> return m; <br/>}</P> <p> int main () <br/>{< br/> int K, I; <br/> vector <int> m; <br/> CIN> K; <br/> while (K! = 0) <br/>{< br/> M. push_back (Joseph (k); <br/> CIN> K; <br/>}< br/> for (I = 0; I <m. size (); I ++) <br/> cout <m [I] <Endl; <br/>}< br/>Error: limited memory exceeded. It should be because too much memory space has been applied for and has not been released. Next, I often use arrays to do this. It is also because memory is too large, it turns out that I forgot to delete the space of the new array generated every time. After I added Delete [] P, I reported an error. It was time limited exceeded and I modified the program for a long time, even arrays are useless, but it still takes too long. Later, after reading the discussion, we found that the data will be duplicated, so I added an mem [] array, it is used to save the previously calculated value. When this value is used for the next time, it is called directly. In this way, the Code successfully submitted is as follows: # Include <iostream> <br/> # include <vector> <br/> using namespace STD; </P> <p> int mem [14] = {0 }; </P> <p> int Joseph (int K) // choose the minimum m that satisfy the requirements <br/>{< br/> If (MEM [k]! = 0) <br/> return mem [k]; <br/> int M, CNT, SP; <br/> for (M = k + 1 ;; M ++) <br/>{< br/> CNT = K * 2; <br/> sp = 0; <br/> while (CNT> K) // start killing <br/>{< br/> sp = (SP + m-1) % CNT; <br/> If (sp <K) <br/> CNT = 0; <br/> CNT --; <br/>}< br/> If (CNT = K) <br/>{< br/> mem [k] = m; <br/> return m; <br/>}< br/> return 0; <br/>}</P> <p> int main () <br/>{< br/> int K, I; <br/> vector <int> m; <br/> CIN> K; <br/> while (K! = 0) <br/>{< br/> M. push_back (Joseph (k); <br/> CIN> K; <br/>}< br/> for (I = 0; I <m. size (); I ++) <br/> cout <m [I] <Endl; <br/>}However, I always think this method is a bit difficult. Do masters have other methods ?? Appendix: Joseph
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:28476 |
|
Accepted:10625 |
Description
The Joseph's problem is notoriously known. for those who are not familiar with the original problem: From among N people, numbered 1, 2 ,..., n, standing in circle every MTH is going to be executed and only the life of the last remaining person will be saved. joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. for example when n = 6 and M = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are K good guys and K bad guys. in the circle the first K are good guys and the last K bad guys. you have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing K. The last line in the input file contains 0. You can suppose that 0 <k <14.
Output
The output file will consist of separate lines containing M corresponding to K in the input file.
Sample Input
340
Sample output
530