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

  1. 3 60

Sample Output

  1. 12 15

Source

POJ Achilles
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <ctime>
  7. using namespace std;
  8. #define INF 0x3f3f3f3f3f3f3f3f
  9. #define ll long long
  10. #define S 8
  11.  
  12. ll mult(ll a,ll b,ll mod)
  13. {
  14. a%=mod,b%=mod;
  15. ll ret=;
  16. while(b)
  17. {
  18. if(b&)
  19. {
  20. ret+=a;
  21. if(ret>=mod) ret-=mod;
  22. }
  23. a<<=;
  24. if(a>=mod) a-=mod;
  25. b>>=;
  26. }
  27. return ret;
  28. }
  29. ll pow(ll a,ll n,ll mod)
  30. {
  31. a=a%mod;
  32. ll ret=;
  33. while(n)
  34. {
  35. if(n&) ret=mult(ret,a,mod);
  36. a=mult(a,a,mod);
  37. n>>=;
  38. }
  39. return ret;
  40. }
  41. bool check(ll a,ll n,ll x,ll t)
  42. {
  43. ll ret=pow(a,x,n),last=ret;
  44. for(int i=;i<=t;i++)
  45. {
  46. ret=mult(ret,ret,n);
  47. if(ret== && last!= && last!=n-) return ;
  48. last=ret;
  49. }
  50. if(ret!=) return ;
  51. return ;
  52. }
  53. bool Miller_Rabin(ll n)
  54. {
  55. if(n<) return ;
  56. if(n==) return ;
  57. if((n&)==) return ;
  58. ll x=n-,t=;
  59. while((x&)==) { x>>=;t++;}
  60. srand(time(NULL));
  61. for(int i=;i<S;i++)
  62. {
  63. ll a=rand()%(n-)+;
  64. if(check(a,n,x,t)) return ;
  65. }
  66. return ;
  67. }
  68. int tot;
  69. ll factor[];
  70. ll gcd(ll a,ll b)
  71. {
  72. ll t;
  73. while(b)
  74. {
  75. t=a;
  76. a=b;
  77. b=t%b;
  78. }
  79. if(a>=) return a;
  80. return -a;
  81. }
  82. ll pollard_rho(ll x,ll c)
  83. {
  84. ll i=,k=;
  85. srand(time(NULL));
  86. ll x0=rand()%(x-)+;
  87. ll y=x0;
  88. while()
  89. {
  90. i++;
  91. x0=(mult(x0,x0,x)+c)%x;
  92. ll d=gcd(y-x0,x);
  93. if(d!= && d!=x) return d;
  94. if(y==x0) return x;
  95. if(i==k) y=x0,k+=k;
  96. }
  97. }
  98. void FindFac(ll n,int k=)
  99. {
  100. if(n==) return;
  101. if(Miller_Rabin(n))
  102. {
  103. factor[tot++]=n;
  104. return;
  105. }
  106. ll p=n;
  107. int c=k;
  108. while(p>=n) p=pollard_rho(p,c--);
  109. FindFac(p,k);
  110. FindFac(n/p,k);
  111. }
  112. ll ansx,ansy,ans;
  113. void dfs(int k,ll x,ll y)
  114. {
  115. if(k>=tot)
  116. {
  117. if(x+y<ans)
  118. {
  119. ans=x+y;
  120. ansx=x;
  121. ansy=y;
  122. }
  123. return;
  124. }
  125. dfs(k+,x*factor[k],y);
  126. dfs(k+,x,y*factor[k]);
  127. }
  128. int main()
  129. {
  130. int i,j;
  131. ll n,m;
  132. while(scanf("%lld%lld",&m,&n)!=EOF)
  133. {
  134. tot=;
  135. ans=INF; //注意初始化
  136. FindFac(n/m,);
  137. sort(factor,factor+tot);
  138. for(i=j=;i<tot;i++)
  139. {
  140. ll tmp=factor[i];
  141. while(i+<tot && factor[i]==factor[i+]) //注意边界
  142. {
  143. tmp*=factor[i];
  144. i++;
  145. }
  146. factor[j++]=tmp;
  147. }
  148. tot=j;
  149. dfs(,,);
  150. if(ansx>ansy) swap(ansx,ansy);
  151. printf("%lld %lld\n",ansx*m,ansy*m);
  152. }
  153. return ;
  154. }

[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. (转载)SQL语句,纵列转横列

    SQL语句,纵列转横列 Feed: 大富翁笔记 Title: SQL语句,纵列转横列 Author: wzmbox Comments sTable.db库位 货物编号 库存数1 0101 501 01 ...

  2. 【Python笔记】异常处理

    1 什么是异常 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误. 当Pytho ...

  3. 奖学金评分系统(系统分析与设计版与Delphi实现代码)

    一.系统规划 1.1 项目背景介绍 在奖学金评比过程中,学生综合测评是学校普遍采用的评比手段.对学生实施综合素质测评的目的在于正确评价学生的综合素质,为评奖学金提供依据,实现学生教育管理工作的标准化. ...

  4. NYOJ 994 海盗分金 逆向递推

    链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=994 题意: 有n个海盗劫得了窖藏的m块金子,并准备瓜分这些战利品.按照古老流传下来的分金法则 ...

  5. 2016 系统设计第一期 (档案一)MVC 引用 js css

    @Styles.Render("~/Bootstrap/css/bootstrap-theme.css") @Scripts.Render("~/jQuery/jquer ...

  6. DNF技能贴图的研究

    一直在猜想DNF的技能贴图怎么贴的,靠在游戏里慢慢移动确定技能的偏移太费时间了.前段发现了“可视坐标生成”这软件,针对DNF改衣服,装备款式的小工具,就自己写了个类似的. 从图上看,技能的域中心点和人 ...

  7. 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法

    利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. 先来个简单的实例热热身吧. 1.无参数的方法调用 asp.net code: view plaincopy to clip ...

  8. Ubuntu Linux启用root用户登录

    Ubuntu Linux有一个与众不同的特点,那就是初次使用时,你无法作为root来登录系统,为什么会这样?这就要从系统的安装说起.对于其他Linux系统来 说,一般在安装过程就设定root密码,这样 ...

  9. Xcode常用快捷键及代码格式刷(缩进)方法-b

    Xcode版本:4.5.1 一.总结的常用命令: 隐藏xcode command+h 退出xcode command+q 关闭窗口 command+w 关闭所有窗口 command+option+w ...

  10. iOS 支付宝应用(备用参考2)

    接入前期准备工作包括商户签约和密钥配置 步骤1:  启动IDE(如Xcode),把iOS包中的压缩文件中以下文件拷贝到项目文件夹下, 并导入到项目工程中. AlipaySDK.bundle    Al ...