LeetCode OJ - Minimum Path Sum
autekwing's blog. 此部落格分享我曾經學習的經驗與感興趣的事務。 This blog presents everything I am interested in and learned . 另外歡迎參觀我的另一個部落格! Hello Wing, Play Garden
有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
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.
2014年10月29日 星期三
2014年10月20日 星期一
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
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
訂閱:
文章 (Atom)