【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 ...
随机推荐
- 7-46 jmu-python-求单词长度 (10 分)
输入n个单词,计算每个单词长度.对单词长度排序,分行输出单词长度及其单词. 输入格式: 行1:单词个数n 分行输入n个单词 输出格式: 分行输出单词长度及其单词.(单词长度,单词)用元组表示 输入样例 ...
- UBB代码
UBB代码是HTML(标准通用标记语言下的一个应用)的一个变种,是Ultimate Bulletin Board (国外的一个BBS程序)采用的一种特殊的TAG.您也许已经对它很熟悉了.UBB代码很简 ...
- IIS6.0文件解析漏洞和短文件名漏洞复现
一.IIS6.0文件解析漏洞 1.ASP一句话木马的准备 新建木马文件“muma.txt”,将“我asp是一句话木马:<%eval request("asp")%>”写 ...
- Vue项目二、vue环境搭建以及Vue-cli使用及详解
一.Vue多页面应用的环境搭建 每一次页面跳转的时候,后台服务器都会给返回一个新的html文档,这种类型的网站也就是多页网站,也叫做多页应用. 环境的搭建如下,在页面中引入如下框架 <scrip ...
- redis环境搭建及一主二从三哨兵模式配置
一.单机redis环境搭建 1.安装: OS:linux redhat6.5 下载redis 官网下载链接:https://redis.io/download 把安装包上传到服务器,进行解压 [roo ...
- go源码分析(一) 通过调试看go程序初始化过程
参考资料:Go 1.5 源码剖析 (书签版).pdf 编写go语言test.go package main import ( "fmt" ) func main(){ fmt.Pr ...
- 单选框 改成 复选框 的css样式
fillEditorFakeTable.less /* add for the global title checkbox fake */ .fake-checkbox { display: inli ...
- css定位属性的运用
position 定位定位:主要解决叠加排列的问题.position 1.static(默认) 2.relative : 相对定位 如果没有定位偏移量,对元素本身没有任何影响(一般用于需要加定位的父容 ...
- mysql8 修改root密码
Navicat工具里选中mysql数据库 执行: ALTER user 'root'@'localhost' IDENTIFIED BY 'newpassward'; //newpassward 新密 ...
- 强连通分量SCC 2-SAT
强连通分量SCC 2-SAT 部分资料来自: 1.https://blog.csdn.net/whereisherofrom/article/details/79417926 2.https://ba ...