美文网首页数据结构和算法分析
LeetCode 372. Super Pow 数学

LeetCode 372. Super Pow 数学

作者: Terence_F | 来源:发表于2016-08-04 14:24 被阅读324次

Super Pow
这道题只要知道

a ^ 666888 = a ^ 666880 * a ^ 8

我们用一个powmod函数可以一边做power运算一遍取膜 🐸

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

相关文章

网友评论

    本文标题:LeetCode 372. Super Pow 数学

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