Problem Description
Given positive integers A and b,find suitable X and Y to meet the conditions:
X+y=a
Least Common Multiple (X, Y) =b
Inputinput includes multiple sets of test data. Each test data occupies one line,including, positive integers a (1≤a≤2*10^4), B (1≤b≤10^9), and their meanings is shown in The description. Contains most of the 12W test cases.
Outputfor each set of input data,output a line of the integers,representing x Y.if you cannot find such x and Y,output on E Line of "No solution" (without quotation) . test instructions: give you two number a, if you can find two numbers x, y to make x+y=a and X, y least common multiple to B. Because the topic gives the number of data groups, more than 12w groups so the traversal will time out, so try to find a way to evaluate directly. According to test instructions, two formulas can be obtained: (1) x+y=a; (2) X*y=b*k; (k is x, y greatest common divisor) obviously there is a k here is not good for the answer, so try to get rid of K. Will (1) all except K to get (3) x/k+y/k=a/k, and then (2) all except K to get (4) (x/k) * (y/k) =b/k; because k is x, y greatest common divisor, x/k, and y/k coprime. So a/k, and b/k is also coprime. So the problem is simplified to I+J=A/GCD (A, B), I*J=B/GCD (A, B), and the solutions of I and J.
#include <iostream> #include <cmath>using namespace Std;int X, y;int gcd (int a, int b) {return b > 0 gcd (b, a% B): A;} int cal (int a, int b, int c) {if (A * a-4 * b < 0) return 0; int GG = A * a-4 * b; int FFF = sqrt (GG); if (GG! = FFF * FFF) {Y =-1, X =-1; return 0; } X = (A + FFF); Y = (A-FFF); if (x% 2 = = 0 && Y% 2 = = 0) {X/= 2; Y/= 2; return 1; } else {X =-1; Y =-1; return 0; }}int Main () {int A, B; while (Cin >> a >> b) {int c = gcd (A, b); X =-1, Y =-1; if (Cal (A/C, b/c, c) && X! =-1 && Y! =-1) {cout << min (X, Y) * C << ' &L t;< Max (X, Y) * C << Endl; } else {cout << "No solution" << Endl; }} return 0;}
Hdu 5974 A Simple math problem (math problem)