快速幂模板
第一种(最简单)
//快速幂模板 (a的b次方)%c结果 此处使用long类型是防止整型溢出
long pow(int a,int b,int c) {
long res=1;
long x=a%c;
while(b!=0) {
if((b&1)==1) res=(res*x)%c;
a=(x*x)%c;
}
return res;
}
第二种(速度稍微快点)
//快速幂模板 (a的b次方)%c结果
long pow(int a,int b,int c) {
long x=(long) a;
if(b==0) return 1;
else {
while((b&1)!=0) {
x=(x*x)%c;
b=b>>1;
}
}
long res=a;
b=b>>1;
while(b!=0) {
x=(x*x)%c;
if((b&1)==0) res=(res*x)%c;
b=b>>1;
}
return res;
}