【poj 2429】GCD & LCM Inverse (Miller-Rabin素数测试和Pollard_Rho_因数分解)
本题涉及的算法个人无法完全理解,在此提供两个比较好的参考。
原理 (后来又看了一下,其实这篇文章问题还是有的……有时间再搜集一下资料)
#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_因数分解)的更多相关文章
- 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 ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- 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(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 ...
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
- HDU1164_Eddy'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 ...
- 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 ...
- POJ2429_GCD & LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...
随机推荐
- java8新特性——stream笔记
stream对象 Stream IntStream LongStream DoubleStream 创建 常用的三种方式: 使用list对象: list.stream() − 为集合创建串行流. li ...
- Failed to open the key database file. c;\\User\\w\\Destop\\SecureCRT_FX6.5.3\\Config\\KnowHosts\\Hostsmap.txt这个问题的解决方法
1.首先将这段错误在百度翻译上面查询一下,是什么意思,查询结果如下: 打开密钥数据库文件失败.C:\用户\ w \平台\ securecrt_fx6.5.3 \\ \\ \\ hostsmap.txt ...
- 【Java】机考常用知识
基本操作 数组 声明数组 方法一: int a[] = null; //声明一维数组 //int[] a = null; 也行,个人习惯 a = new int[10];//分配内存给一维数组 方法二 ...
- Yuchuan_Linux_C编程之九目录操作相关函数
一.整体大纲 二.相关函数 1. getcwd 函数作用:获取当前目录 头文件 #include <unistd.h> 函数原型 char *getcwd(char *buf, size_ ...
- Ubuntu系统下环境安装遇到依赖冲突问题
问题场景:在ubuntu系统下使用docker拉了一个python3.6的镜像,要在该容器中安装vim结果总是报已安装某些依赖的版本不满足要求 解决方法: 1.安装aptitude apt-get i ...
- js的立即执行函数
立即执行函数:常用于第三方库,好处在于隔离作用域,任何一个第三方库都会存在大量的变量和函数,为了避免变量污染(命名冲突),一般想到的方法就是使用立即执行函数.jQuery就是使用的立即执行函数. 函数 ...
- ASP.NET Core 快速入门(Razor Pages + Entity Framework Core)
引子 自从 2009 年开始在博客园写文章,这是目前我写的最长的一篇文章了. 前前后后,我总共花了 5 天的时间,每天超过 3 小时不间断写作和代码调试.总共有 8 篇文章,每篇 5~6 个小结,总截 ...
- Druid未授权(弱口令)的一些利用方式
Druid简介 1.Druid是阿里巴巴数据库事业部出品,为监控而生的数据库连接池. 2.Druid提供的监控功能,监控SQL的执行时间.监控Web URI的请求.Session监控. Druid可能 ...
- Django中ORM中的get与filter区别
本文出自 “orangleliu笔记本” 博客,出处http://blog.csdn.net/orangleliu/article/details/38597593 Django的orm中get和fi ...
- python3使用js2py
安装: pip install js2py 使用: 执行js函数: 执行js函数: import js2py js = js2py.EvalJs({}) js.execute("" ...