Introduction:
Common exponentiation:
\ (A ^ B \) and consecutive multiplying \ (B \) times \ (A \) .Obviously, efficiency
Very TMlow- First, we need to know,
\ (Y = X ^ X ^ {Y_1 + + ... + y_n Y_2} \)
For example, when the \ (b = 11 \) , the
position of the maximum temporary base unit, they must be spelled All number in the range, it is a binary split is the fastest and optimize multiple backpack is also this idea.
The \ (B \) split into two 229, the \ (b = (1011) _2 = (1 × 2 ^ 3 + 0 × 2 ^ 2 + 1 × 2 ^ 1 + 1 × 2 ^ 0) _ {10 } \)
so \ (a ^ b = a ^
{2 ^ 3 + 2 + 1} \) Code:
#include<cstdio>
inline int quick_pow(int A , int B){
int base = A, ans = 1;
while(B){
if(B & 1){//取B的末尾进行算位权
ans *= base;
}
base *= base;//及base=base^2,因为每次次数要按位加一
B >>= 1;//去掉一位
}
return ans;
}
int main(){
int a , b;
scanf("%d%d",&a , &b);
printf("%d",quick_pow(a , b));
return 0;
}