lintcode-快速幂-125

计算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;
    }
};



你可能感兴趣的:(lintcode-快速幂-125)