最小公因数,最小公倍数,以及快速幂模板

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<math.h>
#include<set>
#include"iomanip"
typedef long long ll;
using namespace std;

ll fastpower(ll a, ll b)//快速幂模板
{
int ans = 1;
int res = a;
while (b)
{
if (b & 1)
{
ans = ans * res;
}
b >>= 1;
res = res * res;
}
return ans;
}
ll gcd(ll a, ll b)//最大公因数,辗转相除法(递归形式)
{
return b ? gcd(b, a % b ): a;
}
ll lcm(ll a, ll b)//最小公倍数
{
return a * b / gcd(a, b);
}
int main()
{
ll a, b;
while (cin >> a >> b)
{
cout << fastpower(a, b)<<endl;
cout << gcd(a, b) << endl;
cout << lcm(a, b) << endl;
}
return 0;
}

原文地址:https://www.cnblogs.com/Uiney117/p/14049795.html