372. Super Pow

来源:互联网 发布:ios软件开发语言 编辑:程序博客网 时间:2024/05/17 23:58

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

Example1:

a = 2b = [3]Result: 8

Example2:

a = 2b = [1,0]Result: 1024

Credits:
Special thanks to @Stomach_ache for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

class Solution {public:    static const  int  base = 1337;    int pown(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 k = b.back();        b.pop_back();        return pown(superPow(a,b),10)*pown(a,k)%base;    }};

0 0
原创粉丝点击