快速幂 快速幂取模

快速幂的思想在于快速求解高幂指数的幂运算 复杂度为O(log2n) 与朴素运算相比有很大的改进

接下来给出代码 其中有详解

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;

LL pow1(int a,int b)///最常规的方法
{
    LL ans=1;
    while(b--)
    ans*=a;
    return ans;
}
/**
   将幂指数转化为2进制
   b=bn*2^n+b(n-1)*2^(n-1)+…+b1*2+b0
   a^b=a^(bn*2^n+b(n-1)*2^(n-1)+…+b1*2+b0) 
   a^b=a^(bn*2^n)*a^(b(n-1)*2^(n-1))*…*a^(b1*2)*a
   即若b转化成2进制后 若当前位为0则该项为1
**/
LL pow2(int a,int b)///二分快速幂
{
    LL ans=1,base=a;
    while(b!=0)
    {
        if(b%2)       
            ans*=base;
        base*=base;
        b/=2;
    }
    return ans;
}
LL pow3(int a,int b)///同pow2(位运算)
{
    LL ans=1,base=a;
    while(b!=0)
    {
        if(b&1)///相当于 b%2==1
            ans*=base;
        base*=base;
        b>>=1;///相当于 b/=2
    }
    return ans;
}
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        cout<<pow1(a,b)<<endl;
        cout<<pow2(a,b)<<endl;
        cout<<pow3(a,b)<<endl;
    }
    return 0;
}

有了快速幂的基础求解(a^b)%c 就变得很简单了

/**
   同样是二分的思想 位运算
   a*b%c=(a%c)*(b%c)%c
**/
int  quickmod(int a,int b,int c)
{
    LL ans=1,base=a;
    while(b!=0)
    {
        if(b&1)
            ans=(ans*base)%c; /// 该处的base=a^(2^i)%c 乘到ans上
        b>>=1;              ///计算下一位
        base=base*base%c;///防止超 对c取余
    }
    return ans;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值