A Simple Math Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 396 Accepted Submission(s): 159
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*10^4),b(1≤b≤10^9),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
Source
Recommend
这道题最重要的地方是知道GCD(x,y)=GCD(a,b);然后解一元二次方程就可以了。
为什么
GCD(x,y)=GCD(a,b)呢,渣渣不知道,看了别人的利用反证法证明的过程
设c = gcd(x, y);
那么可以表达出x和y了,就是x = i * c; y = j * c;
因为i和j互质,所以(i + j) 和 i * j 互质。
假设他们不互质,那么设那个数是x,有,i + j = m * x; i * j = k * x;
那么有i和j都能整除x,(由第一条可以得到了),这和i和j互质矛盾。
所以gcd(a, b) = c了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
__int64 GCD(__int64 a,__int64 b)
{
return b?GCD(b,a%b):a;
}
int main()
{
__int64 x,x1,x2,y1,y2,y,i,j,a,b;
while(scanf("%I64d%I64d",&a,&b)!=EOF)
{
__int64 num=GCD(a,b);
b=b*num;
__int64 ans=a*a-4*b;
if(ans<0)
{
printf("No Solution\n");
}
else
{
if((__int64)sqrt(ans)*(__int64)sqrt(ans)!=ans)
{
printf("No Solution\n");
}
else
{
ans=(__int64)sqrt(ans);
y1=(a+ans)/2;
x1=a-y1;
y2=(a-ans)/2;
x2=a-y2;
if((a+ans)&1)
{
if((a-ans)&1)
{
printf("No Solution\n");
}
else
{
printf("%I64d %I64d\n",min(x2,y2),max(x2,y2));
}
}
else
{
if((a-ans)&1)
{
if(min(x1,y1)<0)
{
printf("No Solution\n");
}
else
{
printf("%I64d %I64d\n",min(x1,y1),max(x1,y1));
}
}
else
{
if(min(x1,y1)<0)
{
printf("%I64d %I64d\n",min(x2,y2),max(x2,y2));
}
else
{
if(min(x2,y2)<min(x1,y1))
{
printf("%I64d %I64d\n",min(x2,y2),max(x2,y2));
}
else
{
printf("%I64d %I64d\n",min(x1,y1),max(x1,y1));
}
}
}
}
}
}
}
return 0;
}