先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小。

这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜索的层数也变的非常少了。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<math.h>
  5. #include<stdlib.h>
  6. #include<time.h>
  7. #include<algorithm>
  8. using namespace std;
  9. typedef long long LL;
  10. #define maxn 10000
  11. LL factor[maxn];
  12. int tot;
  13. const int S=10; //測试次数
  14. LL muti_mod(LL a,LL b,LL c)
  15. {
  16. a%=c;b%=c;
  17. LL ret=0;
  18. while (b){
  19. if (b&1){
  20. ret+=a;
  21. if (ret>=c) ret-=c;
  22. }
  23. a<<=1;
  24. if (a>=c) a-=c;
  25. b>>=1;
  26. }
  27. return ret;
  28. }
  29. LL pow_mod(LL x,LL n,LL mod)
  30. {
  31. if (n==1) return x%mod;
  32. int bit[90],k=0;
  33. while (n){
  34. bit[k++]=n&1;
  35. n>>=1;
  36. }
  37. LL ret=1;
  38. for (k=k-1;k>=0;k--){
  39. ret=muti_mod(ret,ret,mod);
  40. if (bit[k]==1) ret=muti_mod(ret,x,mod);
  41. }
  42. return ret;
  43. }
  44. bool check(LL a,LL n,LL x,LL t){ //以a为基,n-1=x*2^t,检验n是不是合数
  45. LL ret=pow_mod(a,x,n),last=ret;
  46. for (int i=1;i<=t;i++){
  47. ret=muti_mod(ret,ret,n);
  48. if (ret==1 && last!=1 && last!=n-1) return 1;
  49. last=ret;
  50. }
  51. if (ret!=1) return 1;
  52. return 0;
  53. }
  54. bool Miller_Rabin(LL n){ //是素数返回0,合数返回1
  55. LL x=n-1,t=0;
  56. while ((x&1)==0) x>>=1,t++;
  57. bool flag=1;
  58. if (t>=1 && (x&1)==1){
  59. for (int k=0;k<S;k++){
  60. LL a=rand()%(n-1)+1;
  61. if (check(a,n,x,t)) {flag=1;break;}
  62. flag=0;
  63. }
  64. }
  65. if (!flag || n==2) return 0;
  66. return 1;
  67. }
  68.  
  69. LL gcd(LL a,LL b){
  70. if (a==0) return 1;
  71. if (a<0) return gcd(-a,b);
  72. while (b){
  73. LL t=a%b; a=b; b=t;
  74. }
  75. return a;
  76. }
  77. LL Pollard_rho(LL x,LL c){
  78. LL i=1,x0=rand()%x,y=x0,k=2;
  79. while (1){
  80. i++;
  81. x0=(muti_mod(x0,x0,x)+c)%x;
  82. LL d=gcd(y-x0,x);
  83. if (d!=1 && d!=x){
  84. return d;
  85. }
  86. if (y==x0) return x;
  87. if (i==k){
  88. y=x0;
  89. k+=k;
  90. }
  91. }
  92. }
  93. void findfac(LL n)//质因数分解,存在factor里
  94. {
  95. if (!Miller_Rabin(n)){
  96. factor[tot++] = n;
  97. return;
  98. }
  99. LL p=n;
  100. while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1);
  101. findfac(p);
  102. findfac(n/p);
  103. }
  104. LL mins,aa,bb;
  105. int top;
  106. void dfs(LL a,LL b,int p)
  107. {
  108. if(a+b>=mins) return;
  109. if(p==top)
  110. {
  111. if(a+b<mins)
  112. {
  113. mins=a+b;
  114. aa=a;
  115. bb=b;
  116. }
  117. return;
  118. }
  119. dfs(a*factor[p],b,p+1);
  120. dfs(a,b*factor[p],p+1);
  121. }
  122. int main()
  123. {
  124. LL a,b,c;
  125. while(~scanf("%lld%lld",&a,&b))
  126. {
  127. if(a==b) {printf("%lld %lld\n",a,b);continue;}
  128. mins=~0ull>>1;
  129. c=b/a;
  130. tot=0;
  131. findfac(c);
  132. sort(factor,factor+tot);
  133. top=0;
  134. for(int i=0;i<tot;i++)
  135. {
  136. if(i==0) factor[top++]=factor[i];
  137. else if(factor[i]==factor[i-1]) factor[top-1]*=factor[i];
  138. else factor[top++]=factor[i];
  139. }
  140. dfs(a,a,0);
  141. if(aa>bb) swap(aa,bb);
  142. printf("%lld %lld\n",aa,bb);
  143. }
  144. return 0;
  145. }

poj 2429 Pollard_rho大数分解的更多相关文章

  1. Pollard_Rho大数分解模板题 pku-2191

    题意:给你一个数n,  定义m=2k-1,   {k|1<=k<=n},并且 k为素数;  当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...

  2. 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811

    题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...

  3. poj 1811 随机素数和大数分解(模板)

    Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...

  4. HDU4344(大数分解)

    题目:Mark the Rope 题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个 数,并且求出和最大的值. 找规律就 ...

  5. Miller-Rabbin 素性测试 和 Pollard_rho整数分解

    今天学习一下Miller-Rabbin  素性测试 和 Pollard_rho整数分解. 两者都是概率算法. Miller_Rabbin素性测试是对简单伪素数pseudoprime测试的改进. (ps ...

  6. poj1181 大数分解

    //Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...

  7. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  8. poj 1811 大数分解

    模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...

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

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

随机推荐

  1. PPT去掉图片白色背景

    双击图片,点击菜单栏“删除背景”,用矩形框选中想要的区域,然后将鼠标焦点移到图片外,单击鼠标即可.

  2. 关于C#基类和子类函数调用问题

    c#基类子类的函数调用关系,代码说明newkeyword后面的类中的函数为对象调用的函数,当然必需要有virtual和override,继承就相当于包括了基类的函数,子类对象调用时基类的函数相当于就在 ...

  3. 在多线程环境中使用Jedis

    Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口. Jedis不是线程安全的.故不应该在多线程环境中共用一个Jedis实例.可是.也应该避免直接 ...

  4. 阿里云部署Docker(5)----管理和公布您的镜像

    出到这节,我在百度搜索了一下"阿里云部署Docker",突然发现怎么会有人跟我写的一样呢?哦,原来是其它博客系统的爬虫来抓取,然后也不会写转载自什么什么的.所以,我最终明确为什么那 ...

  5. 4. 绘制光谱曲线QGraphicsView类

    一.前言 Qt的QGraphicsView类具有强大的视图功能,与其一起使用的还有QGraphicsScene类和QGraphicsItem类.大体思路就是通过构建场景类,然后向场景对象中增加各种图元 ...

  6. 15. SSH 远程

    一.原理:     使用SSH连接Centos时,我们可以创建一个公钥和一个私钥,公钥放在服务端,私钥放在客户端,当客户端去连接服务端时,会先去查找密钥,     要是客户端的私钥可以和服务端的公钥匹 ...

  7. SQL按汉语拼音首字母排序

    以常用到的省的数据表(province)为例,其中name字段为省的名称,SQL语句如下: ))) as py ,a.name from province a left outer join ( se ...

  8. js基础-需要注意的地方

    ---因为跟别的语言很像,所以只记录要注意的地方 1.== 和 === 的区别 ===要求类型也相等 "5"==5 = ture "5"===5 = false ...

  9. C++中的条件传送代码

    条件传送代码-这种代码先计算一个条件操作的两种结果,然后再条件从而选其中一个-条件传送代码匹配了现代处理器的性能特征(因为现代处理器是流水线) void minmax2(int a[],int b[] ...

  10. (整理)ubuntu 的 相关知识(来自 鸟哥的私房菜)

    1. Linux 文件权限概念 $ ls 察看文件的指令 $ ls -al 出所有的文件详细的权限与属性 (包含隐藏档,就是文件名第一个字符为『 . 』的文件) 在你第一次以root身份登入Linux ...