java 372. Super Pow.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 372. Super Pow.java相关的知识,希望对你有一定的参考价值。

// ab % k = (a%k)(b%k)%k
// a^1234567 % k = (a^1234560 % k) * (a^7 % k) % k = (a^123456 % k)^10 % k * (a^7 % k) % k
class Solution {
    private static final int base = 1337;
    // a ^ k mode 1337 where 0 <= k <= 10
    private int powMod(int a, int k) {
        a %= base; 
        int res = 1;
        for (int i = 0; i < k; i++) {
            res = (res * a) % base;
        }
        return res;
    }
    private int helper(int a, int[] b, int idx) {
        if (idx < 0) return 1;
        int last_digit = b[idx--];
        return powMod(helper(a, b, idx), 10) * powMod(a, last_digit) % base;
    }
    public int superPow(int a, int[] b) {
        if (b == null || b.length < 1) return 1;
        return helper(a, b, b.length - 1);
    }
}

以上是关于java 372. Super Pow.java的主要内容,如果未能解决你的问题,请参考以下文章

372. 超级次方快速幂

372. 超级次方快速幂

372. 超级次方快速幂

372. 超级次方

解题报告Leecode 372. 超级次方——Leecode每日一题系列

解题报告Leecode 372. 超级次方——Leecode每日一题系列