POJ2429 GCD & LCM Inverse

题目地址

题解

找质因子用Miller-Rabin和Pollard-Rho实现,然后求解的时候考虑对勾函数,当$a$和$b$越接近$\sqrt{ab}$时$a+b$越小。就做完了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 2000000000
using namespace std;
typedef long long ll;
ll g, l, prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
ll dd[55], ans = -1;
int e1[55] = {0}, e2[55] = {0}, cnt = 0;
double lev, dis;
ll gcd(ll a, ll b){
return (!b) ? a : gcd(b, a % b);
}
ll mul(ll a, ll b, ll M){
ll res = 0;
while(b){
if(b & 1ll) {
res += a;
if(res >= M) res -= M;
}
a <<= 1, b >>= 1;
if(a >= M) a -= M;
}
return res;
}
ll modpow(ll a, ll b, ll M){
a %= M;
ll res = 1;
while(b){
if(b & 1ll) res = mul(res, a, M);
a = mul(a, a, M), b >>= 1;
}
return res;
}
bool witness(ll a, ll n, ll t, ll u){
ll x = modpow(a, u, n);
for(ll i = 1; i <= t; ++i){
ll xx = mul(x, x, n);
if(xx == 1 && x != 1 && x != n - 1)
return true;
x = xx;
}
if(x != 1) return true;
return false;
}
bool miller_rabin(ll n){
for(int i = 0; i < 10; ++i){
if(n == prime[i]) return true;
else if(n % prime[i] == 0) return false;
}
ll t, u;
for(t = 1; ; ++t)
if((n - 1) % (1 << t) == 0) break;
u = (n - 1) / (1 << t);
for(int i = 0; i < 10; ++i)
if(witness(rand() % (n - 1) + 1, n, t, u))
return false;
return true;
}
ll rho(ll n, ll c){
ll x = modpow(rand(), rand(), n), y = x, d = 1;
int k = 2;
for(int i = 1; d == 1; ++i){
x = mul(x, x, n) + c;
if(x >= n) x -= n;
if(x > y) d = gcd(x - y, n);
else d = gcd(y - x, n);
if(i == k) y = x, k <<= 1;
}
return d;
}
ll Pollard(ll n){
ll d = n;
while(d == n)
d = rho(n, rand() % (n - 1) + 1);
return d;
}
void addFac(ll &x, ll d, int *e){
int cur;
for(cur = 0; cur < cnt; ++cur)
if(dd[cur] == d) break;
dd[cur] = d;
do{
x /= d, e[cur]++;
}while(x % d == 0);
if(cur == cnt) cnt++;
}
void getFac(ll x, int *e){
if(!miller_rabin(x)){
ll d = Pollard(x);
getFac(d, e), getFac(x / d, e);
}else{
addFac(x, x, e);
}
}
void dfs(int ind, ll x){
if(ind == cnt){
if(1.0 * x > lev && ans > x)
ans = x;
return ;
}
ll res = 1;
for(int i = 0; i < e2[ind]; ++i) res *= dd[ind];
dfs(ind + 1, x * res);
for(int i = e2[ind]; i < e1[ind]; ++i) res *= dd[ind];
dfs(ind + 1, x * res);
}
void init(){
srand(122144);
}
void solve(){
cnt = 0;
memset(dd, 0, sizeof(dd));
memset(e1, 0, sizeof(e1));
memset(e2, 0, sizeof(e2));
if(g == l) cout << g << " " << l << endl;
else{
getFac(l, e1);
if(g != 1) getFac(g, e2);
lev = sqrt(1.0 * l * g);
ans = l, dfs(0, 1);
cout << (l / ans) * g << " " << ans << endl;
}
}
int main(){
while(scanf("%lld %lld", &g, &l) == 2){
init();
solve();
}
return 0;
}