有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.
顯示具有 Divide and Conquer 標籤的文章。 顯示所有文章
顯示具有 Divide and Conquer 標籤的文章。 顯示所有文章

2014年10月29日 星期三

LeetCode OJ - Minimum Path Sum

LeetCode OJ - Minimum Path Sum

2014年3月19日 星期三

UVa OJ - 374 Big Mod

The following program is my ACcepted code for UVA-374 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UVA 374 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )


在乘法時可以先把數字分開 mod,在乘起來 mod,這樣結果會一樣
Ex: B^P mod M = ( (B^(P-1) mod M) * (B mod M) ) mod M

所以我用Divide and Conquer來解決他
先把 B^P 分成 B^(P/2) * B^(P/2)。
(如果P為奇數就再乘一個 P)
用遞迴的方式求出答案
大概是這樣的感覺  B^P mod M = (B^(P/2) mod M) * (B^(P/2) mod M) mod M