372. Super Pow

很无聊的一个数学题。 需要知道2个公式
计算幂函数的。。

主要问题就是指数太大,a^b中的b太大。。

要分解。

23^1335 = {(23^1330)%mod * (23^5)%mod} % mod

然后一开始的 (23^1330)%mod = ((23 133)%mod)10

原式就是 ((23 133)%mod)10 * (23^5)%mod} % mod

然后 23^133 重新分解一下

edge case比较多

这个题好无聊,我这种榆木脑袋看到这种数学题就变得很消极。。

int mod = 1337;
    public int superPow(int a, int[] b) 
    {
        
        
        
        return helper(a,b,b.length-1);
        
    }
    
    
    public int helper(int a, int[] b, int last)
    {
        if( last == -1) return 1;
        int lastOne = b[last];
        
        return pow(helper(a,b,last-1),10) * pow(a,lastOne)%mod;
    }
    
    public int pow(int a , int b)
    {
        if( b == 0) return 1;
        if (a == 0 ) return 0;
        
        
        int res = 1;
        a %= mod;
        for(int n = 0; n < b;n++)
        {
            res = res * a % mod;
        }
        
        return res;
        
    }
原文地址:https://www.cnblogs.com/reboot329/p/5875881.html