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

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

代码实现

#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. 利用wps创建有目录的PDF/word

    为什么要创建: 在阅读一些行业规范或者很长的文件,像是项目管理方案时,非常麻烦,定位需要重新返回目录去.--->所以我想能不能创建一个带目录的PDF,可以点击直接跳转,那就方便多了. 如何创建: ...

  2. swoole模块的编译安装:php编译安装swoole模块的代码

    本篇文章给大家带来的内容是关于swoole模块的编译安装:php编译安装swoole模块的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.下载swoole 1 wget ht ...

  3. scrapy 在爬取过程中抓取下载图片

    先说前提,我不推荐在sarapy爬取过程中使用scrapy自带的 ImagesPipeline 进行下载,是在是太耗时间了 最好是保存,在使用其他方法下载 我这个是在 https://blog.csd ...

  4. 机器学习实用案例解析(1) 使用R语言

    简介 统计学一直在研究如何从数据中得到可解释的东西,而机器学习则关注如何将数据变成一些实用的东西.对两者做出如下对比更有助于理解“机器学习”这个术语:机器学习研究的内容是教给计算机一些知识,再让计算机 ...

  5. MATLAB神经网络(1) BP神经网络的数据分类——语音特征信号分类

    1.1 案例背景 1.1.1 BP神经网络概述 BP神经网络是一种多层前馈神经网络,该网络的主要特点是信号前向传递,误差反向传播.在前向传递中,输入信号从输入层经隐含层逐层处理,直至输出层.每一层的神 ...

  6. Gorm 预加载及输出处理(二)- 查询输出处理

    上一篇<Gorm 预加载及输出处理(一)- 预加载应用>中留下的三个问题: 如何自定义输出结构,只输出指定字段? 如何自定义字段名,并去掉空值字段? 如何自定义时间格式? 这一篇先解决前两 ...

  7. [BUG]微信小程序生成小程序码"小程序页面路径不存在,请重新输入"

    描述 小程序页面线上能打开. 微信官方 获取小程序页面小程序码 页面 ,输入 小程序页面路径,提示 "小程序页面路径不存在,请重新输入". 使用微信复制小程序路径方法, 也是同样的 ...

  8. 使用Filter来过滤掉需要排除的数组对象

    问题描述:有个地方需要根据判断是否是总分公司来控制转正入口的显影,list是获取到的所有入口的数组,需要判断数组里哪个对象的title为"员工转正",本来打算用for循环的,之后发 ...

  9. JAVA 转换 树结构数据

    JAVA 转换 树结构数据 第一步:引入fastjson <dependency> <groupId>com.alibaba</groupId> <artif ...

  10. Journal of Proteome Research | Clinically Applicable Deep Learning Algorithm Using Quantitative Proteomic Data (分享人:翁海玉)

    题目:Clinically Applicable Deep Learning Algorithm Using Quantitative Proteomic Data 期刊:Journal of Pro ...