美文网首页
[刷题防痴呆] 0372 - 超级次方 (Super Pow)

[刷题防痴呆] 0372 - 超级次方 (Super Pow)

作者: 西出玉门东望长安 | 来源:发表于2021-11-14 10:52 被阅读0次

题目地址

https://leetcode.com/problems/super-pow/description/

题目描述

372. 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.

Example 1:

Input: a = 2, b = [3]
Output: 8
Example 2:

Input: a = 2, b = [1,0]
Output: 1024

思路

一位位计算即可.

关键点

代码

  • 语言支持:Java
class Solution {
    
    int mod = 1337;
    
    public int superPow(int a, int[] b) {
        a = a % mod;
        int res = 1;
        for (int i = 0; i < b.length; i++) {
            res = pow10(res) * pow(a, b[i]) % mod;
        }
        
        return res;
    }
    
    private int pow10(int n) {
        int num = n;
        for (int i = 0; i < 3; i++) {
            num = (num * num) % mod;
        }
        
        int pow2 = n * n % mod;
        return pow2 * num % mod;
    }
    
    private int pow(int n, int p) {
        int res = 1;
        for (int i = 0; i < p; i++) {
            res = res * n % mod;
        }
        
        return res;
    }
}

相关文章

网友评论

      本文标题:[刷题防痴呆] 0372 - 超级次方 (Super Pow)

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