[leetcode] 372. Super Pow

题目链接: https://leetcode-cn.com/problems/super-pow/

 

需用到的数学知识:

a^b % 1337 = (a%1337)^b % 1337

xy % 1337 = ((x%1337) * (y%1337)) % 1337

例如:

X^678 = ((X^670 % 1337) * (X^8 % 1337)) % 1337 = (superPow((X^67 % 1337), 10) * (X^8 % 1337)) % 1337

 

java代码实现:

public class Solution_372 {

    public int superPow(int a, int k) {
        if (k == 0) return 1;
        int ans = 1;
        for (int i = 1; i <= k; i++)
            ans = (ans * a) % 1337;
        return ans;
    }

    public int superPow(int a, int[] b) {
        if (b.length == 0)
            return 1;
        a = a % 1337;
        int lastBit = b[b.length - 1];
        int[] newB = new int[b.length - 1];
        for (int i = 0; i < b.length - 1; i++) {
            newB[i] = b[i];
        }
        return (superPow(superPow(a, newB), 10) * superPow(a, lastBit)) % 1337;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值