In this Leetcode Super Pow problem solution, 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.
Problem solution in Python.
class Solution: def superPow(self, a: int, b: List[int]) -> int: return pow(a,(int("".join([str(i) for i in b]))),1337)
Problem solution in Java.
public class Solution { public int superPow(int a, int[] b) { if(b==null || b.length==0) return 0; int len = b.length; int[] remainders = new int[len]; int first = a % 1337; for(int i=len-1; i>=0; i--) { int[] nums = new int[11]; nums[0] = 1; for(int j=1; j<11; j++) { nums[j] = (nums[j-1]*first) % 1337; } remainders[i] = nums[b[i]]; first = nums[10]; } int remainder = 1; for(int i=0; i<len; i++) { remainder = (remainder*remainders[i]) % 1337; } return remainder; } }
Problem solution in C++.
int superPow(int a, vector<int>& b) { int result = 1; a %= 1337; for(int i = 0;i<b.size();i++){ for(int j = 0;j<b[b.size()-i-1];j++){ result=(result*a)%1337; } int a2 = (a*a)%1337; int a4 = (a2*a2)%1337; int a8 = (a4*a4)%1337; a = (a8*a2)%1337; } return result; }
Problem solution in C.
int func(int a,int b){ int ret=1; for(int i=0;i<b;i++){ ret=ret*a%1337; } return ret; } int myPow(int a,int b,int zeroNum){ int ret=func(a,b); for(int i=0;i<zeroNum;i++){ ret=func(ret,10); } return ret; } int superPow(int a, int* b, int bSize){ int ret=1; a%=1337; for(int i=0;i<bSize;i++){ ret=ret*myPow(a,b[i],bSize-i-1)%1337; } return ret; }
0 Comments