x = lcm/gcd,假设答案为a,b,那么a*b = x且gcd(a,b) = 1,因为均值不等式所以当a越接近sqrt(x),a+b越小。

x的范围是int64的,所以要用Pollard_rho算法去分解因子。因为a,b互质,所以我们把相同因子一起处理。

最多16个不同的因子:2,3,5,7,11,13,17,19,23,29,31,37,41,43,47, 乘积为 614889782588491410, 乘上下一个质数53会爆int64范围。

所以剩下暴力枚举一下就好。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<ctime>
//#include<bits/stdc++.h>
using namespace std;typedef long long ll;ll mulMod(ll a, ll b, ll m) // a %= m
{ll re = 0;while(b){if(b&1){re = (re+a); if(re >= m) re -= m;}a <<= 1;if(a >= m) a -= m;b >>= 1;}return re;
}ll powMod(ll a,ll q,ll m)
{ll re = 1;while(q){if(q&1){re = mulMod(re,a,m);}a = mulMod(a,a,m);q >>= 1;}return re;
}bool witness(ll a,ll d,int t,ll n)
{ll x = powMod(a,d,n), y;while(t--){y = mulMod(x,x,n);if(y == 1 && x != 1 && x != n-1) return true;x = y;}return x != 1;
}bool RabinMiller(ll n,int k = 10)
{if(n == 2) return true;if(n < 2 || ((n&1)^1) ) return false;int t = 0;ll d = n-1;while((d&1)^1) { d>>=1; t++; }while(k--){if(witness(rand()%(n-1)+1,d,t,n)) return false;}return true;
}ll gcd(ll a,ll b)
{while(b){ll t = a%b;a = b;b = t;}return a < 0? -a: a;
}ll rho(ll n)
{ll x = rand()%(n-1) + 1;ll y = x, d;int i = 1, k = 2;ll c = 1+rand()%(n-1);while(true){i++;x = (mulMod(x,x,n) + c);if(x >= n) x -= n;d = gcd(y-x,n);if(d != 1 && d != n) return d;if(y == x) return n;if(i == k){y = x;k <<= 1;}}
}ll Prms[150], stk[150];
int tot;void pollard(ll n)
{tot = 0;int top = 0;stk[++top] = n;while(top){n = stk[top--];if(RabinMiller(n)){Prms[tot++] = n;}else{ll p = n;while(p >= n) p = rho(p);stk[++top] = p;stk[++top] = n/p;}}
}ll mpow(ll a,int q)
{ll re = 1;while(q){if(q&1) re *= a;a *= a;q>>=1;}return re;
}//#define LOCAL
int main()
{
#ifdef LOCALfreopen("in.txt","r",stdin);
#endifll g,l;while(~scanf("%I64d%I64d",&g,&l)){if(l == g){printf("%I64d %I64d\n",g,l);continue;}ll x = l/g;pollard(x);sort(Prms,Prms+tot);Prms[tot] = -1;int k = 0;for(int j = 0,i = 1, cnt = 1; i <= tot; i++) {if(Prms[i] == Prms[j]) cnt++;else {Prms[k++] = mpow(Prms[j],cnt);cnt = 1; j = i;}}ll sqr = floor(sqrt(x)), best = 1;for(int S = 1<<(tot = k); --S; ){ll cur = 1;for(int i = 0; i < tot; i++){if(S>>i&1) cur *= Prms[i];if(cur > sqr) break;}if(cur <= sqr && cur > best) best = cur;}printf("%I64d %I64d\n",g*best,l/best);}return 0;
}

转载于:https://www.cnblogs.com/jerryRey/p/4896245.html

POJ 2429 GCD LCM Inverse(Miller-Rabbin素性测试,Pollard rho质因子分解)相关推荐

  1. POJ 2429 GCD LCM Inverse

    设答案为ans1,ans2 ans1=a1*gcd,ans2=a2*gcd,a1,a2互质 gcd*a1*b1=lcm,gcd*a2*b2=lcm a1*b1=lcm=(ans1*ans2)/gcd= ...

  2. POJ 2429 GCD LCM Inverse ★(pollard-ρ DFS枚举)

    题目链接:http://poj.org/problem?id=2429 题目大意:给定gcd(a,b)和lcm(a,b)(<2^63),求a和b,如果有多种情况,输出和最小的情况.   首先gc ...

  3. POJ 2429 GCD LCM Inverse (整数分解,由gcd+lcm求a,b)

    题意:给你两个数a,b的最大公约数和最小公倍数,求a,b.(有多组a,b的情况下取a+b最小的) 题解:令 c = a * b / gcd(a,b),对 c 因式分解 假如c = p1^k1 * p2 ...

  4. GCD and LCM Aizu - 0005(辗转相除)+GCD LCM Inverse POJ - 2429(java或【Miller Rabin素数測试】+【Pollar Rho整数分解】)

    题目:GCD and LCM Aizu - 0005 Write a program which computes the greatest common divisor (GCD) and the ...

  5. POJ2429 GCDLCM Inverse(整数分解,由GCD+LCM求a,b)

    POJ2429 GCD&LCM Inverse(整数分解,由GCD+LCM求a,b) 分类: 数论 2013-02-12 22:00  180人阅读  评论(1)  收藏  举报 题目:GCD ...

  6. hdu-3071 Gcd Lcm game---质因数分解+状态压缩+线段树

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所 ...

  7. UVa 11388 - GCD LCM

    题目大意:给出两个数的最大公约数G和最小公倍数L,求出这两个数. 根据a*b = GCD * LCM,然后枚举判断就好了. 1 #include <cstdio> 2 typedef un ...

  8. Poj 1811 Prime Test 素数测试 Miller-Rabin 与 整数的因子分解 Pollard rho

    随机化算法,想尝试自己写一下,最后还是变成了抄代码... 代码参考了:POJ 1811 Prime Test(大素数判断和素因子分解) - kuangbin - 博客园 学习链接: Miller-Ra ...

  9. miller_rabin_素性测试

    摘自:http://blog.csdn.net/pi9nc/article/details/27209455 看了好久没看懂,最后在这篇博客中看明白了. 费马定理的应用,加上二次探测定理. Ferma ...

  10. 费马素性测试和米勒—拉宾素性测试

    chapter 1 Fermat's little theorem 费马小定理 费马小定理说的是:如果p是一个素数,那么对于任意一个整数a,a p − a 能被p整除,也可以用模运算表示如下: (p是 ...

最新文章

  1. 每一个开发人员都应该懂的 UML 规范
  2. 如何成为一名数据中心运营工程师?
  3. jQuery操作Select2控件
  4. 深度优先 广度优先
  5. python typeerror console未定义,Python ctypes加载错误:未定义的符号
  6. okHttp源码解析------待续
  7. 因“智”而治,数据库自动驾驶时代大门即将开启!
  8. System.getProperty(属性名)方法的使用
  9. io python 读取pdf_python自动化办公之 Python 解析 PDF
  10. Debian 10(buster) 更换国内软件源
  11. 【论文写作】学位论文写作过程中应注意的7个问题,非常实用
  12. HCIE Security 防火墙虚拟系统 备考笔记(幕布)
  13. 解决SVN安装语言包后无法选择中文的问题
  14. Kafka权威指南-学习笔记---第一章
  15. 李宇春-唐人街无损品质mp3-flac音乐歌曲网盘免费下载
  16. 7.25 1figting!
  17. php predis连接超时,如何解决redis连接超时问题
  18. 计算机的利弊四级作文,2016年6月英语四级作文范文:网络的利弊
  19. LAN8742 教程(2) 数据手册 中文翻译(2)
  20. Java实训——桌面日历

热门文章

  1. C是一个结构化语言它的重点在于算法和数据结构
  2. 走进内存,走进汇编指令来看C/C++指针
  3. 安装配置文件共享协议(SAMBA)
  4. 《赢在中国》第二赛季108将进京
  5. Maven仓库—Nexus环境搭建及简单介绍
  6. 面试4轮字节Java研发岗,最终拿下Offer(原题复盘)
  7. 开放 API 接口签名验证,让你的接口从此不再裸奔 !
  8. 一个 Java 方法,最多能定义多少参数?
  9. 今天 Java 14 正式发布了!放弃 Java 8 行吗?
  10. 后端开发都应该掌握的Redis基础