• the meaning of problems
Given two positive integers a, b;
Solving k, such that LCM (a + k, b + k) a minimum, if there is a plurality of k such that the LCM () minimum, the minimum output of the k;
• ideas
After a lapse of a long time, again do this problem
Sure enough, one can know the new ponder ❤
Important points
GCD(a,b)=GCD(a,b-a)=GCD(b,b-a) (b>a)
prove:
Provided GCD (a, b) = c
Is a% c = 0, b% c = 0, (ba)% c = 0
Therefore GCD (a, ba) = c
得GCD(a,b)=GCD(a,b-a)
A thought:
gcd (a + k, ba) must be (ba) Factor
Therefore, gcd (a + k, b + k) is (ba) a factor, so we enumerate (ba) a factor (the factor referred i)
Such that (a + k) i is a multiple of
Solutions of the k, and then determines whether or not meet the minimum lcm
Note that only enumerated i (a + k) and a divisor of (b + k) is not necessarily the greatest common divisor gcd
Both divisor obtained common multiple common multiple = a * b / divisor
If it is a common multiple of the greatest common divisor of both must be minimized,
Here there is no discrimination whether it is the common denominator but simply get the number of convention, then get is a common multiple
In all common multiple of the least common multiple of the smallest
It does not affect the answer to the solution of the least common multiple
E.g:
12 30
12 is 30
A + K = 13 is B + K = 31 is divisor i = 1 common multiples = 403 K =. 1
A + K = 18 is B + K = 36 divisor i = 18 common multiples = 36 K =. 6
A + K = 14 B + k = 32 divisor i = 2 common multiple = 224 K = 2
A + K = 18 is B + K = 36 divisor i =. 9 common multiples = 72 K =. 6
A + K = 15 B + K = 33 is divisor i = common multiple of K 3 = 165 3 =
A + B + 18 is K = 36 K = i = 6 well divisor multiples = 108 k = 636 minimum number of convention, when k = 6
• Code
View Code1 //#include<bits/stdc++.h> 2 //using namespace std; 3 //#define ll long long 4 //#define inf 0x3f3f3f3f 5 //ll ans=inf; 6 //ll a,b; 7 //ll lcm; 8 // 9 //ll __lcm(ll x,ll y) 10 //{ 11 // return x*y/__gcd(x,y); 12 //} 13 // 14 // 15 //void Slove(int f) 16 //{ 17 // int x=a/f+(a%f!=0); 18 // int y=(b-a)/f; 19 // if(__gcd(x,y)==1) 20 // { 21 // if(__lcm(x*f,y*f)<lcm) 22 // { 23 // lcm=__lcm(x*f,y*f); 24 // ans=x*f-a; 25 // } 26 // if(__lcm(x*f,y*f)==lcm) 27 // ans=min(ans,x*f-a); 28 // } 29 // else 30 // { 31 // if(__lcm((x+1)*f,y*f)<lcm) 32 // { 33 // lcm=__lcm((x+1)*f,y*f); 34 // ans=(x+1)*f-a; 35 // } 36 // if(__lcm((x+1)*f,y*f)==lcm) 37 // ans=min(ans,(x+1)*f-a); 38 // } 39 //} 40 // 41 //int main() 42 //{ 43 // cin>>a>>b; 44 // if(b<a) 45 // swap(a,b); 46 // lcm=a*b/__gcd(a,b); 47 // 48 // if(a==b) 49 // ans=0; 50 // else 51 // { 52 // for(ll i=1;i*i<=b-a;i++) 53 // { 54 // if((b-a)%i==0) 55 // { 56 // Slove(i); 57 // Slove((b-a)/i); 58 // } 59 // } 60 // } 61 // cout<<ans<<endl; 62 //} 63 64 65 #include<bits/stdc++.h> 66 using namespace std; 67 #define ll long long 68 ll a,b; 69 ll ans,lcm=0x3f3f3f3f3f3f3f3f; 70 int main() 71 { 72 cin>>a>>b; 73 ll d=abs(a-b); 74 for(ll i=1;i*i<=d;i++) 75 { 76 IF (D% i == 0 ) // factor ba enumeration of i 77 { 78 LL% IA = K i; // to make up a multiple of K i needs + 79 LL T = (K + a) * ( B + K) / I; // A * B / I have a common multiple of 80 IF (T < LCM) 81 { 82 LCM = T; 83 ANS = K; 84 } 85 86 LL II = D / I; 87 K = II % -a II; 88 T = (K + A) * (B + K) / II; 89 IF(t<lcm) 90 { 91 lcm=t; 92 ans=k; 93 } 94 } 95 } 96 cout<<ans<<endl; 97 }