Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b.

Input

The input contains multiple test cases, each of which contains two positive integers, the GCD and the LCM. You can assume that these two numbers are both less than 2^63.

Output

For each test case, output a and b in ascending order. If there are multiple solutions, output the pair with smallest a + b.

Sample Input

3 60

Sample Output

12 15

题意:给出两个数的最大公约数和最小公倍数,让你找出满足条件的两个数,使他们的和最小。

题解:

对于两个数a,b和他们的最大公约数gcd以及最小公倍数lcm,有lcm=a*b/gcd,进一步变形可以得到:(a/gcd * b/gcd)*gcd=lcm,即(a/gcd * b/gcd)=lcm/gcd,我们令m=a/gcd,n=b/gcd,那么问题就变为了:找出两个互素的整数,使他们的乘积为key=lcm/gcd。我们可以用Pollard_rho算法进行质因素分解的方法找出lcm/gcd的所有素因子,然后dfs找出任意几个素数因子之积(设为x),设y=key/x,找出最小的x+y就行了

代码:

  1 #include <cstdio>
2
3 #include <iostream>
4
5 #include <cstdlib>
6
7 #include <cmath>
8
9 #include <algorithm>
10
11
12
13 #define times 10
14
15 #define N 501
16
17 using namespace std;
18
19 typedef unsigned long long LL;
20
21 const LL INF=(LL)1<<61;
22
23 LL key,ct,cnt,gd,lm,resa,resb,mini;
24
25 LL fac[N],num[N];
26
27
28
29 LL gcd(LL a,LL b)
30
31 {
32
33 return b?gcd(b,a%b):a;
34
35 }
36
37
38
39 LL multi(LL a,LL b,LL m)
40
41 {
42
43 LL ans=0;
44
45 a%=m;
46
47 while(b)
48
49 {
50
51 if(b&1)
52
53 {
54
55 ans=(ans+a)%m;
56
57 b--;
58
59 }
60
61 b>>=1;
62
63 a=(a+a)%m;
64
65 }
66
67 return ans;
68
69 }
70
71
72
73 LL quick_mod(LL a,LL b,LL m)
74
75 {
76
77 LL ans=1;
78
79 a%=m;
80
81 while(b)
82
83 {
84
85 if(b&1)
86
87 {
88
89 ans=multi(ans,a,m);
90
91 b--;
92
93 }
94
95 b>>=1;
96
97 a=multi(a,a,m);
98
99 }
100
101 return ans;
102
103 }
104
105
106
107 bool Miller_Rabin(LL n)
108
109 {
110
111 if(n==2) return true;
112
113 if(n<2||!(n&1)) return false;
114
115 LL m=n-1;
116
117 int k=0;
118
119 while(!(m&1))
120
121 {
122
123 k++;
124
125 m>>=1;
126
127 }
128
129 for(int i=0;i<times;i++)
130
131 {
132
133 LL a=rand()%(n-1)+1;
134
135 LL x=quick_mod(a,m,n);
136
137 LL y=0;
138
139 for(int j=0;j<k;j++)
140
141 {
142
143 y=multi(x,x,n);
144
145 if(y==1&&x!=1&&x!=n-1) return false;
146
147 x=y;
148
149 }
150
151 if(y!=1) return false;
152
153 }
154
155 return true;
156
157 }
158
159
160
161 LL Pollard_rho(LL n,LL c)
162
163 {
164
165 LL i=1,k=2;
166
167 LL x=rand()%(n-1)+1;
168
169 LL y=x;
170
171 while(true)
172
173 {
174
175 i++;
176
177 x=(multi(x,x,n)+c)%n;
178
179 LL d=gcd((y-x+n)%n,n);
180
181 if(1<d&&d<n) return d;
182
183 if(y==x) return n;
184
185 if(i==k)
186
187 {
188
189 y=x;
190
191 k<<=1;
192
193 }
194
195 }
196
197 }
198
199
200
201 void Find(LL n,LL c)
202
203 {
204
205 if(n==1) return ;
206
207 if(Miller_Rabin(n))
208
209 {
210
211 fac[ct++]=n;
212
213 return ;
214
215 }
216
217 LL p=n;
218
219 LL k=c;
220
221 while(p>=n) p=Pollard_rho(p,c--);
222
223 Find(p,k);
224
225 Find(n/p,k);
226
227 }
228
229
230
231 void dfs(LL dept,LL product)
232
233 {//dept为递归深度,product为其中一个因子
234
235 if(dept==cnt)
236
237 {
238
239 LL a=product;
240
241 LL b=key/a;
242
243 if(gcd(a,b)==1)
244
245 {
246
247 a*=gd;
248
249 b*=gd;
250
251 if(a+b<mini)
252
253 {
254
255 mini=a+b;
256
257 resa=a;
258
259 resb=b;
260
261 }
262
263 }
264
265 return ;
266
267 }
268
269 for(int i=0;i<=num[dept];i++)
270
271 {
272
273 if(product>mini) return ;
274
275 dfs(dept+1,product);
276
277 product*=fac[dept];
278
279 }
280
281 }
282
283
284
285
286
287 void Solve(LL n)
288
289 {
290
291 ct=0;
292
293 Find(n,120);
294
295 sort(fac,fac+ct);
296
297 num[0]=1;
298
299 int k=1;
300
301 for(int i=1;i<ct;i++)
302
303 {
304
305 if(fac[i]==fac[i-1])
306
307 num[k-1]++;
308
309 else
310
311 {
312
313 num[k]=1;
314
315 fac[k++]=fac[i];
316
317 }
318
319 }
320
321 cnt=k;
322
323 dfs(0,1);
324
325 if(resa>resb) swap(resa,resb);
326
327 }
328
329
330
331 int main()
332
333 {
334
335 while(cin>>gd>>lm)
336
337 {
338
339 if(gd==lm)
340
341 {
342
343 printf("%llu %llu\n",gd,lm);
344
345 continue;
346
347 }
348
349 mini=INF;
350
351 key=lm/gd;
352
353 Solve(key);
354
355 printf("%llu %llu\n",resa,resb);
356
357 }
358
359 return 0;
360
361 }

POJ2429 GCD & LCM Inverse pollard_rho大整数分解的更多相关文章

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

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

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

  3. poj2429 GCD & LCM Inverse

    用miller_rabin 和 pollard_rho对大数因式分解,再用dfs寻找答案即可. http://poj.org/problem?id=2429 #include <cstdio&g ...

  4. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  5. [POJ 2429] GCD & LCM Inverse

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

  6. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

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

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

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

  9. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

随机推荐

  1. paramunittest参数化测试基础

    samples: import paramunittestimport unittest@paramunittest.parametrized( (10,20), (30,40), # (100,20 ...

  2. Java并发包源码学习系列:挂起与唤醒线程LockSupport工具类

    目录 LockSupport概述 park与unpark相关方法 中断演示 blocker的作用 测试无blocker 测试带blocker JDK提供的demo 总结 参考阅读 系列传送门: Jav ...

  3. WEB开发框架性能排行与趋势分析2-三大惊喜变化

    WEB开发框架性能排行与趋势分析2-三大惊喜变化 Web框架性能排名 上一次基于TechEmpower的<Web Framework Benchmarks>性能基准测试的解读之后,时隔两年 ...

  4. kubernets之configMap和secret

    一  如何有效且更好的将配置写到pod的容器中 考虑一个问题,就是在传统的应用中,程序里面需要的配置一般以配置文件的形式或者shell脚本里面的参数是在执行的时候在命令行里面进行添加,但是在kuber ...

  5. 快速查询表中的NULL数据

    正常情况下,NULL值是不会放入B-TREE索引的,因此根据IS NULL查询的时候走的通常是全表扫描,如果记录比较少还好,记录比较多,查询会非常耗时 可以通过创建一个索引来解决 CREATE IND ...

  6. 力软最新版本与.netCore版本

    功能强大,直接上图: 加微信或QQ交流开发技术:25489181 netcore版本 版本优势: .NET Core是适用于 Windows.Linux 和 macOS 的免费.开源托管的计算机软件框 ...

  7. vagrant up报错【io.rb:32:in `encode': "\x95" followed by "\"" on GBK (Encoding::InvalidByteSequenceError)】

    vagrant up报错[io.rb:32:in `encode': "\x95" followed by """ on GBK (Encoding: ...

  8. DOI技术扫盲一

    DOI:  desktop office intergration   桌面办公软件集成简单的将,就是我们在Windows桌面中打开的办公软件(如:word,excel,pdf等等)可以在SAP系统进 ...

  9. 创建Django REST framework工程

    1.创建工程虚拟环境 2.创建工程目录和调整目录结构: 创建Django的项目 创建docs 用于存放一些说明文档资料 创建scripts 用于存放管理脚本文件 创建logs 用于存在日志 在与项目同 ...

  10. 原生ajax分享

    最近被大佬问了一个很有趣的问题,你还能手打出一个ajax吗?,我当时的想法是有现成的为什么要自己打,后来我反思了一下(只有靠自己才是强者),在这里给大家分享一个我自己打的ajax,也是自己的一个知识点 ...