实现原理:
a^b mod c = ((a^2)^b/2)mod c,b为偶数,
a^b mod c = ((a^2)^b/2 * a)mod c,b为奇数。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;
typedef unsigned long long ll;
ll a,b,MOD;
ll powr(ll a,ll i)//位运算递归版
{
if (i==0) return 1;
int temp=powr(a,i>>1);
temp=temp*temp%MOD;
if (i&1) temp=(ll)temp*a%MOD;
return temp%MOD;
}
ll f(ll a,ll b,ll n)//位运算非递归
{
int t,y;
t=1; y=a;
while (b!=0)
{
if (b&1==1) t=t*y%MOD;
y=y*y%MOD; b=b>>1;
}
return t;
}
int pow1(int a,int b)//常规求幂
{
int r=1;
while(b--) r*=a;
return r;
}
int pow2(int a,int b)//一般快速求幂
{
int r=1,base=a;
while(b!=0)
{
if(b%2) r*=base;
base*=base;
b/=2;
}
return r;
}
int main()
{
scanf("%llu%llu%llu",&a,&b,&MOD);
printf("%d\n",powr(a,b));
printf("Time used = %0.3f\n",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
应用:
费马小定理求逆元的实现。
费马小定理:假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p)。
即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。a^(p-1)%p = 1
逆元:如果a*x≡1(mod p),则称x为a关于mod p意义下的逆元(inv),x即是1/a。
那么我们就可以将x换为a^(p-2),逆元定义式便转化为了费马小定理公式,那么a关于mod p意义下的逆元即为a^(p-2)——快速幂求解。