A Simple Math Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 14 Accepted Submission(s): 12
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亚洲区大连站-重现赛(感谢大连海事大学)
打表发现…gcd(X,Y)=gcd(a,b)
所以:
x+y=a
x*y=gcd(a,b)*b
得到一元二次方程
PS:居然没有special judge 要输出(x,y)字典序小的答案 题面也没给 醉了…
证明:
令:gcd(X,Y)=d
x=X/d
y=Y/d
LCM(X,Y)=X*Y/gcd(X,Y)=x*y*d
又因为:
b=LCM(X,Y)=x*y*d
a=X+Y=(x+y)*d
gcd(a,b)=gcd( x*y*d , (x+y)*d )
显然gcd(x,y)=1
所以gcd(a,b)=d=gcd(X,Y)
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
ll a,b;
while(~scanf("%lld%lld",&a,&b)){
bool haveAns=false;
ll k=gcd(a,b)*b;
//x^2-ax+k==0
if(a*a<4*k){
puts("No Solution");
continue;
}
double t=sqrt(a*a-4*k);
ll ans1=((double)a-t)/2.0;
ll ans2=((double)a+t)/2.0;
if(ans1*(a-ans1)==k){
printf("%lld %lld\n",ans1,a-ans1);
continue;
}
if(ans2*(a-ans2)==k){
printf("%lld %lld\n",ans2,a-ans2);
continue;
}
puts("No Solution");
}
return 0;
}