当前位置 : 主页 > 编程语言 > c语言 >

XI Samara Regional Intercollegiate Programming Contest Russia, Problem A.Restoring Numbers

来源:互联网 收集:自由互联 发布时间:2023-09-07
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. Input A single line contains two integers s and g (1 ≤ s ≤ 109 , 1 ≤

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.
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
standard input standard output
6 2 4 2
7 2 -1

给定两个数字sum 以及 gcd ,求出这两个数;
(智障题)。。
考虑无解:sum%gcd!=0 || sum%gcd==0&&sum/gcd=1;
很好理解;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 500005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

bool prime(int x) {
    if (x == 0 || x == 1)return false;
    for (int i = 2; i <= sqrt(x); i++) {
        if (x%i == 0)return false;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    ll s, g;
    cin >> s >> g;
    if (s%g != 0)cout << -1 << endl;
    else if (s == 1 && g == 1)cout << -1 << endl;
    else {
        int k = s / g;
        if (k == 1)cout << -1 << endl;
        else cout << 1ll * g << ' ' << (k - 1)*g << endl;
    }
}
【转自:美国多ip服务器 http://www.558idc.com/mgzq.html 欢迎留下您的宝贵建议】
网友评论