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质因子分解后 ...
随机推荐
- SAS笔记(4) FIRST.和LAST.临时变量
FIRST.和LAST.临时变量是SAS很有特色的一点,我在R和Python中暂时没有发现类似的功能(也许它们也有这个功能,我不知道而已).考虑这样一种场景:我们有患者就诊的数据,每一条观测对应一个患 ...
- [Xcode 实际操作]三、视图控制器-(10)在Storyboard中使用图像视图控件
目录:[Swift]Xcode实际操作 本文将演示常用的图像视图控件在故事板中的使用. 打开故事板文件[Main.storyboard]点击选择视图控制器的根视图. 点击库图标,打开控件库面板. 在控 ...
- 解读人:刘佳维,Spectral Clustering Improves Label-Free Quantification of Low-Abundant Proteins(谱图聚类改善了低丰度蛋白的无标记定量)
发表时间:(2019年4月) IF:3.95 单位: 维也纳医科大学: 欧洲生物信息研究所(EMBL-EBI): 分子病理学研究所: 奥地利科学院分子生物技术研究所: Gregor Mendel分子植 ...
- Unity---DOTween插件学习(2)---设置参数、Ease曲线、回调函数、动画控制函数
目录 6.Set设置参数 7.Ease曲线 8.回调函数 9.动画控制函数 本文及系列参考于Andy老师的DOTween系列 欢迎大家关注Andy老师 6.Set设置参数 在Unity中添加一个Cub ...
- POJ1321-棋盘问题
题目链接:点击打开链接 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和 ...
- Kipmi0 占用CPU 100%
查看当前正在运行的进程发现kipmi0进程占用率达到100% kipmi -% of the CPUs, %/% comes when the machine is idle? A second is ...
- UVa 10256(凸包、线段交、点在多边形内)
要点 红蓝点分别求凸包显然 判断两凸包是否相交方法:所有红点不在蓝凸包内,反之亦然:所有红凸包线不与蓝凸包线相交,反之亦然. 书上让特判一下凸包退化成点或线段的情况,为什么我感觉代码仓库的代码并没特判 ...
- 练习十八:求这样的一组数据和,s=a+aa+aaa+aaaa+aa...a,其中a为一个数字
例如:2+22+222+2222+22222(此时共有5个数字相加),这里具体几个数字由键盘控制 方法一:普通做法 a = int(input("计算要加的数(1-9之间数):") ...
- py---------socketserver
同时两个客户端连接, server 不能有input server端根据client端的要求去执行固定的代码 server.py #-*- coding:utf-8 -*- import time i ...
- PartTime_网址_国外
https://www.douban.com/group/topic/6248314/ 国外威客网站大全 国外兼职网站,以及国外外包网站.这些国外项目网站包括的项目类型很多:logo设计.图形设计.f ...