快速幂下的快速乘法

思路类似快速米,

为了防止数相乘超出long long的范围。

复杂度再加一个log(n);

很实用的东西:


ll mut(ll a,ll b)
{
   ll res=0;
   while (b)
   {
     if (b&1)  res=(res+a)%p;
     a=(a+a)%p;
     b>>=1;
   }
   return res;
}

ll mul(ll x)
{
ll c=1,tmp=2;
   while(x)
   {
      if (x&1) c=mut(c,tmp );
        x/=2;
        tmp=mut(tmp,tmp);
     }
    return c;
}


原文地址:https://www.cnblogs.com/forgot93/p/4340732.html