Problem Description
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
Input
Input includes multiple sets of test data.Each test data occupies one line,including two positive integers a(1≤a≤2*104),b(1≤b≤109),and their meanings are shown in the description.Contains most of the 12W test cases.
Output
For 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
此题存在以下难点
证明1
i,j互质则i+j,ij互质
(如果 i, j 互质, i+j 和 ij 不互质,那么 i+j 和 ij 存在公因数 T ,那么由题意可知 ,T必定为 i 或 j 的因数,同时 T 还为 i + j 的因数,那么假设 i % t == 0, ( i + j ) % t == 0, j 必定为 t 的一个倍数,满足不了i j 互质,故猜想成立。)
证明2
如果i,j互质那么ik=A,j*k=B那么gcd(A,B)=k(易证,如果k不是AB的gcd那么gcd m一定大于k(因为k已经是AB的一个公约数了,最大的公约数m肯定比k大)那么就要从i,j中提取一部分给k使之变为m,但i,j互质)
下面就简单了
A = X+Y , B= GMD(X,Y) 求 x 最小时的 X Y ,无解输出 No Solition
然后先去看本题 A = X+Y , B = GMD(X,Y) 那么有已知的最小公倍数和最大公约数关系可知
B * GCD(X,Y) = X*Y
看到 X*Y已知,X+Y已知,求X Y,很不由自主的就想到了韦达定理。。
那么根据韦达定理 (设 gcd(x,y) = k)
X+Y = A
X-Y = sqrt( AA - 4k*B)
所以整个问题的难点之一就在于 如何把 k 变成已知量
ki=X
kj=Y
i, j互质
k(i+j)=A
k(ij)=B
根据1 i+j和ij互质
根据2 gcd(A,B)=k;
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
#define ll long long int
ll gcd(ll a,ll b)
{
if(a%b)
return gcd(b,a%b);
return b;
}
ll max(ll a, ll b)
{
if(a>b)
return a;
else
return b;
}
ll min(ll a, ll b)
{
if(a>b)
return b;
else
return a;
}
ll a,b;
ll x,y;
int main()
{
while(~scanf("%lld%lld",&a,&b))
{
ll k=gcd(a,b);
ll d = a*a-4*b*k;
ll dd =(ll)sqrt(1.0*d);
if(d<0)
{
printf("No Solution\n");
continue;
}
else if((dd+a)%2!=0||dd*dd!=d)
{
printf("No Solution\n");
continue;
}
else
{
x = (a + dd) / 2;
y = a - x;
printf("%lld %lld\n",min(x,y),max(x,y));
}
}
return 0;
}