#!/usr/bin/env python3

__author__ = 'Wei Mu'

class Solution:
    def myPow(self, x: 'float', n: 'int') -> 'float':
        if n < 0:
            x, n = 1/x, -n
        
        if n == 1 or x == 1:
            return x
        
        if n == 0:
            return 1.0
        
        ans = x
        factor = 1.0
        
        while n != 0:
            if n == 1:
                break
            if n % 2 == 1:
                factor *= ans
                n -= 1
                continue
            else:
                ans = ans * ans
                n //= 2
                
    return ans * factor
