本题涉及的算法个人无法完全理解,在此提供两个比较好的参考。

原理 (后来又看了一下,其实这篇文章问题还是有的……有时间再搜集一下资料)

代码实现

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long int top;
const int S = ;
ll sta[], Mul[]; ll gcd(ll a, ll b) {
if (b == ) return a;
return gcd(b, a % b);
} ll mul(ll a, ll b, ll mod) {
ll r = ;
while (b) {
if (b & ) {
r = r - mod + a;
if (r < ) r += mod;
//r = (r + a) % mod;
}
a = a - mod + a;
if (a < ) a += mod;
//a = (a + a) % mod;
b >>= ;
}
return r;
} // 64位数相乘 ll ksm(ll a, ll n, ll mod) {
ll r = ;
while (n) {
if (n & ) r = mul(r, a, mod);
a = mul(a, a, mod);
n >>= ;
}
return r;
} // 64位数乘方 bool isprime(ll n) {
if (n == ) return true;
if (n < || (n & ) == ) return false;
ll a, x, y, u = n - ;
int t = ;
while ((u & ) == ) {
t++;
u >>= ;
}
for (int i = ; i < S; i++) {
a = rand() % (n - ) + ; //[1,n-1]
x = ksm(a, u, n);
for (int j = ; j < t; j++) {
y = mul(x, x, n);
if (y == && x != && x != n - ) return false;
x = y;
}
if (x != ) return false;
}
return true;
} ll Abs(ll x) {
if (x >= ) return x;
return -x;
} void rho(ll n) { //注意:不能处理1,否则会运行到对n-1=0取模
if (isprime(n)) {
for (int i = ; i <= top; i++) {
if (n == sta[i]) {
Mul[i] *= n;
return;
}
}
top++;
sta[top] = Mul[top] = n;
return;
}
ll x, y, z, c, d;
while (true) {
x = y = rand() * rand() % (n - ) + ;
c = rand() * rand() % (n - ) + ; // c!=0&&c!=-2
for (int i = , j = ;; i++) {
x = mul(x, x, n) - n + c;
if (x < ) x += n; // 64位数相加
d = gcd(Abs(x - y), n);
if (d > && d < n) {
rho(d);
rho(n / d);
return;
}
if (x == y) break;
if (i == j) y = x, j <<= ;
}
}
} int main() {
ll a, b;
while (scanf("%lld%lld", &a, &b) != EOF) {
top = ;
memset(sta, , sizeof(sta));
if (b / a == ) {
printf("%lld %lld\n", a, a);
continue;
}
rho(b / a);
ll p, q, res = , rp, rq;
for (int i = ; i < << top; i++) {
p = q = ;
for (int j = ; j < top; j++) {
if (i & ( << j))
p *= Mul[j + ];
else
q *= Mul[j + ];
}
if (p + q <= res || res == ) {
res = p + q;
rp = p;
rq = q;
}
}
if (rp > rq) swap(rp, rq);
printf("%lld %lld\n", rp * a, rq * a);
}
}

【poj 2429】GCD & LCM Inverse (Miller-Rabin素数测试和Pollard_Rho_因数分解)的更多相关文章

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

  2. [POJ 2429] GCD & LCM Inverse

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

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

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

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

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

  6. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  7. HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

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

  9. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

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

随机推荐

  1. PHP时区转换(默认中国时区<Asia/Shanghai>转意大利时区<Europe/Rome>)

    <?php function changeTimeZone($date_time, $format = 'Y-m-d H:i:s', $to = 'Europe/Rome', $from = ' ...

  2. typescript 02 数据类型

    ---恢复内容开始--- 1.数据类型 ts为了使代码更加规范并利于维护,增加了类型校验 提供了以下几种类型 布尔类型 boolean 数字类型 number 字符串类型 string 数组类型 ar ...

  3. day06可变与不可变类型,if判断,运算符

    1:可变不可变类型 2.什么是条件?什么可以当做条件?为何要要用条件? 显式布尔值:True.False 隐式布尔值:所有数据类型,其中0.None.空为假 3:逻辑运算符:用来 # not. and ...

  4. angular root在css和less的写法

    背景:angular7项目,angular cli创建了一个模板 新建了一个common.css 本身modle会有一个专属的less文件 写样式 父组件下的子组件样式修改 什么情况下用:root  ...

  5. Redis系列六 - 浅谈如何设计秒杀系统

    前言 设计一个系统之前,我们肯定要先确认系统业务场景是怎样的,下面就以某电商平台上的秒杀活动为场景,一起来探讨一个秒杀系统改如何去设计. 场景 我们现在要卖100件纸尿布,按照系统的用户量及以往经验来 ...

  6. Java并发编程(02):线程核心机制,基础概念扩展

    本文源码:GitHub·点这里 || GitEE·点这里 一.线程基本机制 1.概念描述 并发编程的特点是:可以将程序划分为多个分离且独立运行的任务,通过线程来驱动这些独立的任务执行,从而提升整体的效 ...

  7. Mathtype快捷键&小技巧

    Mathtype使用方便,能插入到Office等编辑器中,Latex公式在某些地方更加通用,如网页和书籍. 1. Mathtype简介 数学公式编辑器(MathType)是一款专业的数学公式编辑工具, ...

  8. 微信小程序接入LeanCloud

    大家在做小程序或者客户端开发的时候肯定会想使得数据进行联网,但这样就必须有对应的后台服务器以及数据库,再加上linux运维等各种细节,往往会对新手比较劝退,在这里给大家推荐一种bass(后端即服务), ...

  9. 学习 CSS 之用 CSS 3D 实现炫酷效果

    一.前言 把大象关进冰箱需要几步?三步,把冰箱门打开,把大象关进去,把冰箱门关上. 用 CSS 实现 3D 效果需几步?三步,设置透视效果 perspective,改变元素载体为 preserve-3 ...

  10. c# 导出excel的两种常见方法

    1,不是用第三方插件(html直接输出) StringBuilder ssb = new StringBuilder(); StringBuilder sb = new StringBuilder() ...