http://www.lintcode.com/zh-cn/problem/fast-power/
class Solution {
public:
/*
* @param a, b, n: 32bit integers
* @return: An integer
*/
int *d;
int fastPower(int a, int b, int n) {
// write your code here
return k(a, b, n);
}
long long k(int a, int b, int n) {
// write your code here
if (n == 0)return 1 % b;
if (n == 1)return a%b;
int mid = sqrt(n);
long long c = k(a, b, mid);
long long l = k(a, b, n - mid*mid);
long long result = 1;
while (mid--)
{
result *= c;
result %= b;
}
return result*l%b;
}
};