题目地址
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;
}
}
网友评论