http://acm.hdu.edu.cn/showproblem.php?pid=5974
I was really kneeling when I met a math problem.
Topic Requirements
X + Y = a
LCM (X, Y) = b
Set c = gcd (x, y);
Then we can express x and Y, which is x = i * C; y = J * C;
Where I and J are coprime.
So LCM (x, y) = i * J * C = b
Then we get two equations.
I * C + j * C = A;
I * J * C = b;
But there is a C, three unknowns.
Because I and J coprime, so (i + j) and I * j coprime.
Assuming that they are not coprime, then set that number to be X, yes, i + j = m * x; I * j = k * x;
Then there are both I and J can divide X, (by the first one can be obtained), which and I and J coprime contradiction.
GCD (A, b) = C,
Then you can solve a unary two-time equation. The minimum solution is to be output when there is a solution, because this ensures that no larger solution is taken and the other is negative.
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>LL A, b;voidWork () {LL C=__GCD (A, b); LL B= A/C; LL C= b/C; LL Diaota= b * B-4*C; LL T= INF/2; if(Diaota >=0) T=(LL) sqrt (Diaota); LL ans1= B +T; LL Ans2= B-T; if(Diaota <0|| T * t! = Diaota | | ((Ans1 &1) && (Ans2 &1)) {cout<<"No Solution"<<Endl; } Else{LL x, y; if(ans1%2==0&& Ans2%2==0) {//Priority Minimum SolutionLL tans =min (ans1, ans2); Tans/=2; cout<< Tans * C <<" "<< (B-tans) * C <<Endl; } Else if(Ans1 &1) {Ans2/=2; X= Ans2 *C; Y= (B-ANS2) *C; cout<< Ans2 * C <<" "<< (B-ANS2) * C <<Endl; } Else{ans1/=2; X= Ans1 *C; Y= (b-ans1) *C; cout<< ans1 * C <<" "<< (b-ans1) * C <<Endl; }//if (x + Y! = a) while (1); }}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endifIOS; while(Cin >> a >>b) Work (); return 0;}
View Code
HDU 5974 A Simple math problem math problem