快速计算a^b(x^n),输入在int范围内 注意:在计算矩阵快速幂时采用递归写法容易爆栈
递归写法
1 typedef
long long ll;
2 ll fast(ll x, ll n)
3 {
4 if(n ==
0)
return 1;
5 if(n&
1)
return x*fast(x,n-
1);
6 ll tmp = fast(x,n/
2);
7 return tmp*
tmp;
8 }
循环写法
typedef
long long ll;
ll fast(ll x, ll n)
{
ll res =
1;
while(n >
0)
{
if(n&
1) res*=
x;
x*=
x;
n>>=
1;
}
return res;
}
转载于:https://www.cnblogs.com/Pos-Proteus/p/5268980.html
相关资源:矩阵快速幂