2016ICPC-大连 A Simple Math Problem (数学)

Given two 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 two positive integers a(1≤a≤2*10^4),b(1≤b≤10^9),and their meanings are shown in the description.Contains most of the 12W test cases.OutputFor each set of input data,output a line of two integers,representing X, Y.If you cannot find such X and Y,output one line of "No Solution"(without quotation).Sample Input

6 8
798 10780

Sample Output

No Solution
308 490

设gcd(x,y)=u,x=m*u,y=n*u
a/b=(mu+nu)/(m*n*u)
化简一下可以求出m,n,从而求出X,Y

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<vector>
 8 #include<cmath>
 9 using namespace std;
10 const double pi=acos(-1.0);
11 int n,d,x;
12 long long a,b,u;
13 long long gcd(long long a,long long b)
14 {
15     if (b==0) return a;
16      else return gcd(b,a%b);
17 }
18 int main()
19 {
20      while(~scanf("%lld%lld",&a,&b))
21      {
22          u=gcd(a,b);
23          a=a/u;
24          b=b/u;
25          if ( (long long)sqrt(a*a-4*b)*(long long)sqrt(a*a-4*b)==a*a-4*b )
26          {
27              long long t=(long long)sqrt(a*a-4*b);
28              if ((a+t)%2==0 && a>t) printf("%lld %lld
",u*(a-t)/2,u*(a+t)/2);
29               else printf("No Solution
");
30          } else printf("No Solution
");
31      }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/Annetree/p/7612460.html