《具體數學》——遞迴問題

來源:互聯網
上載者:User

標籤:

  以後的搬磚轉化一下思路,以一本書為主體,進行類似讀書筆記式的討論和總結,首先便從這本高德納的《具體數學》開始吧。

  大致翻了一下這本書,能夠明顯感覺到大師所著的書和國內的一些出版物是沒辦法比的。看了一下這本書的序言,發現這本書有一個很大的特色就是“數學塗鴉”,好像是有一部分讀者在閱讀該書時的注釋被收錄到了這本書當中,這些塗鴉中不僅僅有俏皮的玩笑,還不乏精巧的解題方略,與作者在論述清晰優雅而又詼諧的語調融為一體,大概才看了一節,感覺《具體數學》的確是一本值得仔細研讀的好書。

  那麼下面我們便開始正式開始搬磚工作。

 

  約瑟夫環問題。

  這個問題有個很有意思的曆史起源:“傳說如果不是由於他的數學天賦,約瑟夫不會活到出名的那一 天.在猶太羅馬戰爭期間,他們41名猶太反抗者困在了羅馬人包圍的洞穴中.這些反抗者寧 願自殺也不願被活捉,於是決定圍成一個圓圈,並沿著圓圈每隔兩個人殺死一個人,直到剩 下後兩個人為止.但是,約瑟夫和一個未被告發的同謀者不希望無謂地自殺,於是他迅速 計算出他和其朋友在這個險惡的圓圈中應該站的位置。”

  我們先將將這個問題抽象化成數學模型:即將n個人圍成一圈,從n開始每隔1個人殺死一個,那麼求解最終活下來的那個人的起始編號?

  這個問題似乎進行類比運算是可解的,但是過程太過繁雜醜陋,我們需要一些更加漂亮的數學技巧。

  我們以n = 10為例來類比這個過程,我們繞著圓周篩選一次,則2、4、6、8、10被刪除,剩餘1、3、5、7、9,為了方便新一輪的篩選,我們此時進行重新編號。

  1 -> 1

  3 -> 2

  5 -> 3

  7 -> 4

  9 -> 5

  我們設定J(n)表示約瑟夫問題的解,通過上述問題的轉化我們發現,我們在類比求解J(n)的時候,產生了一個子問題,而這個子問題又可以轉化成另外一個完全獨立的問題,也就是在求解J(10)的過程中,我們形成了一個1、3、5、7、9作為環的子問題,而我們又將這個子問題轉化成了求解J(5),基於原問題和子問題具有同解性,再基於將子問題轉化成J(5)的過程,我們容易建立起J(10)和J(5)的遞推關係,即:J(10) = 2J(5) - 1。

  由此我們不難進行推廣,J(2n) = 2J(n) - 1。

  其實講到這裡,如果理解了的話,其實掌握瞭解決約瑟夫環問題的核心思維了,雖然此時我們對於奇數情況還沒有討論。

  我們基於這種思維,進行一個推廣,對於n個人,從1開始,每次刪掉m的整數倍,該如何求解呢?

  類似上文的思路,這次我們縮短我們的腳步,不要一次刪掉一圈再進行子問題轉化,而刪掉一個數後就進行子問題轉化。以n = 10,m = 3為例。

  第一次刪除:3。

  刪除後的結果:1、2、4、5、6、7、8、9、10。

  轉化子問題:1 -> 8

                   2 -> 9

                   4 -> 1

                   5 -> 2

                   6 -> 3

                   7 -> 4

                   8 -> 5

                   9 -> 6

                  10-> 7

  我們容易看到,J(10) = (J(9)   + 3) %10,我們也不難找到推廣規律——J(n) = (J(n-1) + m)%n。

  然後基於J(1) = 1,我們便可以對所有問題進行遞推求解。

  我們通過一個具體問題來應用一下這個思路。(Problem source : hdu 2925)

 

Problem DescriptionIn the traditional game of Musical Chairs, N + 1  children run around N  chairs (placed in a circle) as long as music is playing. The moment the music stops, children run and try to sit on an available chair. The child still standing leaves the game, a chair is removed, and the game continues with N  children. The last child to sit is the winner.In an attempt to create a similar game on these days‘ game consoles, you modify the game in the following manner: N Children are seated on N chairs arranged around a circle. The chairs are numbered from 1 to N . Your program pre-selects a positive number D . The program starts going in circles counting the children starting with the first chair. Once the count reaches D , that child leaves the game, removing his/her chair. The program starts counting again, beginning with the next chair in the circle. The last child remaining in the circle is the winner.
For example, consider the game illustrated in the figure above for N = 5 and D = 3 . In the figure, the dot indicates where counting starts and × indicates the child leaving. Starting off, child #3 leaves the game, and counting restarts with child #4. Child #1 is the second child to leave and counting restart with child #2 resulting in child #5 leaving. Child #2 is the last to leave, and child #4 is the winner. Write a program to determine the winning child given both N and D .  InputYour program will be tested on one or more test cases. Each test case specifies two positive integers N  and D  on a single line, separated by one or more spaces, where N, D < 1, 000, 000 .
The last line of the input file contains two 0‘s and is not part of the test cases.  OutputFor each test case, write the winner using the following format:
N D W
Where N and D are as above, is a space character, and W is the winner of that game.     容易看到,這道問題基於約瑟夫環問題在資料上做了小小的變動,由於總數是n+1,那麼我們便從0開始編號,即初始條件J(1) = 0。

《具體數學》——遞迴問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.