首页 > 其他 > 详细

leetcode 372

时间:2019-05-23 19:22:46      阅读:99      评论:0      收藏:0      [点我收藏+]

技术分享图片

题意:求 a^b mod 1337的值。

两个重要公式:1)(a*b)%k = (a%k) * (b%k)%k

2) a^b % k = (a%k)^b %k

分治法,拆成两个子问题求解。

class Solution {
public:
    int superPow(int a, vector<int>& b) {
        long res = 1;
        for(int i=0; i<b.size();i++){
            res = pow(res,10)*pow(a,b[i])%1337;
        }
        return res;
    }
    int pow(int x, int n){
        if(n==0) return 1;
        if(n==1) return x%1337;
        return pow(x%1337, n/2) * pow(x%1337, n-n/2)%1337;
    }
};

 

leetcode 372

原文:https://www.cnblogs.com/Bella2017/p/10913808.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!