快速幂取模

时间:2021-04-08 13:55:01   收藏:0   阅读:0
   long long myPow(long long x, int n) {
        long long ans = 1;
        while(n){
            if(n % 2 != 0){
                ans *= x;
                ans %= modN;
            }
            x *= x;
            x %= modN;
            n /= 2;
        }
        return ans;
    }

如上求x的n次方,并对结果求余modN

比如:3的11次方等于3 * (3^2)^5 等于 3 * (3^2) * ((3^2)^2)^2 如此递推 

 

关于题目:https://leetcode-cn.com/problems/maximize-number-of-nice-divisors/

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!