码迷,mamicode.com
首页 > 其他好文 > 详细

2019年安徽大学ACM/ICPC实验室新生赛(公开赛)D 不定方程

时间:2019-12-04 23:34:58      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:安徽   判断   turn   题意   情况   div   链接   else   clu   

题目链接: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 }

 

 

2019年安徽大学ACM/ICPC实验室新生赛(公开赛)D 不定方程

标签:安徽   判断   turn   题意   情况   div   链接   else   clu   

原文地址:https://www.cnblogs.com/chuyds/p/11986447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!