// divide and conquer
#include<stdio.h>
long long times( int x,int n ){
if (n==2)
return x*x;
else if (n==1)
return x;
if (n%2 == 0){
long a = times(x,n/2);
return a*a ;
}
else {
long a = times( x,(n-1)/2 );
return a*a*x;
}
}
int main (){
printf ("%ld",times(3,5
));
return 0;
}
分治法 时间为logn的求x^n的算法
最新推荐文章于 2022-04-08 18:43:51 发布