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

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

代码实现

#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. CF 1305E. Kuroni and the Score Distribution

    题目大意:题目给定两个数n和m(1<=n<=5000,0<=m<=1e9)要求构造一个数列A,A中元素 大于等于1,小于等于1e9且满足严格递增 满足ai+aj=ak的(i,j ...

  2. PHPRAP v1.0.6 发布,修复因php7.1版本遗弃mcrypt扩展造成安装失败的BUG

    PHPRAP,是一个PHP轻量级开源API接口文档管理系统,致力于减少前后端沟通成本,提高团队协作开发效率,打造PHP版的RAP. 更新记录 [修复]修复因php7.1版本遗弃mcrypt扩展造成安装 ...

  3. 前端每日实战:48# 视频演示如何用纯 CSS 创作一盘传统蚊香

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/BVpvMz 可交互视频教程 此视频 ...

  4. leetcode 209 3 滑动窗口

    class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { ,r=-; //由于数组是[]区间, ...

  5. 自定义FrameWork

    本项目是基于iOS-Universal-Framework-master框架制作的,故编译之前需要安装iOS-Universal-Framework-master框架, 步骤如下:1.跳转到iOS-U ...

  6. 06 Linux 的常用命令

    Linux 刚面世时并没有图形界面,所有的操作全靠命令完成,如 磁盘操作.文件存取.目录操作.进程管理.文件权限 设定等 在职场中,大量的 服务器维护工作 都是在 远程 通过 SSH 客户端 来完成的 ...

  7. 使用twisted将mysql插入变成异步执行

    python 异步MySQL存库   对于异步框架而言,这些延迟是无法接受的.因此, Twisted 提供了 twisted.enterprise.adbapi, 遵循DB-API 2.0协议的一个异 ...

  8. Python模块三

    collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...

  9. 在ES批量插入数据超时时自动重试

    当我们使用ES批量插入数据的时候,一般会这样写代码: from elasticsearch import Elasticsearch,helpers es =Elasticsearch(hosts=[ ...

  10. 可运行jar包的几种打包/部署方式(转)

    转自:https://www.cnblogs.com/yjmyzz/p/executable-jar.html java项目开发中,最终生成的jar,大概可分为二类,一类是一些通用的工具类(不包含ma ...