题目链接:https://ac.nowcoder.com/acm/contest/2720/D
题意:对于 ax = by = c ,给出 x, y 求符合条件的 a, b, c 且 c 为最小的解,不满足条件输出 -1。
idea:容易看出 c 为x, y 的最小公倍数。设最小公倍数为 s ,所以 a = s / x,b = s / y。因为 s = x * y / __gcd(x,y) ,所以 a = y / __gcd(),b = x / __gcd(),所以 x 和 y 一定互质。若__gcd(x, y) != 1,即可输出 -1。否则为最小公倍数。
当时每想出来判断 - 1 的情况,擦
代码:
1 #include <bits/stdc++.h>
2
3 using namespace std;
4 typedef long long ll;
5 ll t, x, y, ans;
6
7 int main()
8 {
9 scanf("%lld",&t);
10 while (t -- )
11 {
12 scanf("%lld%lld",&x,&y);
13 ll s = __gcd(x, y);
14 if (s != 1) cout << "-1" << endl;
15 else cout << y / s << " " << x / s << " " << x * y / s << endl;
16 }
17 return 0;
18 }