372. Super Pow
Super Pow
Solution
public class Solution {
private static int VALUE = 1337;
public int superPow(int a, int[] b) {
long result = 1;
for (int i = 0; i < b.length; i++) {
long first = pow(result, 10) % VALUE;
long second = pow(a, b[i]) % VALUE;
result = (first * second) % VALUE;
}
return (int) result;
}
private long pow(long x, int y) {
if (y == 0) return 1;
if (y == 1) return x % VALUE;
x = x % VALUE;
y = y % VALUE;
long result = pow(x, y / 2) % VALUE;
if (y % 2 == 1) {
return result * result * x;
} else {
return result * result;
}
}
}Last updated