[leetcode] 372. Super Pow 解题报告

来源:互联网 发布:linux alias 多条命令 编辑:程序博客网 时间:2024/05/16 18:40

题目链接: https://leetcode.com/problems/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

思路: 需要用到的数学知识

1. a^b % 1337 = (a%1337)^b % 1337

2. xy % 1337 = ((x%1337) * (y%1337)) % 1337, 其中xy是一个数字如:45, 98等等

其中第一个公式可以用来削减a的值, 第二个公式可以将数组一位位的计算, 比如 12345^678, 首先12345可以先除余1337, 设结果为X, 则原式就可以化为: 

X^678 = ((X^670 % 1337) * (X^8 % 1337)) % 1337 = (pow((X^670 % 1337), 10) * (X^8 % 1337)) % 1337

在上面我用了一个pow来化简表示 X^670 = pow(X^670, 10), 当然不是库函数里面pow, 因为会超出界限, 因此我们需要自己在写一个pow来一个个的边乘边除余.

代码如下:

class Solution {public:    int superPow(int a, int k)    {        if(k ==0) return 1;        int ans = 1;        for(int i = 1; i <= k; i++) ans = (ans*a) %1337;        return ans;    }        int superPow(int a, vector<int>& b) {        if(b.empty()) return 1;        a = a%1337;        int lastBit = b.back();        b.pop_back();        return (superPow(superPow(a, b), 10) * superPow(a, lastBit))%1337;    }};
参考: https://discuss.leetcode.com/topic/50489/c-clean-and-short-solution/2

1 1
原创粉丝点击