-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnekocbad.cpp
85 lines (67 loc) · 1.78 KB
/
nekocbad.cpp
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
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function for finding all divisors
// of a given number
vector<int> getDivisors(int diff) {
// Stores the divisors of the
// number diff
vector<int> divisor;
for (int i = 1; i * i <= diff; i++) {
// If diff is a perfect square
if (i == diff / i) {
divisor.push_back(i);
continue;
}
// If i divides diff then
// diff / i also a divisor
if (diff % i == 0) {
divisor.push_back(i);
divisor.push_back(diff / i);
}
}
// Return the divisors stored
return divisor;
}
// Function to find smallest integer x
// such that LCM of (A + X) and (B + X)
// is minimized
int findTheSmallestX(int a, int b) {
int diff = b - a;
// Find all the divisors of diff
vector<int> divisor = getDivisors(diff);
// Find LCM of a and b
int lcm = (a * b) / __gcd(a, b);
int ans = 0;
for (int i = 0;
i < (int)divisor.size(); i++) {
// From equation x = M - a % M
// here M = divisor[i]
int x = (divisor[i] - (a % divisor[i]));
// If already checked for x == 0
if (!x)
continue;
// Find the product
int product = (b + x) * (a + x);
// Find the GCD
int tempGCD = __gcd(a + x, b + x);
int tempLCM = product / tempGCD;
// If current lcm is minimum
// than previous lcm, update ans
if (lcm > tempLCM) {
ans = x;
lcm = tempLCM;
}
}
// Print the number added
cout << ans << endl;
}
// Driver Code
int main() {
// Given A & B
int A, B;
cin >> A >> B;
// Function Call
findTheSmallestX(A, B);
return 0;
}