-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11414.cpp
More file actions
59 lines (47 loc) · 1.12 KB
/
11414.cpp
File metadata and controls
59 lines (47 loc) · 1.12 KB
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
#include <iostream>
#include <vector>
using namespace std;
int min (int n1, int n2) {
return n1 < n2 ? n1 : n2;
}
long long int lcm (int n1, int n2) {
int temp1 = n1, temp2 = n2;
while (temp1 > 0 && temp2 > 0) {
if (temp1 > temp2) temp1 %= temp2;
else temp2 %= temp1;
}
int gcd;
if (temp1 > 0) gcd = temp1;
else gcd = temp2;
return (long long int)n1 * (long long int)n2 / (long long int)gcd;
}
int A, B;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> A >> B;
if (A == B) {
cout << "1";
return 0;
}
vector<int> divisor;
int B_A = abs(B - A);
for (int i = 1; i * i <= B_A; i++) {
if (B_A % i == 0) {
divisor.push_back(i);
if (i * i != B_A) divisor.push_back(B_A / i);
}
}
long long int minLCM = 4 * 1e18 + 1;
int N = 1e9 + 1;
for (int d : divisor) {
int tempN = d - A % d;
long long int tempLCM = lcm(A + tempN, B + tempN);
if (minLCM > tempLCM) {
minLCM = tempLCM;
N = tempN;
} else if (minLCM == tempLCM) N = min(tempN, N);
}
cout << N;
return 0;
}