GCD & LCM Inverse
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 9756Accepted: 1819

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

题目大意:给你两个数a和b的最大公约数和最小公倍数。求a和b

(当中在满足条件的情况下。使a+b尽量小)

思路:最大公约数和最小公倍数的规模为2^63,暴力果断不行。

已知a*b = L(最小公倍数)*G(最大公约数);

设p = L/a,q = L/b,s = L/G;

即p、q为a和b除去最大公约数的部分,且两者互质;

GCD(p,q) = 1,LCM(p。q) = p * q = L*L/(a*b) = L*L/(L*G) = L/G = s。

LCM(p,q) = s;

由上可得我们可由s求出a和b。此题就是让我们把s分解成两个互质数相乘的形式。

Pollar Rho整数分解Miller
Rabin素数測试
结合起来,将s的全部质因子分解出来。

由于GCD(p,q) = 1,全部同样的质数不能同一时候分到p和q中,应将同样的质数分开放。

这里我们把全部同样的质数当做一个总体。

将这些数枚举相乘,找到最接近s的平方根且不

大于s的平方根的组合即为p。则q = s/p

终于a = L/p。b = L/q

比如 G L 为 2 120

s = 60 = 2 * 2 * 3 * 5 = 4 * 3 * 5。枚举进行组合。找到最接近根号60并不超过根号60的

值为5。即p = 5,则q = 60/5 = 12。终于a = 24。b = 10。

參考博文:http://blog.sina.com.cn/s/blog_69c3f0410100uac0.html

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define MAX_VAL (pow(2.0,60))
//miller_rabbin素性測试
//__int64 mod_mul(__int64 x,__int64 y,__int64 mo)
//{
// __int64 t;
// x %= mo;
// for(t = 0; y; x = (x<<1)%mo,y>>=1)
// if(y & 1)
// t = (t+x) %mo;
//
// return t;
//} __int64 mod_mul(__int64 x,__int64 y,__int64 mo)
{
__int64 t,T,a,b,c,d,e,f,g,h,v,ans;
T = (__int64)(sqrt(double(mo)+0.5)); t = T*T - mo;
a = x / T;
b = x % T;
c = y / T;
d = y % T;
e = a*c / T;
f = a*c % T;
v = ((a*d+b*c)%mo + e*t) % mo;
g = v / T;
h = v % T;
ans = (((f+g)*t%mo + b*d)% mo + h*T)%mo;
while(ans < 0)
ans += mo;
return ans;
} __int64 mod_exp(__int64 num,__int64 t,__int64 mo)
{
__int64 ret = 1, temp = num % mo;
for(; t; t >>=1,temp=mod_mul(temp,temp,mo))
if(t & 1)
ret = mod_mul(ret,temp,mo); return ret;
} bool miller_rabbin(__int64 n)
{
if(n == 2)
return true;
if(n < 2 || !(n&1))
return false;
int t = 0;
__int64 a,x,y,u = n-1;
while((u & 1) == 0)
{
t++;
u >>= 1;
}
for(int i = 0; i < 50; i++)
{
a = rand() % (n-1)+1;
x = mod_exp(a,u,n);
for(int j = 0; j < t; j++)
{
y = mod_mul(x,x,n);
if(y == 1 && x != 1 && x != n-1)
return false;
x = y;
}
if(x != 1)
return false;
}
return true;
}
//PollarRho大整数因子分解
__int64 minFactor;
__int64 gcd(__int64 a,__int64 b)
{
if(b == 0)
return a;
return gcd(b, a % b);
} __int64 PollarRho(__int64 n, int c)
{
int i = 1;
srand(time(NULL));
__int64 x = rand() % n;
__int64 y = x;
int k = 2;
while(true)
{
i++;
x = (mod_exp(x,2,n) + c) % n;
__int64 d = gcd(y-x,n);
if(1 < d && d < n)
return d;
if(y == x)
return n;
if(i == k)
{
y = x;
k *= 2;
}
}
}
__int64 ans[1100],cnt;
void getSmallest(__int64 n, int c)
{
if(n == 1)
return;
if(miller_rabbin(n))
{
ans[cnt++] = n;
return;
}
__int64 val = n;
while(val == n)
val = PollarRho(n,c--);
getSmallest(val,c);
getSmallest(n/val,c);
}
__int64 a,b,sq;
void choose(__int64 s,__int64 val)
{
if(s >= cnt)
{
if(val > a && val <= sq)
a = val;
return;
}
choose(s+1,val);
choose(s+1,val*ans[s]);
} int main()
{
int T;
__int64 G,L;
while(~scanf("%I64d%I64d",&G,&L))
{
if(L == G)
{
printf("%I64d %I64d\n",G,L);
continue;
}
L /= G;
cnt = 0;
getSmallest(L,200);
sort(ans, ans+cnt);
int j = 0;
for(int i = 1; i < cnt; i++)
{
while(ans[i-1] == ans[i] && i < cnt)
ans[j] *= ans[i++];
if ( i < cnt )
ans[++j] = ans[i];
} cnt = j+1;
a = 1;
sq = (__int64)sqrt(L+0.0);
choose(0,1);
printf("%I64d %I64d\n",a*G,L/a*G);
}
return 0;
}

POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】的更多相关文章

  1. POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  2. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  3. HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

  5. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

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

  7. Miller Rabin素数检测

    #include<iostream> #include<cstdio> #include<queue> #include<cstring> #inclu ...

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

  9. 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)

    关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...

随机推荐

  1. Oracle 专用模式(DEDICATED) 和 共享模式(SHARE)

    Oracle 是一门博大精深的技术.玩了2年的oracle,依旧还有很多知识点不清楚. 昨天群里的朋友提到了 DEDICATED 和 SHARE 两种模式. 不清楚,默默的做点功课了.从网上搜了点知识 ...

  2. Linux 内核网络协议栈 ------sk_buff 结构体 以及 完全解释 (2.6.16)

    转自:http://blog.csdn.net/shanshanpt/article/details/21024465 在2.6.24之后这个结构体有了较大的变化,此处先说一说2.6.16版本的sk_ ...

  3. 都市侠盗第一季/全集Leverage迅雷下载

    第一季 Leverage Season 1 (2008)看点:Nate Ford(蒂莫西·赫顿 Timothy Hutton 饰)曾当过保险调查员,为自己的老板挽回过数百万美元的损失,是个忠心耿耿的雇 ...

  4. 详解Java多线程编程中LockSupport类的线程阻塞用法

    LockSupport类是Java6(JSR166-JUC)引入的一个类,提供了基本的线程同步原语.LockSupport实际上是调用了Unsafe类里的函数,归结到Unsafe里,只有两个函数: p ...

  5. python 直方图hist

    import sys sys.path.append('/usr/local/lib/python2.7/site-packages') sys.path.append('/usr/lib/pytho ...

  6. ViewHolder的标准写法

    最标准的写法,就是为每一个AdapterView的子View新建一个对应的ViewHolder,同时声明为prtivate final static.ViewHolder类中定义各种成员变量. pub ...

  7. Keras教程

    In this step-by-step Keras tutorial, you’ll learn how to build a convolutional neural network in Pyt ...

  8. Martin Fowler谈微服务的优缺点

    很多开发团队已经认识到微服务架构比单体架构更优越.但是也有其他团队感觉到这是一种消弱生产力的负担.就像任何软件架构,微服务架构同样有利弊.为了能做出一个明智的选择,你必须了解这些应用并将它们运用到你特 ...

  9. CMS (内容管理系统)

    ylbtech-Miscellaneos:CMS (内容管理系统) CMS是"Content Management System"的缩写,意为"内容管理系统". ...

  10. js中document.write的那点事

    document.write()方法可以用在两个方面:页面载入过程中用实时脚本创建页面内容,以及用延时脚本创建本窗口或新窗口的内容.该方法需要一个字符串参数,它是写到窗口或框架中的HTML内容.这些字 ...