Power, Binary Exponentiation and Matrix Exponentiation

Binary Exponentiation

50. Pow(x, n)

class Solution {
public:
double helper(double x, long n)
{
if(n==0)return 1.0;
if(n<0)return 1/helper(x, -n);
else
{
auto ret = helper(x, n/2);
ret *= ret;
if(n%2)ret *= x;
return ret;
}
return 0.0;
}
double myPow(double x, int n)
{
return helper(x, long(n));
}
};

Matrix Exponentiation

Reference