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质因子分解)的更多相关文章

  1. [POJ 2429] GCD & LCM Inverse

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

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

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

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

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

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

  6. poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

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

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

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

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

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

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

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

随机推荐

  1. vue -- 项目调试

    方式1:vue-devtools插件 vue-devtools是一款基于chrome游览器的插件,用于调试vue应用,这可以极大地提高我们的调试效率. 使用步骤 1. 到github下载:https: ...

  2. 51nod1010(枚举+二分)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 题意:中文题诶- 思路:求第一个比 x (1<= ...

  3. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  4. [Xcode 实际操作]三、视图控制器-(11)在Storyboard中使用表格控件

    目录:[Swift]Xcode实际操作 本文将演示表格控件在故事板中的使用. 点击[显示或隐藏检查器按钮],再界面右侧打开检查器面板. 在控制器根视图上点击鼠标,以选择该根视图. 现在往根视图中添加一 ...

  5. MySQL zip安装问题

    今天安装mysql的压缩版出现了问题,就是服务总是启动不了,折腾了两三个小时.后面实在是想不明白,就直接把注册表的东西删了. 如果你之前安装过mysql,则进行删除mysql:E:\work\mysq ...

  6. PartTime_网址_国外

    https://www.douban.com/group/topic/6248314/ 国外威客网站大全 国外兼职网站,以及国外外包网站.这些国外项目网站包括的项目类型很多:logo设计.图形设计.f ...

  7. Pandas处理数据常用方法

    # -*- coding: utf-8 -*-import pandas as pd"""(1)利用pandas读取csv文件"""def ...

  8. C数据结构与算法-算法复杂度

    算法复杂度分为时间复杂度T(n)和空间复杂度F(n) 时间复杂度:也就是执行算法程序所需的时间,与硬件的速度.编程语言的级别.编译器的优化.数据的规模.执行的频度有关,前三个有很大的不确定性,所以衡量 ...

  9. jquery字符串数组转json字符串 C#json字符串转字符串list

    一.jquery字符串数组转json字符串 var str=['1','2','3']; var jsonText= JSON.stringify(str);//把一个对象转换成json字符串 str ...

  10. 【Java密码学】XML签名

    http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html XML签名的结构和类型 基本上XML签名 ...