GCD & LCM Inverse
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16206   Accepted: 3008

Description

Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b.

Input

The input contains multiple test cases, each of which contains two positive integers, the GCD and the LCM. You can assume that these two numbers are both less than 2^63.

Output

For each test case, output a and b in ascending order. If there are multiple solutions, output the pair with smallest a + b.

Sample Input

3 60

Sample Output

12 15

Source

题意:
给出gcd和lcm求和最小的两个数a,b
代码:
//a*b=gcd*lcm,x=a/gcd,y=b/gcd,s=lcm/gcd => x*y=s;
//想要a+b小 => x*gcd+y*gcd小 => x+y小,x和y越接近sqrt(S)时x+y越小(对勾函数)
//所以求出s的素因子然后搞一搞就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
typedef long long ll;
const int S=;//随机算法判定次数,S越大判错概率越小
//计算(a*b)%c;a,b,c<2^63
ll mult_mod(ll a,ll b,ll c){
a%=c;
b%=c;
ll ret=;
while(b){
if(b&){
ret+=a;
ret%=c;
}
a<<=;
if(a>=c) a%=c;
b>>=;
}
return ret;
}
//计算(x^n)%c
ll pow_mod(ll x,ll n,ll c){
if(n==) return x%c;
x%=c;
ll tmp=x;
ll ret=;
while(n){
if(n&) ret=mult_mod(ret,tmp,c);
tmp=mult_mod(tmp,tmp,c);
n>>=;
}
return ret;
}
//以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(ll a,ll n,ll x,ll t){
ll ret=pow_mod(a,x,n);
ll last=ret;
for(int i=;i<=t;i++){
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;
last=ret;
}
if(ret!=) return true;
return false;
}
// Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
bool Miller_Rabin(ll n){
if(n<) return false;
if(n==) return true;
if((n&)==) return false;
ll x=n-;
ll t=;
while((x&)==){
x>>=;
t++;
}
for(int i=;i<S;i++){
ll a=rand()%(n-)+;
if(check(a,n,x,t)) return false;
}
return true;
}
ll gcd(ll a,ll b){
if(a==) return ;
if(a<) return gcd(-a,b);
while(b){
ll t=a%b;
a=b;
b=t;
}
return a;
}
//Pollard_rho质因数分解算法
ll factor[];//质因数分解结果(无序的)
int tot;//质因数的个数,数组下标从0开始
ll Pollard_rho(ll x,ll c){
ll i=,k=;
ll x0=rand()%x;
ll y=x0;
while(){
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
ll d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k){
y=x0;
k+=k;
}
}
}
//对n进行素因子分解
void findfac(ll n){
if(Miller_Rabin(n)){//素数
factor[tot++]=n;
return;
}
ll p=n;
while(p>=n)
p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
}
ll ans,fa[];
int nu;
void dfs(int st,ll x,ll maxx)
{
if(st>nu){
if(x<=maxx&&x>ans)
ans=x;
return;
}
dfs(st+,x,maxx);
dfs(st+,x*fa[st],maxx);
}
int main()
{
//srand(time(NULL));
ll a,b;
while(scanf("%lld%lld",&a,&b)==){
if(a==b){
printf("%lld %lld\n",a,b);
continue;
}
tot=nu=;
ll s=b/a;
findfac(s);
sort(factor,factor+tot);
fa[]=factor[];
for(int i=;i<tot;i++){//合并相同的素因子
if(factor[i]==factor[i-]) fa[nu]*=factor[i];
else
fa[++nu]=factor[i];
}
ll x=(ll)sqrt(s*1.0);
ans=;
dfs(,,x);
printf("%lld %lld\n",ans*a,s/ans*a);
}
return ;
}

POJ 2429 long long 质因数分解的更多相关文章

  1. poj 3421 X-factor Chains——质因数分解

    题目:http://poj.org/problem?id=3421 记忆化搜索竟然水过去了.仔细一想时间可能有点不对,但还是水过去了. #include<iostream> #includ ...

  2. POJ 2167 Irrelevant Elements 质因数分解

    Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2231   Accepted: 55 ...

  3. POJ 1845 Sumdiv#质因数分解+二分

    题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...

  4. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  5. poj 2429 Pollard_rho大数分解

    先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...

  6. Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)

    一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...

  7. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  8. 求n!质因数分解之后素数a的个数

    n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...

  9. AC日记——质因数分解 1.5 43

    43:质因数分解 总时间限制:  1000ms 内存限制:  65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...

随机推荐

  1. 手机端网页返回顶部js代码

    <!DOCTYPE html>  <html>  <head>  <meta http-equiv="Content-Type" cont ...

  2. 网页性能管理详解:浅谈chrome-Timeline及window.requestAnimationFrame()方法

    你遇到过性能很差的网页吗? 这种网页响应非常缓慢,占用大量的CPU和内存,浏览起来常常有卡顿,页面的动画效果也不流畅. 你会有什么反应?我猜想,大多数用户会关闭这个页面,改为访问其他网站.作为一个开发 ...

  3. 【Linux 运维】查看网络连接状态信息之netstat和ss命令详解

    一.netstat 常用命令详解 通过man netstat可以查看netstat的帮助信息: netstat 命令:用于显示各种网络相关信息,如网络连接,路由表,接口状态,无效连接,组播成员 等等. ...

  4. 聊聊、dubbo 找不到 dubbo.xsd 报错

    平常在用 Dubbo 的时候,创建 xml 会提示 http://code.alibabatech.com/schema/dubbo/dubbo.xsd 找不到. 大家可以去 https://gith ...

  5. day-14 回归中的相关系数和决定系数概念及Python实现

    衡量一个回归模型常用的两个参数:皮尔逊相关系数和R平方 一.皮尔逊相关系数 在统计学中,皮尔逊相关系数( Pearson correlation coefficient),又称皮尔逊积矩相关系数(Pe ...

  6. Centos下的SVN搭建

    需求: 搭建SVN实现本地开发环境,方便线上代码的更新. 步骤: 1. 安装SVN服务 yum install -y subversion 2.创建SVN代码库的目录.创建版本库 mkdir -p / ...

  7. Java 抽象类和Final关键字

    抽象类 用abstract关键字来修饰一个类时,这个类叫抽象类: 用abstract关键字来修饰一个方法时,该方法叫做抽象方法. 含有抽象方法的类必须被定义而为抽象类,抽象类必须被继承,抽象方法必须被 ...

  8. 福大软工1816 · 第五次作业 - 结对作业2_map与unordered map的比较测试

    测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...

  9. 《剑指offer》---两个栈实现队列

    本文算法使用python3实现 1.题目描述:   用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型.   时间限制:1s:空间限制:32768K 2.思路描述:   ...

  10. PHP中Session和Cookie的探究

    一.Session (1)Session的由来以及介绍 Session:在计算机中,尤其是在网络应用中,称为“会话控制”,生存时间为用户在浏览某个网站时,从进入网站到关闭这个网站所经过的这段时间,也就 ...