【模板】快速幂+取模

【模板】快速幂+取模

普通快速幂

template<class T>T hpow(T a, T b)
{
    T ans = 1;
    while(b)
      {
        if(b & 1)  ans = ans * a;
        a = a * a;
        b >>= 1;
      }
    return ans;
}

取模快速幂

template<class T>T hpow(T a, T b, T mo)
{
    T ans = 1;
    a %= mo;
    while(b)
      {
        if(b & 1) ans = ans * a % mo;
        a = a * a % mo;
        b >>= 1;
      }
    return ans;
}

!注意:本篇博文仅是贴上个人习惯的快速幂模板,并未做出详细讲解,对您造成的不便敬请谅解~

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Mashiro_ylb/article/details/78479514