Super Pow

来源:互联网 发布:围棋 人工智能算法 编辑:程序博客网 时间:2024/04/30 22:10

Super Pow

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.

Example1:

a = 2b = [3]Result: 8

Example2:

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

每一位一位的的求,当前位为之前所有位的结果的10次方再乘以a的当前位次方。


代码:

class Solution {public:    int spow(int a,int b)    {        int base=1337;        a%=1337;        int res=1;        for (int i=0; i<b; i++)        {            res*=a;            res%=base;        }        return res;    }    int superPow(int a, vector<int>& b) {        int ans=1;        int base=1337;        for (int i=0; i<b.size(); i++)        {            ans=(spow(ans,10)*spow(a,b[i]))%base;                    }        return ans;    }};



0 0
原创粉丝点击