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质因子分解后 ...
随机推荐
- SCUT - 289 - 小O的数字 - 数位dp
https://scut.online/p/289 一个水到飞起的模板数位dp. #include<bits/stdc++.h> using namespace std; typedef ...
- php soap连接https的wsdl报错SOAP-ERROR: Parsing WSDL:Couldn't load from
转发:https://blog.csdn.net/keyunq/article/details/51804728 SOAP-ERROR: Parsing WSDL:Couldn’t load from ...
- ARC085F(动态规划,线段树)
#include<bits/stdc++.h>using namespace std;const int maxn = 0x3f3f3f3f;int mn[801000];int cost ...
- 解决Navicat无法连接到Mysql
Navicat无法连接到Mysql,返回的错误码是Lost connection to MySQL server at ‘reading initial communication packet’, ...
- 洛谷P3478 [POI2008]STA-Station
P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...
- 全网排名第一的免费开源ERP Odoo Git源代码部署教程
文/开源智造联合创始人老杨 本文来自<开源自主OdooERP部署架构指南>试读:第三章-Git源代码部署 .书籍尚未出版,请勿转载.欢迎您反馈阅读意见. 我们将从git源代码部署Odoo ...
- vue 中的.sync语法糖
提到父子组件相互通信,可能大家的第一反应是$emit,最近在学着封装组件,以前都是用的别人封装好的UI组件,对vue中的.sync这个修饰符有很大的忽略,后来发现这个修饰符很nice,官方对她的描述是 ...
- require、require_once、include、include_once
在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容. include 和 require 语句用于在执行流中插入写在其他文件中的有用的代码. include 和 requ ...
- ch8 -- useLK
useLK 光流法跟踪FAST角点 执行 ./useLK ../../data 运行程序. 光流法需要include<opencv2/video/tracking.hpp>,用到列表 ...
- uoj455 【UER #8】雪灾与外卖
http://uoj.ac/problem/455 题解: https://blog.csdn.net/litble/article/details/88410435 https://www.mina ...