GCD & LCM Inverse
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10621   Accepted: 1939

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

POJ Achilles
 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define S 8 ll mult(ll a,ll b,ll mod)
{
a%=mod,b%=mod;
ll ret=;
while(b)
{
if(b&)
{
ret+=a;
if(ret>=mod) ret-=mod;
}
a<<=;
if(a>=mod) a-=mod;
b>>=;
}
return ret;
}
ll pow(ll a,ll n,ll mod)
{
a=a%mod;
ll ret=;
while(n)
{
if(n&) ret=mult(ret,a,mod);
a=mult(a,a,mod);
n>>=;
}
return ret;
}
bool check(ll a,ll n,ll x,ll t)
{
ll ret=pow(a,x,n),last=ret;
for(int i=;i<=t;i++)
{
ret=mult(ret,ret,n);
if(ret== && last!= && last!=n-) return ;
last=ret;
}
if(ret!=) return ;
return ;
}
bool Miller_Rabin(ll n)
{
if(n<) return ;
if(n==) return ;
if((n&)==) return ;
ll x=n-,t=;
while((x&)==) { x>>=;t++;}
srand(time(NULL));
for(int i=;i<S;i++)
{
ll a=rand()%(n-)+;
if(check(a,n,x,t)) return ;
}
return ;
}
int tot;
ll factor[];
ll gcd(ll a,ll b)
{
ll t;
while(b)
{
t=a;
a=b;
b=t%b;
}
if(a>=) return a;
return -a;
}
ll pollard_rho(ll x,ll c)
{
ll i=,k=;
srand(time(NULL));
ll x0=rand()%(x-)+;
ll y=x0;
while()
{
i++;
x0=(mult(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;
}
}
void FindFac(ll n,int k=)
{
if(n==) return;
if(Miller_Rabin(n))
{
factor[tot++]=n;
return;
}
ll p=n;
int c=k;
while(p>=n) p=pollard_rho(p,c--);
FindFac(p,k);
FindFac(n/p,k);
}
ll ansx,ansy,ans;
void dfs(int k,ll x,ll y)
{
if(k>=tot)
{
if(x+y<ans)
{
ans=x+y;
ansx=x;
ansy=y;
}
return;
}
dfs(k+,x*factor[k],y);
dfs(k+,x,y*factor[k]);
}
int main()
{
int i,j;
ll n,m;
while(scanf("%lld%lld",&m,&n)!=EOF)
{
tot=;
ans=INF; //注意初始化
FindFac(n/m,);
sort(factor,factor+tot);
for(i=j=;i<tot;i++)
{
ll tmp=factor[i];
while(i+<tot && factor[i]==factor[i+]) //注意边界
{
tmp*=factor[i];
i++;
}
factor[j++]=tmp;
}
tot=j;
dfs(,,);
if(ansx>ansy) swap(ansx,ansy);
printf("%lld %lld\n",ansx*m,ansy*m);
}
return ;
}

[POJ 2429] GCD & LCM Inverse的更多相关文章

  1. POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)

    [题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...

  2. 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 ...

  3. POJ 2429 GCD & LCM Inverse(Miller-Rabbin素性测试,Pollard rho质因子分解)

    x = lcm/gcd,假设答案为a,b,那么a*b = x且gcd(a,b) = 1,因为均值不等式所以当a越接近sqrt(x),a+b越小. x的范围是int64的,所以要用Pollard_rho ...

  4. POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)

    原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...

  5. poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted:  ...

  6. Mathematics:GCD & LCM Inverse(POJ 2429)

    根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...

  7. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  8. 【poj 2429】GCD & LCM Inverse (Miller-Rabin素数测试和Pollard_Rho_因数分解)

    本题涉及的算法个人无法完全理解,在此提供两个比较好的参考. 原理 (后来又看了一下,其实这篇文章问题还是有的……有时间再搜集一下资料) 代码实现 #include <algorithm> ...

  9. poj2429 GCD & LCM Inverse

    用miller_rabin 和 pollard_rho对大数因式分解,再用dfs寻找答案即可. http://poj.org/problem?id=2429 #include <cstdio&g ...

随机推荐

  1. Spark Streaming揭秘 Day3-运行基石(JobScheduler)大揭秘

    Spark Streaming揭秘 Day3 运行基石(JobScheduler)大揭秘 引子 作为一个非常强大框架,Spark Streaming兼具了流处理和批处理的特点.还记得第一天的谜团么,众 ...

  2. NEV_SDK开发环境部署手册

    根据项目开发需求,要在MEC服务器上部署如下内容:Nginx.Nginx push stream module.Jason CPP.Spawn-fcgi.libfcgi.Redis.Hiredis.B ...

  3. fedora gnome extension

    如果想在gnome-shell桌面放点个性化的应用,可以在https://extensions.gnome.org网站上安装扩展(记得使用firefox). 下面我记录几个我们觉得还不错的扩展: 1. ...

  4. Android Studio 单刷《第一行代码》系列 05 —— Fragment 基础

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  5. C#线程同步总结

    对于整数数据类型的简单操作,可以用Interlocked类的成员来实现线程同步.对于复杂的线程同步,有以下几个方法: 1.lock关键字: 2.Monitor: 3.同步事件和等待句柄: 4.Mute ...

  6. 1025: [SCOI2009]游戏 - BZOJ

    Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对 ...

  7. 解决 Eclipse build workspace 慢,validation javascript 更慢的问题

    鸣谢:http://zuoming.iteye.com/blog/1430925 ------------------------------------------------ 如果用到js插件或者 ...

  8. [转载]VS2012创建MVC3项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。

    如果在没有安装vs2012 update3升级包的情况下,创建MVC3项目会出现下面的错误信息. 因为VS2012已经全面切换到使用NuGet这个第三方开源工具来管理项目包和引用模块了,使用VS201 ...

  9. leetcode4 Valid Palindrome回文数

    Valid Palindrome回文数 whowhoha@outlook.com Question: Given a string, determine if it is a palindrome, ...

  10. uva 10827

    与108类似 多加了两层循环 水过 #include <iostream> #include <cstring> #include <cstdio> #includ ...