# On the fast power

This essay briefly explain kinds of math problems quickly realize the power of the principles and implementation.

## Fast power use

As the name suggests, is a very fast power fast exponentiation, imagine when you face a problem: find \ (a ^ b \) when you first reaction is to open \ (long long \) followed by \ (for \) little by little demand cycle. Then you will already have the power operation \ (O (b) \) algorithm. Logically speaking, this algorithm has been good enough, but encountered some problems when the time card still \ (T \) , then quickly came into power. Simply put, quickly power is a kind of complexity is \ (O (logb) \) exponentiation algorithm.

## The principle of rapid power

Because the power of fast time complexity is \ (O (logb) \) , so we naturally thought of binary and bit computing. It is obvious, we know that an integer can be split into a number of \ (2 ^ k \) and. So the analogy a bit, for a exponentiation problem \ (A ^ b \) , we can \ (b \) binary decomposed into several \ (2 ^ k \) and then the corresponding down is that these and powers of product.

for example:

Solving Problems: \ (3 ^ {42} \) .

The first step, the 42 binary split:

\ [(42) _ {10} = (101010). 1 _2 = \ ^ 2. 5 + 0 Times \ ^ 2. 4 Times + \ cdots + 0 \ 0 ^ 2 Times \]

then, \ (3 ^ {42} \) becomes:

\ [^ {3 ^ {42}. 1 = 3 × 32 × 16 + 0 + + 0. 8. 1 × × × 2. 4 +. 1. 1 + 0 × } \]

so, we have such a principle:

Solving \ (a ^ b \) when, if \ (B \) is odd, then the original formula is: \ (A \ B-Times. 1 A ^ {} \) .

Similarly, if \ (B \) is even, the original formula can become: \ (A ^ B {\ Div2} \ {Times A ^ B \ div 2} \) .

This is a doubling of the idea, so we put the original \ (O (b) \) algorithm was optimized became \ (O (logb) \) algorithm.

This is the power of rapid implementation principle.

## Fast power of code to achieve

According to the rapid realization of the principle of power we have just learned, we can easily find that this thing can be accomplished with recursion. code show as below:

```
int qpow(int a,int b)
{
if(!b)
return 1;
else if(b&1)
return a*qpow(a,b-1);
else
{
int t=qpow(a,b>>1);
return t*t;
}
}
```

However, we learned recursive small partner should know very huge recursive constant. So the above code is not a senior \ (OIer \) will choose something.

How to write fast that power is not constant guarantee it?

We use an iterative writing:

We will find that no matter \ (b \) what value it fast Iteration process either \ (--1 \) or \ (\ div 2 \) . But regardless of which it uses the above operation, there will be both a time, \ (= B. 1 \) . That is, \ (b \) in the process of iteration, at least there will be a moment \ (b \) is odd.

So we consider that we can in the \ (b \ div 2 \) iterations, the result is not the first iteration affect the answers, but the result of the first iteration of storage down, and then wait until \ (b \) is an odd number when added to a unified answer to go. Thus eliminating the tedious process of recursion and record answers, ensuring constant is small, and maintain the correctness of the answer.

code show as below:

```
int qpow(int a,int b)
{
int ret=1;
while(b>0)
{
if(b&1)
ret*=a;
a*=a;
b>>=1;
}
}
return ret;
}
```