Super Pow

https://www.lintcode.com/problem/super-pow/description

public class Solution {
    /**
     * @param a: the given number a
     * @param b: the given array
     * @return: the result
     */
    public int superPow(int a, int[] b) {
        // Write your code here
        if (b.length == 1) {
            if (b[0] == 0) {
                return 1;
            } else if (b[0] == 1) {
                return a % 1337;
            }
        }
        int[] next;
        if (b[0] >= 2) {
            int left = 0;
            next = new int[b.length];
            for (int i = 0; i < b.length; i++) {
                int i1 = b[i] + left * 10;
                next[i] = i1 / 2;
                if ((i1 & 1) == 1) {
                    left = 1;
                } else {
                    left = 0;
                }
            }
        } else {
            next = new int[b.length - 1];
            int left = b[0];
            for (int i = 0; i < next.length; i++) {
                int i1 = b[i + 1] + left * 10;
                next[i] = i1 / 2;
                if ((i1 & 1) == 1) {
                    left = 1;
                } else {
                    left = 0;
                }
            }
        }
        int result = superPow(a, next);
        result %= 1337;
        result *= result;
        result %= 1337;
        if ((b[b.length - 1] & 1) == 1) {
            a %= 1337;
            result *= a;
            result %= 1337;
            return result;
        } else {
            return result;
        }
    }
}

你可能感兴趣的:(Super Pow)