美文网首页
Leetcode 372. Super Pow

Leetcode 372. Super Pow

作者: 刘宇轩Freeman | 来源:发表于2017-05-03 21:28 被阅读0次

    题目描述:

    Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

    One knowledge: ab % k = (a%k)(b%k)%k

    const int base = 1337;
    int powerMod(int a,int b){
        a %= base;
        int result = 1;
        for(int i = 0;i < b;i++){
            result = (result * a) % base;
        }
        return result;
    }
    
    int superPow(int a, vector<int>& b) {
        if(b.empty()){
            return 1;
        }
        int lastDigit = b.back();
        b.pop_back();
        return powerMod(superPow(a,b),10) * powerMod(a,lastDigit) % base;   
    }

    相关文章

      网友评论

          本文标题:Leetcode 372. Super Pow

          本文链接:https://www.haomeiwen.com/subject/nomltxtx.html