general algorithm for any number other than 1337, no fancy math


  • 0
    Y
    class Solution {
        public int superPow(int a, int[] b) {
            int[] dp=new int[b.length];
            int newa=a%1337;
            int result=1;
            for(int i=b.length-1;i>=0;i--){
                dp[i]=newa;
                result=(result*mypow(dp[i],b[i]))%1337;
                newa=mypow(newa,10);
            }
            return result;
        }
        private int mypow(int a,int b){
            int result=1;
            for(int i=0;i<b;i++){
                result=(result*(a%1337))%1337;
            }
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.