HDU 5974 A Simple math problem math problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.