A. Restoring Numbers

A. Restoring Numbers
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Pavel had two positive integers a and b. He found their sum s and greatest common divisor g, and forgot a and bafter that. Help him to restore the original numbers.

Input

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.

Output

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.

Examples
input
Copy
6 2
output
Copy
4 2
input
Copy
7 2
output
Copy
-1

題意:輸入一個s和g;讓你根據 s和g 求 a和b 的值,其中a+b等於s,g=gcd(a,b),如果找不到整數a和b輸出-1;
因爲a和b都是g的倍數那麼可以寫成xg=a,yg=b,那麼(x+y)g=s;x+y=1+(x+y-1);而gcd(1*g,(x+y-1)*g)=g;

當然還要排除a==0||b==0,驗證一下輸出就行了。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<map>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<ctime>
#include<stack>
#define PI 3.14159265358797832384626
using namespace std;
int main()
{
    long long s,g,a,b;
    while(~scanf("%lld%lld",&s,&g))
    {
        a=((s/g)-1)*g;
        b=g;
        if(a==0||b==0)
            printf("-1\n");
        else if(__gcd(a,b)==g&&a+b==s)
            printf("%lld %lld\n",a,b);
        else
            printf("-1\n");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章