首页 > 其他分享 >快速幂

快速幂

时间:2022-10-30 22:01:57浏览次数:26  
标签:int 复杂度 ans 快速 times2 mod


title: 快速幂
date: 2022-10-05 17:31:57


概念:

快速幂降低了时间复杂度,将指数转换为二进制计算

\(2^3\) ->\(2^{2^1}\times2^{2^0}\)

时间复杂度

  • O(logN)
  • 如果正常计算是for循环N次

代码实现

int fast_power(int a,int b,int mod){//a^b次
    int ans = 1;
    while(b){
        if(b&1){
            ans = a*ans%mod;
        }
        b>=1;
        a=a*a%mod;
    }
    return ans;
}

标签:int,复杂度,ans,快速,times2,mod
From: https://www.cnblogs.com/tsqo/p/16842370.html

相关文章