用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的更多相关文章

  1. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  2. 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 这 ...

  3. 【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse

    题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B. n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的. 于是对n/m质因子分解后 ...

  4. [POJ 2429] GCD & LCM Inverse

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: ...

  5. Mathematics:GCD & LCM Inverse(POJ 2429)

    根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...

  6. 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 ...

  7. POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)

    [题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...

  8. POJ-2429 GCD & LCM Inverse---给出gcd和lcm求原来两个数

    题目链接: https://cn.vjudge.net/problem/POJ-2429 题目大意: 给出两个数的gcd和lcm,求原来的这两个数(限定两数之和最小). 解题思路: 首先,知道gcd和 ...

  9. 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 ...

随机推荐

  1. iOS6:在你的企业系统中支持Passbook

    前言 这是一篇翻译,感谢Jonathan Tang. 原文地址:iOS 6 Tutorial: Supporting Passbook within Your Enterprise Systems 正 ...

  2. iOS中model出来一个控制器的尺寸怎么设置?

    在xib的控制器里添加self.preferredContentSize = CGSizeMake( , ) 就能修改xib在界面上显示的大小- (void)viewDidLoad { [super ...

  3. iOS中scrollview是否要回弹

    1. @property(nonatomic) BOOL bounces //当滚动到内容边缘是否发生反弹,default is YES.2. @property(nonatomic) BOOL al ...

  4. 数据库迁移之从oracle 到 MySQL

    方式一: 手动方式导入导出 手动的方式导入, 就是操作步骤会比较繁琐一些. 对Table 的结构和数据: 1. 使用 SQL Developer 把 oracle 的 table 的schema 和 ...

  5. Ubuntu 16.04 LTS Final Beta

    期待已久的Ubuntu  LTS 版本开放了公测版本 Ubuntu 16.04 (Xenial Xerus) Daily Build(http://cdimage.ubuntu.com/daily-l ...

  6. 如何在OneNote2013中粘贴高亮的代码

    有的时候想在OneNote粘贴代码,但是直接复制粘贴进去的代码没有高亮,下面有一个办法让自己的代码在OneNote里面更加完整美观. 工具/原料 Notepad++ word2013 OneNote2 ...

  7. 代码实现sql数据库的附加(通常在安装的时候)

    判断数据库是否已经存在 SqlConnection judgeConn = new SqlConnection("server=.;database=master;uid="+us ...

  8. Rest服务

    资源:是网络上的一个实体,或者是网络上的一个具体信息,每一个资源对应一个特定的URI(统一资源定位符),要访问该资源,访问它的URI就可以了. 表现层:把资源的具体形式表现出来. 状态转化:每发出一个 ...

  9. HDU 1724 Ellipse(数值积分の辛普森公式)

    Problem Description Math is important!! Many students failed in 2+2’s mathematical test, so let's AC ...

  10. 夺命雷公狗---DEDECMS----4快速入门之栏目页报错之快速解决

    我们dedecms是分3级或者3层的,如下图所示: 点击“玄幻小说”后发现出了错,如下图所示: 我们的dedecms是分3层的,他们分别是: 网站首页 网站的栏目页面(多级)--->栏目页面是需 ...