最大公约数:3种算法
1.
int gcd( int a, int b) **//递归**
{
if( b==0 )
return a;
else
return gcd(b, a%b);
}
> int gcd(int a, int b) **//迭代**
> {
> int temp;
> while( b!=0 )
> {
> temp=a;
> a=b;
> b=temp%b;
> }
> return a; }
**
快速幂
**
a^b
int quickmi(int a,int b){
int ans = 1;
int base = a;
while(b){
if(b & 1) ans *= base;
base *= base;
b >>= 1;
}
return ans;
}
例题:快速幂取模 :
(a^b) mod c = (a mod c)^b mod c
int quickmi_mod(int a,int b,int c){
int ans = 1;
int base = a%c;
while(b){
if(b & 1) ans = (ans*base)%c;
base = (base*base)%c;
b >>= 1;
}
return ans;
}