当前位置:网站首页>Fast power (template)

Fast power (template)

2022-07-04 06:02:00 Zheng Qingda doesn't know his name


Fast power ( Templates )


Time complexity O(logn);
Spatial complexity O(1);

double myPow(double x, int n) {
    
        if(n==0) return 1;
        double sum=1;
        long long m=abs(n);//long long  Type prevents n=-2^31 Take the timing and burst it int;
        while(m) {
    
            if(m&1) {
    
                if(n>0) sum*=x;
                else sum/=x;
            }
            m>>=1;
            x*=x;
        }
        return sum;
    }
原网站

版权声明
本文为[Zheng Qingda doesn't know his name]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141621402723.html