a的b次方怎么求
pow(a, b)是数学头文件math.h里面有的函数
可是它返回值是double类型,数据有精度误差
LL pow(LL a, LL b){//a的b次方
LL ret = 1;
for(LL i = 1; i <= b; i ++){
ret *= a;
}
return ret;
}
如果数据量大而且要mod
LL pow_mod(LL a, LL b){//a的b次方
LL ret = 1;
while(b != 0){
if(b % 2 == 1){
ret = (ret * a) % MOD ;
}
a = (a * a ) % MOD ;
b /= 2;
}
return ret;
}
用位运算稍微再快一点
LL pow_mod(LL a, LL b, LL p){//a的b次方求余p
LL ret = 1;
while(b){
if(b & 1) ret = (ret * a) % p;
a = (a * a) % p;
b >>= 1;
}
return ret;
}