Hoj 2010 GCD & LCM Inverse
来源:互联网 发布:人工智能图像理解 编辑:程序博客网 时间:2024/06/11 08:45
题目: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;
}
- Hoj 2010 GCD & LCM Inverse
- Hoj 2010 GCD & LCM Inverse
- [数论]HOJ 2010 GCD & LCM Inverse
- GCD & LCM Inverse
- POJ5429 GCD & LCM Inverse
- POJ2429 GCD & LCM Inverse
- GCD & LCM Inverse POJ
- GCD & LCM Inverse POJ
- poj 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- POJ 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- POJ-2429 GCD & LCM Inverse
- Poj2429 inverse of LCM&GCD
- GCD & LCM Inverse(目前超时)
- poj 2429 GCD & LCM Inverse
- POJ 2429 GCD & LCM Inverse 因式分解
- Caused by: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1
- TextView_跑马灯
- 白话经典算法系列之九 从归并排序到数列的逆序数对(微软笔试题)
- Spring依赖注入(dependency injection)
- 马尔科夫与隐马尔可夫总结
- Hoj 2010 GCD & LCM Inverse
- CSS样式初始化分为PC端和移动APP端
- mysql索引类型normal,unique,full text的区别是什么?
- 【ActiveMQ】ActiveMQ 管理页面点消息ID提示Error
- C#WIFI搜索与连接
- HDU
- 马拦过河卒
- 软件
- Docker基础教程(2)简介