Hoj 2010 GCD & LCM Inverse
2017-08-04 08:59
537 查看
题目:http://acm.hit.edu.cn/hoj/problem/view?id=2010
题意就是已知两个数的最大公约数gcd和最小公倍数lcm求这两个数a, b。
由欧几里德求算法可以知道,a/gcd * b/gcd = lcm/gcd,
a/gcd ,b/gcd 互质;
定义两个互质的数 x,y;
可以推断 a/gcd * b/gcd = x * y;
所以问题变为把lcm/gcd分解为两个互质的数的乘积。这样就可以枚举了,
从lcm/gcd的平方根开始枚举a即可。由于数据比较大,所以用了long long。
以下只是欧几里得的算法过程,并A不了题,
因为for循环超时了,可以自行去网上找模板修改,
大家在这里可以学到这个公式的用法。
#include <stdio.h>
#include <iostream>
#include <math.h>
#define ll long long
using namespace std;
void swap(ll &x,ll &y){ll t=x;x=y;y=t;}
ll gcdd(ll a,ll b){return b==0?a:gcdd(b,a%b);}
int main(){
ll lcm,gcd;
while(~scanf("%lld%lld",&gcd,&lcm)){
ll t=lcm/gcd;ll a,b;
for(a=sqrt(t);a>=1;a--){
if(t%a==0){
b=t/a;
if(gcdd(a,b)==1){
break;
}
}
}
if(a>b) swap(a,b);
printf("%lld %lld\n",a*gcd,b*gcd);
}
return 0;
}
题意就是已知两个数的最大公约数gcd和最小公倍数lcm求这两个数a, b。
由欧几里德求算法可以知道,a/gcd * b/gcd = lcm/gcd,
a/gcd ,b/gcd 互质;
定义两个互质的数 x,y;
可以推断 a/gcd * b/gcd = x * y;
所以问题变为把lcm/gcd分解为两个互质的数的乘积。这样就可以枚举了,
从lcm/gcd的平方根开始枚举a即可。由于数据比较大,所以用了long long。
以下只是欧几里得的算法过程,并A不了题,
因为for循环超时了,可以自行去网上找模板修改,
大家在这里可以学到这个公式的用法。
#include <stdio.h>
#include <iostream>
#include <math.h>
#define ll long long
using namespace std;
void swap(ll &x,ll &y){ll t=x;x=y;y=t;}
ll gcdd(ll a,ll b){return b==0?a:gcdd(b,a%b);}
int main(){
ll lcm,gcd;
while(~scanf("%lld%lld",&gcd,&lcm)){
ll t=lcm/gcd;ll a,b;
for(a=sqrt(t);a>=1;a--){
if(t%a==0){
b=t/a;
if(gcdd(a,b)==1){
break;
}
}
}
if(a>b) swap(a,b);
printf("%lld %lld\n",a*gcd,b*gcd);
}
return 0;
}
相关文章推荐
- [数论]HOJ 2010 GCD & LCM Inverse
- Hoj 2010 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)
- POJ 2429 GCD & LCM Inverse(大整数素因子分解+二进制枚举)
- 2429 GCD & LCM Inverse 大整数分解质因数
- (Relax 数论 1.1)POJ 2429 GCD & LCM Inverse(已知GCD、LCM,反求a、b)
- POJ2429 GCD & LCM Inverse (大整数分解)
- poj2492GCD & LCM Inverse(GCD&&LCM)(此题目前超时)
- <模板>(Miller-Rabin和Pollard_rho算法)poj 2429 GCD & LCM Inverse (数论)
- poj 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- HDU 3071-Gcd & Lcm game-线段树+素因子分解-[解题报告]HOJ
- POJ2429_GCD & LCM Inverse【Miller Rabin素数测试】【Pollar Rho整数分解】
- poj 2429 GCD & LCM Inverse(Miller_rabin 测试+pollard_rho大数分解)
- [POJ 2429] GCD & LCM Inverse
- POJ 2429 GCD&LCM Inverse [pollard_rho]【数论】
- poj 2429 GCD & LCM Inverse
- GCD & LCM Inverse POJ - 2429 Pollard_rho大数因子分解
- poj2429 GCD & LCM Inverse