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

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #include<math.h>
  5. #include<algorithm>
  6. using namespace std;
  7. #define MAX_VAL (pow(2.0,60))
  8. //miller_rabbin素性測试
  9. //__int64 mod_mul(__int64 x,__int64 y,__int64 mo)
  10. //{
  11. // __int64 t;
  12. // x %= mo;
  13. // for(t = 0; y; x = (x<<1)%mo,y>>=1)
  14. // if(y & 1)
  15. // t = (t+x) %mo;
  16. //
  17. // return t;
  18. //}
  19.  
  20. __int64 mod_mul(__int64 x,__int64 y,__int64 mo)
  21. {
  22. __int64 t,T,a,b,c,d,e,f,g,h,v,ans;
  23. T = (__int64)(sqrt(double(mo)+0.5));
  24.  
  25. t = T*T - mo;
  26. a = x / T;
  27. b = x % T;
  28. c = y / T;
  29. d = y % T;
  30. e = a*c / T;
  31. f = a*c % T;
  32. v = ((a*d+b*c)%mo + e*t) % mo;
  33. g = v / T;
  34. h = v % T;
  35. ans = (((f+g)*t%mo + b*d)% mo + h*T)%mo;
  36. while(ans < 0)
  37. ans += mo;
  38. return ans;
  39. }
  40.  
  41. __int64 mod_exp(__int64 num,__int64 t,__int64 mo)
  42. {
  43. __int64 ret = 1, temp = num % mo;
  44. for(; t; t >>=1,temp=mod_mul(temp,temp,mo))
  45. if(t & 1)
  46. ret = mod_mul(ret,temp,mo);
  47.  
  48. return ret;
  49. }
  50.  
  51. bool miller_rabbin(__int64 n)
  52. {
  53. if(n == 2)
  54. return true;
  55. if(n < 2 || !(n&1))
  56. return false;
  57. int t = 0;
  58. __int64 a,x,y,u = n-1;
  59. while((u & 1) == 0)
  60. {
  61. t++;
  62. u >>= 1;
  63. }
  64. for(int i = 0; i < 50; i++)
  65. {
  66. a = rand() % (n-1)+1;
  67. x = mod_exp(a,u,n);
  68. for(int j = 0; j < t; j++)
  69. {
  70. y = mod_mul(x,x,n);
  71. if(y == 1 && x != 1 && x != n-1)
  72. return false;
  73. x = y;
  74. }
  75. if(x != 1)
  76. return false;
  77. }
  78. return true;
  79. }
  80. //PollarRho大整数因子分解
  81. __int64 minFactor;
  82. __int64 gcd(__int64 a,__int64 b)
  83. {
  84. if(b == 0)
  85. return a;
  86. return gcd(b, a % b);
  87. }
  88.  
  89. __int64 PollarRho(__int64 n, int c)
  90. {
  91. int i = 1;
  92. srand(time(NULL));
  93. __int64 x = rand() % n;
  94. __int64 y = x;
  95. int k = 2;
  96. while(true)
  97. {
  98. i++;
  99. x = (mod_exp(x,2,n) + c) % n;
  100. __int64 d = gcd(y-x,n);
  101. if(1 < d && d < n)
  102. return d;
  103. if(y == x)
  104. return n;
  105. if(i == k)
  106. {
  107. y = x;
  108. k *= 2;
  109. }
  110. }
  111. }
  112. __int64 ans[1100],cnt;
  113. void getSmallest(__int64 n, int c)
  114. {
  115. if(n == 1)
  116. return;
  117. if(miller_rabbin(n))
  118. {
  119. ans[cnt++] = n;
  120. return;
  121. }
  122. __int64 val = n;
  123. while(val == n)
  124. val = PollarRho(n,c--);
  125. getSmallest(val,c);
  126. getSmallest(n/val,c);
  127. }
  128. __int64 a,b,sq;
  129. void choose(__int64 s,__int64 val)
  130. {
  131. if(s >= cnt)
  132. {
  133. if(val > a && val <= sq)
  134. a = val;
  135. return;
  136. }
  137. choose(s+1,val);
  138. choose(s+1,val*ans[s]);
  139. }
  140.  
  141. int main()
  142. {
  143. int T;
  144. __int64 G,L;
  145. while(~scanf("%I64d%I64d",&G,&L))
  146. {
  147. if(L == G)
  148. {
  149. printf("%I64d %I64d\n",G,L);
  150. continue;
  151. }
  152. L /= G;
  153. cnt = 0;
  154. getSmallest(L,200);
  155. sort(ans, ans+cnt);
  156. int j = 0;
  157. for(int i = 1; i < cnt; i++)
  158. {
  159. while(ans[i-1] == ans[i] && i < cnt)
  160. ans[j] *= ans[i++];
  161. if ( i < cnt )
  162. ans[++j] = ans[i];
  163. }
  164.  
  165. cnt = j+1;
  166. a = 1;
  167. sq = (__int64)sqrt(L+0.0);
  168. choose(0,1);
  169. printf("%I64d %I64d\n",a*G,L/a*G);
  170. }
  171. return 0;
  172. }

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. 无法上外网, ping网关ping不通的解决——arp命令

    转自:http://jingyan.baidu.com/article/3c48dd34873909e10be35894.html 转自:http://man.linuxde.net/arp 用来管理 ...

  2. 《王者荣耀》技术总监复盘回炉历程:没跨过这三座大山,就是另一款MOBA霸占市场了

    如今已经大获市场成功的<王者荣耀>一直是业内各方关注的对象,而我们也知道这款产品在成为国民级游戏之前,也遇到过一段鲜有人知的调优期.也就是在2015年8月18号正式不删档测试版本推出之后, ...

  3. JAVA使用Marvin在图片中搜索图片

    Java对图像的处理框架比较少,目前比较流行的有Jmagick以及Marvin,但Jmagick只能处理图像(上篇Java清除图片中的恶意信息(利用Jmagick)中对Jmagick已做过简略介绍), ...

  4. 冰川时代5:星际碰撞Ice Age: Collision Course迅雷下载

    影片讲述松鼠奎特为了追松果,偶然引发了宇宙事件,改变并威胁着冰川时代的世界.为了拯救自己,话唠树懒希德.猛犸象曼尼.剑齿虎迪亚哥,以及别的动物群族必须离开家园,踏上了他们充满喜剧色彩的冒险旅程,他们来 ...

  5. C# byte[]和文件FileStream相互转化

    , pReadByte.Length);             }            catch            {                return false;        ...

  6. POI Excel表格合并,边框设置

    RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb); RegionUtil.setBorderBottom(1, cellRangeAdd ...

  7. Reloading Java Classes 201: How do ClassLoader leaks happen? Translation

    The original link : http://zeroturnaround.com/rebellabs/rjc201/ From ClassLoaders to Classes 从ClassL ...

  8. QMUI UI库 控件 弹窗 列表 工具类 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. mybatis @Select注解中如何拼写动态sql

    @Mapper public interface DemandCommentMapper extends BaseMapper<DemandComment>{ @Select(" ...

  10. 平均值(Mean)、方差(Variance)、标准差(Standard Deviation) (转)

    http://blog.csdn.net/xidiancoder/article/details/71341345 平均值 平均值的概念很简单:所有数据之和除以数据点的个数,以此表示数据集的平均大小: ...