lintcode-快速幂-125

来源:互联网 发布:北京电视台网络直播 编辑:程序博客网 时间:2024/05/25 18:11

计算an % b其中a,b和n都是32位的整数。


样例

例如 231 % 3 = 2

例如 1001000 % 1000 = 0

挑战

O(logn)

class Solution {public:     int fastPower(int a, int b, int n) {        if(0==n)            return 1%b;        if(1==n)            return a%b;        long Pow=fastPower(a,b,n/2);        Pow=(Pow*Pow)%b;        if(n&1)            return (a*Pow)%b;        return Pow;    }};



0 0