A Simple Math Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 486 Accepted Submission(s): 196
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
2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5974
一个数 拆成两个正整数和相加为a,最小公倍数为b
x+y=a;
xy=b;
g=gcd(x,y); 设ng=x,mg=y,
则m,n都为质数 m+n=a/g; m*n=b/g;
这时有个结论 n,m互质则 n*m与n+m互质 证明略 求证好多人都无果(也可以不用这个结论 直接试a的因子就OK了a最大就2W)
所以 gcd(x,y)=gcd(a,b); 只要解一个一元二次方程就行了
所以O(1) 注意有多组输出 x小的
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
if(!b) return a;
else return gcd(b,a%b);
}
int main ()
{
int a,b,g,w,p;
while(~scanf("%d%d",&a,&b))
{
g=gcd(b,a);
w=a/g;
p=b/g;
double dt=w*w-4*p;
//printf("%lf\n",dt);
double n1,n2,m1,m2;
if(dt<0) {printf("No Solution\n");continue;}
n1=(w-sqrt(dt))/2.0;m1=(w*1.0-n1);
n2=(w+sqrt(dt))/2.0;m2=(w*1.0-n2);
int flag=0;
if(m1<n1) swap(n1,m1);
if(m2<n2) swap(n2,m2);
if(n1>n2) {swap(n1,n2);swap(m1,m2);}
// printf("%lf %lf %lf %lf\n",n1,m1,n2,m2);
int n3=n1;int m3=m1;
int n4=n2;int m4=m2;
if(n3==n1)
if(n1>0&&m1>0)
{ flag=1;
printf("%d %d\n",n3*g,m3*g);
}
if(!flag)
if(n4==n2)
if(n2>0&&m2>0)
{
flag=1;
printf("%d %d\n",n4*g,m4*g);
}
if(!flag)
printf("No Solution\n");
}
return 0;
}