# 何为快速幂

a ^ b =( (a ^2) ^ (b/ 2) ) * a ^ (b % 2) = （a ^ (b/2)）^ 2 * a ^ (b % 2)
= ( a ^ (b/2) * a ^ (b /2) ） * a ^ (b % 2) … 接着不断的分治

# Code 递归 （非快速幂）

//非快速幂递归求 a ^ b
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int fun1(int a, int b)
{
if (b == 1)
return a;
return fun1(a, b - 1) * a;
}
int main()
{
int a, b;
a = 7; b = 9;
cout << "sum1: " << fun1(a, b) << endl;
return 0;
}


# Code 递归 （快速幂）

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int fun2(int a, int b)
{
int t;
if (b == 1)//递归结束
return a;

if (b % 2 == 0)
{
t = fun2 (a, b / 2);
return t * t;
}
else {
t = fun2 (a, b / 2);
return t * t * a;
}
}
int main()
{
int a, b;
a = 7; b = 9;
cout << "sum2: " << fun2(a, b) << endl;
return 0;
}


# Code 迭代 （非快速幂）

//非快速幂迭代
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int a, b;
a = 7; b = 9;
int sum1 = 1;
for (int i = 0; i < b; i++)
sum1 = a * sum1;
cout << "sum1 : " << sum1 << endl;
return 0;
}


# Code 迭代 （非快速幂)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int a, b;
a = 7; b = 9;
int res = 1;
while (b > 0)
{
if (b % 2 == 1) res = res * a;

a = a * a;

b = b / 2;
}
cout << "sum2:" << res << endl;
return 0;
}


10-22 59
07-03 335

09-13 64
07-25 239
02-16 405
09-01 256
03-01 995
07-22 44
08-01 213
01-09 209