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算法去分解因子。因为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 = ;
while(b){
if(b&){
re = (re+a); if(re >= m) re -= m;
}
a <<= ;
if(a >= m) a -= m;
b >>= ;
}
return re;
} ll powMod(ll a,ll q,ll m)
{
ll re = ;
while(q){
if(q&){
re = mulMod(re,a,m);
}
a = mulMod(a,a,m);
q >>= ;
}
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 == && x != && x != n-) return true;
x = y;
}
return x != ;
} bool RabinMiller(ll n,int k = )
{
if(n == ) return true;
if(n < || ((n&)^) ) return false;
int t = ;
ll d = n-;
while((d&)^) { d>>=; t++; }
while(k--){
if(witness(rand()%(n-)+,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 < ? -a: a;
} ll rho(ll n)
{
ll x = rand()%(n-) + ;
ll y = x, d;
int i = , k = ;
ll c = +rand()%(n-);
while(true){
i++;
x = (mulMod(x,x,n) + c);
if(x >= n) x -= n;
d = gcd(y-x,n);
if(d != && d != n) return d;
if(y == x) return n;
if(i == k){
y = x;
k <<= ;
}
}
} ll Prms[], stk[];
int tot; void pollard(ll n)
{
tot = ;
int top = ;
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 = ;
while(q){
if(q&) re *= a;
a *= a;
q>>=;
}
return re;
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
ll 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] = -;
int k = ;
for(int j = ,i = , cnt = ; i <= tot; i++) {
if(Prms[i] == Prms[j]) cnt++;
else {
Prms[k++] = mpow(Prms[j],cnt);
cnt = ; j = i;
}
}
ll sqr = floor(sqrt(x)), best = ;
for(int S = <<(tot = k); --S; ){
ll cur = ;
for(int i = ; i < tot; i++){
if(S>>i&) cur *= Prms[i];
if(cur > sqr) break;
}
if(cur <= sqr && cur > best) best = cur;
}
printf("%I64d %I64d\n",g*best,l/best);
} return ;
}
POJ 2429 GCD & LCM Inverse(Miller-Rabbin素性测试,Pollard rho质因子分解)的更多相关文章
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)
[题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...
- 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 ...
- 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 这 ...
- 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 ...
- poj 2429 GCD & LCM Inverse 【java】+【数学】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9928 Accepted: ...
- Mathematics:GCD & LCM Inverse(POJ 2429)
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- 【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse
题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B. n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的. 于是对n/m质因子分解后 ...
随机推荐
- python文件名匹配
待匹配文件:#FY3D_IPMNT_GBAL_L1_20180516_0003_030KM_MS.HDF 干扰文件:#FY3D_IPMNT_GBAL_L1_20180516_0003_030KM_MS ...
- bzoj3731: Gty的超级妹子树(树分块)
传送门 分块树,代码参考了Manchery的 具体细节还是看代码好了 这题卡常……注意常数写好点…… //minamoto #include<iostream> #include<c ...
- css 实现三级联动菜单
昨天因为项目中想要把二级联动菜单改成三级联动菜单,所以我就单独写了一个tab导航栏,用纯css的方式实现的三级联动.一开始我想着可以用js实现,但是js的hover事件和mouseenter,mous ...
- CF987C Three displays 解题报告
题目传送门 题目大意 n个位置,每个位置有两个属性s,c,要求选择3个位置i,j,k,使得s_i<s_j<s_k,并使得c_i+c_j+c_k最小 方法1 n³枚举每一种情况(也许可以拿 ...
- chrome安装文件点击没有反应(收藏用)
备份Chrome浏览器用户数据 关闭Chrome浏览器,用Windows资源管理器打开%LOCALAPPDATA%\Google,复制Chrome文件夹到其它目录. 打开程序和功能管理功能 按下W ...
- weblogic.xml
<?xml version="1.0" encoding="UTF-8"?> <weblogic-web-app xmlns="ht ...
- UnityError 打包到Android错误解决
- (转)diff 命令
每天一个linux命令(36):diff 命令 原文:http://www.cnblogs.com/peida/archive/2012/12/12/2814048.html diff 命令是 li ...
- 一,JVM 自带命令行工具之JPS
jps:虚拟机进程状况工具 可以列出正在运行的虚拟机进程,并显示虚拟机执行主类(main class,class()函数所在的类)的名称,以及这些进程的本地虚拟机的唯一ID. jps命令格式: jps ...
- 27 个Jupyter Notebook的小提示与技巧
不多说,直接上干货! 见 http://liuchengxu.org/pelican-blog/jupyter-notebook-tips.html