Pow 算法

#include <iostream>
using namespace std;

template<class T, class Int>
T Pow(T x, Int n)
{
    T r(1);    // 应是含幺半群的幺元
    while (n != 0)
    {
        if (n & 0x1 == 1)
        {
            r *= x;
        }
        n >>= 1;
        x *= x;
    }

    return r;
}

template <class T, class Int>
T PowBoost(T x, Int n)
{
    T r(n & 0x1 ? x : 1);
    while ((n >>= 1) != 0)
    {
        x *= x;
        if (n & 0x1 == 1)
        {
            r *= x;
        }
    }
    return r;
}

int main(int argc, char **argv)
{
    return 0;
}
原文地址:https://www.cnblogs.com/jjtx/p/3605968.html