话不多说直接上代码
int Pow(int a,int b){
int ans = 1;
int base = a;
while(b){
if(b & 1) ans *= base;
base *= base;
b >>= 1;
}
return ans;
}
需要取模:
#define mod 1000000007
int pow_mod(int a,int b){
int ans = 1;
int base = a%mod;
while(b){
if(b & 1) ans = (ans*base)%mod;
base = (base*base)%mod;
b >>= 1;
}
return ans;
}