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质因子分解后 ...
随机推荐
- OpenStack基础知识-virtualenv工具详解
1.virtualenv介绍 virtualenv通过创建一个单独的虚拟化python运行环境,将我们所需的依赖安装进去,不同项目之间相互不干扰,从而解决不同的项目之间依赖不同,造成的冲突问题 2.安 ...
- [CentOS7] 常用工具 之 差异备份工具 rdiff-backup
差异备份: rdiff-backup ==> 用于累积差异备份,加上自己写的shell script,每日备份,效果更佳 先用yum search rdiff-backup看看是否yum源含有r ...
- [poj 1837] Balance dp
Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...
- 使用imp命令和exp命令对oracle数据库进行导入导出操作
命令说明 imp命令和exp命令需要在cmd命令窗口下执行,并且需要在系统环境变量中配置imp,exp命令所在目录 导出命令 --全库导出 exp system/systempassword@orcl ...
- 洛谷P2068 统计和
题目描述 给定一个长度为\(n(n \leq 100000)\),初始值都为\(0\)的序列,\(x(x \leq 10000)\)次的修改某些位置上的数字,每次加上一个数,然后提出\(y (y \l ...
- PAT甲级——1093 Count PAT's (逻辑类型的题目)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93389073 1093 Count PAT's (25 分) ...
- vue中点击空白处隐藏弹框(用指令优雅地实现)
在写vue的项目的时候,弹框经常性出现,并要求点击弹框外面,关闭弹框,那么如何实现呢?且听我一一...不了,能实现效果就好 <template> <div> <div c ...
- 原生DOM操作方法小结
1.0 DOM结构 父节点 兄弟节点 当前节点 属性节点 子节点 兄弟节点 一般任意一个节点我们都会从父节点,子节点,以及兄弟节点的角度去考察.节点一般我们只需关注元素节点,属性节点及文本节点即可 ...
- excel单元格内容合并
这几天在整理数据,有时候数据都在表格的不同单元格中,想把两格内容合并为一格,于是验证了两种方法 方法一: (1)在B1输入公式=A1&B1 (2)做完第一步后,选中B1后,鼠标移到单元格右下出 ...
- jquery——下载、使用
jQuery是目前使用最广泛的javascript函数库. 怎样安装? 这是下载地址:https://code.jquery.com/ minified是压缩版的 新建一个网页打开上面这个网址,ctr ...