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
Input
Input includes multiple sets of test data.Each test data occupies one line,including two positive integers a(1≤a≤2104),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
题意
满足x+y=a; lcm(x,y)=b;
解析:a和b有公约数;(x1+y1)c=a; x1y1c=b;(公倍数)
然后解一个方程;(因为数据大,不能用枚举)
#include<stdio.h>
#include<math.h>
int gcd(int a,int b)
{
return b>0?gcd(b,a%b):a;
}
int main()
{
long long a1,b1,i,j,c,flag,a,b,s,x,y;
while(scanf("%lld%lld",&a,&b)!=EOF)
{
c=gcd(a,b);
b=b*c;
s=sqrt(a*a-4*b);
if(s<0||s*s!=a*a-4*b) //在判定他是不是小数 s*s!=a*a-4*b(小数会不相等)和可不可以解出来。
{
printf("No Solution\n");
continue;
}
y=(a+s)/2;
x=a-y;
printf("%lld %lld\n",x,y);
}
}