题目大意

给定两个数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

这样的话我们只要对L/G进行质因数分解,找出最接近√(L/G)的因子p,最终结果就是a=p*G,b=L/p,对(L/G)就是套用Miller–Rabin和Pollard's rho了,刚开始Pollard's rho用的函数也是

f(x)=x^2+1,然后死循环了。。。。改成f(x)=x^2+c(c<=181)就OK了

代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #include<math.h>
  5. #include<algorithm>
  6. #define MAXN 100000
  7. using namespace std;
  8. typedef unsigned long long LL;
  9. LL fac[MAXN],cnt,G,L,m,p;
  10. LL min(LL a,LL b)
  11. {
  12. return a<b?a:b;
  13. }
  14. LL gcd(LL a,LL b)
  15. {
  16. return b==0?a:gcd(b,a%b);
  17. }
  18. LL mult_mod(LL a,LL b,LL mod)
  19. {
  20. LL ans=0;
  21. while(b)
  22. {
  23. if(b&1)
  24. ans=(ans+a)%mod;
  25. a=(a<<1)%mod;
  26. b>>=1;
  27. }
  28. return ans;
  29. }
  30. LL pow_mod(LL a,LL b,LL mod)
  31. {
  32. LL d=1;
  33. a%=mod;
  34. while(b)
  35. {
  36. if(b&1)
  37. d=mult_mod(d,a,mod);
  38. a=mult_mod(a,a,mod);
  39. b>>=1;
  40. }
  41. return d%mod;
  42. }
  43. bool witness(LL a,LL n)
  44. {
  45. LL u=n-1,t=0;
  46. while((u&1)==0)
  47. {
  48. u>>=1;
  49. t++;
  50. }
  51. LL x,x0=pow_mod(a,u,n);
  52. for(LL i=1; i<=t; i++)
  53. {
  54. x=mult_mod(x0,x0,n);
  55. if(x==1&&x0!=1&&x0!=(n-1))
  56. return true;
  57. x0=x;
  58. }
  59. if(x!=1)
  60. return true;
  61. return false;
  62. }
  63. bool miller_rabin(LL n)
  64. {
  65. if(n==2) return true;
  66. if(n<2||!(n&1)) return false;
  67. for(int j=1; j<=8; j++)
  68. {
  69. LL a=rand()%(n-1)+1;
  70. if(witness(a,n))
  71. return false;
  72. }
  73. return true;
  74. }
  75. LL pollard_rho(LL n,LL c)
  76. {
  77. LL i=1,k=2,d,x=2,y=2;
  78. while(true)
  79. {
  80. i++;
  81. x=(mult_mod(x,x,n)+c)%n;
  82. d=gcd(y-x,n);
  83. if(d!=1&&d!=n)
  84. return d;
  85. if(x==y) return n;
  86. if(i==k)
  87. {
  88. y=x;
  89. k<<=1;
  90. }
  91. }
  92. }
  93. void find_fac(LL n,LL c)
  94. {
  95. if(miller_rabin(n)||n<=1)
  96. {
  97. fac[cnt++]=n;
  98. return;
  99. }
  100. LL p=pollard_rho(n,c);
  101. while(p>=n) p=pollard_rho(p,c--);
  102. find_fac(p,c);
  103. find_fac(n/p,c);
  104. }
  105. void dfs( LL step,LL num)
  106. {
  107. if(step==cnt||num>p)
  108. {
  109. if(num<=p&&num>m)
  110. m=num;
  111. return;
  112. }
  113. dfs(step+1,num*fac[step]);
  114. dfs(step+1,num);
  115. }
  116. int main()
  117. {
  118. srand(time(NULL));
  119. while(scanf("%I64u%I64u",&G,&L)!=EOF)
  120. {
  121. cnt=0;
  122. find_fac(L/G,181);
  123. sort(fac,fac+cnt);
  124. LL i=0,t=0;
  125. while(i<cnt)
  126. {
  127. LL ans=1,j=i;
  128. while(j<cnt&&fac[i]==fac[j])
  129. {
  130. ans*=fac[i];
  131. j++;
  132. }
  133. fac[t++]=ans;
  134. i=j;
  135. }
  136. cnt=t,m=1,p=sqrt(0.0+(L/G));
  137. dfs(0,1);
  138. printf("%I64u %I64u\n",m*G,L/m);
  139. }
  140. return 0;
  141. }

POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)的更多相关文章

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

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

  2. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  3. 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式

    找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...

  4. 【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse

    题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B. n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的. 于是对n/m质因子分解后 ...

  5. poj2429 GCD & LCM Inverse

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

  6. [POJ 2429] GCD & LCM Inverse

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

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

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

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

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

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

随机推荐

  1. httpcontext in asp.net unit test

    [TestMethod] [HostType("ASP.NET")] [UrlToTest("http://localhost:25153/qq/a.aspx" ...

  2. 视频边下边播--缓存播放数据流-b

    google搜索“iOS视频变下边播”,有好几篇博客写到了实现方法,其实只有一篇,其他都是copy的,不过他们都是使用的本地代理服务器的方式. 原理很简单,但是缺点也很明显,需要自己写一个本地代理服务 ...

  3. centos 下 yum 安装 nginx 平滑切换安装到 Tengine

    ---恢复内容开始--- 据说淘宝的Tengine很牛X,所以我们今天也来玩玩,我们这里是某开放云的vps,现在已经安装好了nginx,现在我们要平滑切换到安装Tengine. 下载Tengine,解 ...

  4. js library

    jquery.js prototype.js requirejs.js backbone.js modernizr.js knockout.js http://share.renren.com/sha ...

  5. js高手

    http://kb.cnblogs.com/page/173798/ http://kb.cnblogs.com/page/121539/ http://blog.jobbole.com/9648/ ...

  6. HDU 1098 Ignatius's puzzle

    http://acm.hdu.edu.cn/showproblem.php?pid=1098 题意 :输入一个K,让你找一个a,使得f(x)=5*x^13+13*x^5+k*a*x这个f(x)%65等 ...

  7. android 服务service开启和关闭

    startService()方法开启一个服务. 服务只会开启一次,如果服务已经创建,并且没有销毁,多次调用startService方法只会执行onStartCommand方法和onStart方法. 服 ...

  8. MongoDB实战指南(三):MongoDB的锁机制

    与关系数据库一样,MongoDB也是通过锁机制来保证数据的完整性和一致性,MongoDB利用读写锁来支持并发操作,读锁可以共享写锁具有排他性.当一个读锁存在时,其他读操作也可以用这个读锁:但当一个写锁 ...

  9. ASP.NET在主题中添加CSS文件

    ASP.NET在主题中添加CSS文件 在ASP.NET中,可以使用CSS来控制页面上HTML元素和ASP.NET控件的皮肤.如果在主题文件夹中添加了CSS文件,则在页面应用主题时也会自动应用CSS. ...

  10. easyui源码翻译1.32--Messager(消息窗口)

    前言 使用$.messager.defaults重写默认值对象.下载该插件翻译源码 消息窗口提供了不同的消息框风格,包含alert(警告框), confirm(确认框), prompt(提示框), p ...