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.
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
题目大意:
给定a,b,找到一组x,y满足
x + y = a;
lcm(x,y)=b;
化简式子
y = x - a;
x * y = b * gcd(x,y);
设k=gcd(x,y) 可得 x * (x-a) = k * b;
那么,我们可以通过枚举k得到x,并验证k是否等于gcd(x,y)。
枚举k时嘤判断求得x是否为整数。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
long long gcd(long long x,long long y)
{
return y?gcd(y,x%y):x;
}
int main()
{
long long a,b;
long long x,y;
bool found;
while(~scanf("%lld%lld",&a,&b)){
found=0;
long long sz=min((a*a)/4/b+1,a);
for(long long k=1;k<sz;++k){
double c=sqrt(a*a-4*k*b);
if(!(c-(int(c)))&&!((a+(int)c)&1)){
x=(a-c)/2;y=(a+c)/2;
if(gcd(x,y)==k){
found=1;
break;
}
}
}
if(found)
printf("%lld %lld\n",x,y);
else
printf("No Solution\n");
}
return 0;
}