poj2429 GCD & LCM Inverse
用miller_rabin 和 pollard_rho对大数因式分解,再用dfs寻找答案即可。
http://poj.org/problem?id=2429
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef __int64 LL;
const int maxn = ;
const int times = ;
LL prime[maxn], k;
int cnt[maxn];
LL c, d, pm, mid; void dfs(int pos, LL t){
if(pos == k){
if(pm < t) pm = t;
return;
}
LL tem = ;
int c = cnt[pos];
while(c--) tem *= prime[pos];
dfs(pos + , t);
if(t * tem <= mid) dfs(pos + , t * tem);
} LL random(LL n){
return (double)rand() / RAND_MAX * n + 0.5;
} LL multi(LL a, LL b, LL mod){
a %= mod, b %= mod;
LL ans = ;
while(b){
if(b & ) ans += a, ans %= mod;
b >>= ;
a <<= ;
a %= mod;
}
return ans;
} LL power(LL a, LL p, LL mod){
a %= mod;
LL ans = ;
while(p){
if(p & ) ans = multi(ans, a, mod), ans %= mod;
p >>= ;
a = multi(a, a, mod);
a %= mod;
}
return ans;
} LL gcd(LL a, LL b){
if(!b) return a;
return gcd(b, a % b);
} bool witness(LL a, LL n){
LL u = n - ;
while(!(u & )) u >>= ;
LL t = power(a, u, n);
while(u != n - && t != && t != n - ){
t = multi(t, t, n);
u <<= ;
}
return t == n - || u & ;
} bool miller_rabin(LL n){
if(n == ) return ;
if(n < || !(n & )) return ;
for(int i = ; i < times; i++){
LL p = random(n - ) + ;
if(!witness(p, n)) return ;
}
return ;
} LL pollard_rho(LL n, LL t){
LL x = random(n - ) + ;
LL y = x, i = , k = , d;
while(){
++i;
x = (multi(x, x, n) + t) % n;
d = gcd(y - x, n);
if( < d && d < n) return d;
if(x == y) return n;
if(i == k){
y = x;
k <<= ;
}
}
} void solve(){
LL m = d / c;
LL m1 = m;
mid = (LL)sqrt(m);
k = ;
memset(cnt, , sizeof cnt);
if(m % == ){
prime[k++] = ;
while(m % == ) m >>= , ++cnt[k - ];
}
while(!miller_rabin(m) && m > ){
int seed = ;
LL p1 = m;
while(p1 >= m || !miller_rabin(p1)) p1 = pollard_rho(m, seed--);
prime[k++] = p1;
while(m % p1 == ) m /= p1, ++cnt[k - ];
}
if(m != ) prime[k++] = m, ++cnt[k - ];
pm = ;
dfs(, );
LL qm = m1 / pm;
printf("%I64d %I64d\n", pm * c, qm * c);
} int main(){
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(~scanf("%I64d%I64d", &c, &d)) solve();
return ;
}
poj2429 GCD & LCM Inverse的更多相关文章
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- 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 这 ...
- 【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse
题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B. n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的. 于是对n/m质因子分解后 ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- Mathematics:GCD & LCM Inverse(POJ 2429)
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...
- 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 ...
- 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---给出gcd和lcm求原来两个数
题目链接: https://cn.vjudge.net/problem/POJ-2429 题目大意: 给出两个数的gcd和lcm,求原来的这两个数(限定两数之和最小). 解题思路: 首先,知道gcd和 ...
- 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 ...
随机推荐
- Java基础之线程——管理线程同步方法(BankOperation2)
控制台程序. 当两个或多个线程共享同一资源时,例如文件或内存块,就需要采取措施,确保其中的一个线程不会修改另一个线程正在使用的资源.当其中的一个线程更新文件中的某个记录,同时另一个线程正在检索这个记录 ...
- Idea 安装 lombok
idea 目前是Java开发者最流行的一款编辑器.为了让java开发更加的简便idea 也提供了lombok的插件. 插件的按钮方式为: 1.进入idea的.setting面板 2.按照以下图进行操作 ...
- R语言putty中直接使用X11(Xming)绘图
1.下载Xming地址 http://pan.baidu.com/s/1o6ilisU,安装,推荐默认安装在C盘,推荐快捷方式放在与putty快捷方式同一个文件夹: 2.打开putty,在SSH的X1 ...
- java io读书笔记(8)FileInputStream/FileOutputStream的应用
转自:http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html 这是一对继承于InputStream和OutputStream的类,用 ...
- Winform 控件
※控件在视图工具箱里面找,找到之后双击即可添加进来,也可以点住拖进来 ※每个工具,控件,窗体都有一个name,相当于id,用来标识该对象的名称,name值不允许重复 控件: 1.Label -- 文本 ...
- each实现原理
<script> function isEach(arr, callback) { for (var i in arr) { callback(i, arr[i]); } }; funct ...
- url组成部分
协议 eg:http 主机IP地址 eg :端口号8080 项目资源地址 eg:目录名,文件夹名
- ACdream 1104 瑶瑶想找回文串(SplayTree + Hash + 二分)
Problem Description 刚学完后缀数组求回文串的瑶瑶(tsyao)想到了另一个问题:如果能够对字符串做一些修改,怎么在每次询问时知道以某个字符为中心的最长回文串长度呢?因为瑶瑶整天只知 ...
- (第九周)视频发布及git统计报告
项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 代码地址:HTTPS: https://git.coding.net/li_yuhuan/FoodChain. ...
- OpenStack 计算节点删除
前提 计算节点中一个僵尸计算节点存在,而里面的CPU数目在总物理CPU中,导致认为当前能创建实例.而实际没有这么多资源. 其中node-11为僵尸节点. 原因 删除计算节点不能直接格式化该服务器,否则 ...