Pavel had two positive integers a and b. He found their sum s and greatest common divisor g, and forgot a and b after that. Help him to restore the original numbers.
A single line contains two integers s and g (1 ≤ s ≤ 109, 1 ≤ g ≤ 109) — sum and greatest common divisor of the numbers a and b.
If Pavel made a mistake and there are no such numbers a and b, output a single number - 1.
Otherwise, output two positive integers a and b on a single line, separated by a space. If there are multiple possible solutions, output any of them.
6 2
4 2
7 2
-1
这是一道很有意思的题目,就是有两个数a,b;给你他们的和s以及他们的gcd,求出a和b;刚开始想着用循环暴力,结果21一直超时,后来仔细一想发现只要让a和b中的一个是它们的gcd,再判断是否他们的和是否是s就行了。放代码:
#include<iostream>
#include<algorithm>
using namespace std;
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;
}
int main()
{
long long s,g,k;
cin>>s>>g;
if(gcd(g,s-g)==g&&s!=g)
{
cout<<max(g,s-g)<<" "<<min(g,s-g)<<endl;
}
else cout<<"-1"<<endl;
return 0;
}