power(乘幂)函数剖析
生活随笔
收集整理的這篇文章主要介紹了
power(乘幂)函数剖析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
近來學習STL,看到power函數的實現感覺挺有趣,記錄一下。
1. 一般情況下,我自己要實現乘冪函數會這樣實現:
int power(int x,size_t n) {int result = 1;while (n--)result *= x;return result; }這樣即使實現,這里的時間復雜度和n有關,時間復雜度為0(n)。
2. 看了stl源碼實現是這樣:
// Returns __x ** __n, where __n >= 0. _Note that "multiplication" // is required to be associative, but not necessarily commutative. //意思是multiplication要滿足結合律,但不需要滿足交換律 template <class _Tp, class _Integer, class _MonoidOperation> _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr) //這里第三個參數是一個二元操作函數 {if (__n == 0)return identity_element(__opr); //返回1else {while ((__n & 1) == 0) //如果n為偶數 {__n >>= 1;__x = __opr(__x, __x);}_Tp __result = __x;__n >>= 1;while (__n != 0) {__x = __opr(__x, __x);if ((__n & 1) != 0) //如果n為奇數__result = __opr(__result, __x);__n >>= 1;}return __result;} }template <class _Tp, class _Integer> inline _Tp __power(_Tp __x, _Integer __n) {return __power(__x, __n, multiplies<_Tp>()); }// Alias for the internal name __power. Note that power is an extension, // not part of the C++ standard. template <class _Tp, class _Integer, class _MonoidOperation> inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr) //也可以自定義,傳進去一個函數 {return __power(__x, __n, __opr); }template <class _Tp, class _Integer> inline _Tp power(_Tp __x, _Integer __n) //默認情況下是乘冪 {return __power(__x, __n); }這里:當n為偶數時,X^n=(X^2)^(2/n),此時看2/n是否還是偶數,如果是則繼續,否則,轉向n是奇數的情況;
? ? ? ? ? ?當n為奇數時,X^n=X*X^(n-1);
例如計算5^6=(5^2)^3,也就是計算25^3。
這種情況和第三種情況類似,只是當n開始為偶數時,比第三種方法的效率更高,n為奇數時,和第三種方法時間復雜度一樣。
3. 第三種實現,和stl源碼差不多,只是表示更簡潔,和第二種情況相比效率會差點:
int power(int x, size_t n) {if(n == 0)return 1;int result = 1;while(n){if(n & 1) //n為奇數時result *= x; n >>= 1;x *= x; }return result; }
如計算5^6是6化為二進制為0110,所以這里5^6=5^2*5^4,這里時間復雜度為0((log2n)+1)
轉載于:https://www.cnblogs.com/liuamin/p/7107202.html
總結
以上是生活随笔為你收集整理的power(乘幂)函数剖析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是AOP?
- 下一篇: buildroot的使用简介【转】