1 / 25

305 - Joseph

305 - Joseph. 約瑟夫問題. 資科二 林鼎威 99703012 資科二 蔡長霖 99703039 資 科二 陳柏翰 99703040. 題目.

shira
Download Presentation

305 - Joseph

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 305 - Joseph 約瑟夫問題 資科二 林鼎威 99703012 資科二 蔡長霖 99703039 資科二 陳柏翰 99703040

  2. 題目 • 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.

  3. Joseph's problem • 有N個人排成一圈,以m個一數,數到的那個人就要被處死 • 若是數到的人已經死亡,就往下跳一個人繼續

  4. 舉例: 如果有六個人 四個一數 1 1 2 2 6 3 3 5 4 4

  5. 舉例: 如果有六個人 四個一數 3 1 4 2 2 6 1 3 5 4

  6. 舉例: 如果有六個人 四個一數 4 1 3 2 6 1 2 3 5 4

  7. 舉例: 如果有六個人 四個一數 1 3 2 6 4 1 2 3 5 4

  8. 舉例: 如果有六個人 四個一數 1 2 4 2 6 1 3 3 5 4

  9. 舉例: 如果有六個人 四個一數 1 2 6 3 5 4

  10. 問題 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 K(0<k<14) • Output m

  11. 舉例: k=3 ,when m=1,2,3 ERROR : m <= k G G B G B B

  12. 舉例: k=3 ,when m=4 1 G 2 G B 3 G B 4 B

  13. 舉例: k=3 ,when m=4 3 ERROR G 4 2 G B 1 G B B

  14. 舉例: k=3 ,when m=5 1 G 2 G B 3 5 G B 4 B

  15. 舉例: k=3 ,when m=5 2 G 3 1 G B 4 G B 5 B

  16. 舉例: k=3 ,when m=5 2 G 3 1 5 G B 4 G B B

  17. 想法 • 好人是前k個,壞人是後k個,所以說一開始m會從k+1開始算 • 以排列來看的話,如果原本是GGGBBB,殺掉一個壞人之後將後面一個往前移動會變成GGGBB • 所以說當我每次將數到的數字+(m-1)再%(現有人數)就會知道這次是要殺好人還是壞人 • 當人口為k值時,就可跳出迴圈傳回m值。當迴圈結束,而人口不為k值,表示殺錯人了,m + 1從頭繼續運算。

  18. 如果k=3也就是數列為GGGBBB( 0 1 2 3 4 5) 假如一開始就設m=5步驟如下 第一次殺掉的為 ( 0+(5-1) )%6=4 剩下數列為GGGBB(0 1 2 3 4) 第二次殺掉的為( 4+(5-1) )%5= 3 剩下數列為GGGB(0 1 2 3) 第三次殺掉的為( 3+(5-1) ) %4=3 最後數列就只剩下GGG

  19. Pesudo Code for m = k+1to ∞ for p = 0 , r = 2*kto k if ((p = (p + m - 1) % r) < k) break; if (r == k) return m;

  20. Analyzing Joseph #include <stdio.h> #include <stdlib.h> intJoseph (int k) { int m, n, p, r; for (m = k+1;; m++) { for (p = 0, r = 2*k; r > k; r--) if ((p = (p + m - 1) % r) < k) break; if (r == k) return m; } }

  21. Analyzing Joseph int main(){ int k; scanf("%d",&k); printf("%d\n", Joseph(k)); system("PAUSE"); return 0; }

  22. Analyzing Joseph In function Joseph: 1 for (m = k+1;; m++){ 2 for (p = 0, r = 2*k; r > k; r--) 3 if ((p = (p + m - 1) % r) < k) break; 4 if (r == k) return m

  23. Analyzing Joseph In main: 5 scanf("%d",&k) 6 printf("%d\n", Joseph(k));

  24. The Running Time T(n) = C1(n) + C2(n^2) + C3((n^2)-1) + C4(n-1) + C5 + C6 = C1n + C2n^2 + C3n^2 – C3 + C4n – C4 + C5 + C6 = (C2 + C3)n^2 + (C1 + C4)n + (C5 + C6 – C4 – C3) => O(n^2)

  25. The EndThank You

More Related