Codeforces Beta Round #7 C. Line (扩展欧几里德)

时间:2022-11-16 13:13:41

题目链接:http://codeforces.com/problemset/problem/7/C

给你一个直线方程,有整数解输出答案,否则输出-1。

扩欧模版题。这里有讲解:http://www.cnblogs.com/Recoder/p/5459812.html

(很久没写exgcd,都不会写了)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 
 5 LL exgcd(LL a , LL b , LL &x , LL &y) {
 6     LL res = a;
 7     if(!b) {
 8         x = 1 , y = 0;
 9     }
10     else {
11         res = exgcd(b , a % b , x , y);
12         LL temp = x;
13         x = y;
14         y = temp - a / b * y;
15     }
16     return res;
17 }
18 
19 int main()
20 {
21     LL a , b , c , gcd , x , y;
22     cin >> a >> b >> c;
23     gcd = exgcd(a , b , x , y);
24     if(c % gcd == 0) {
25         LL temp = -c / gcd;
26         cout << x * temp << " " << y * temp << endl;
27     }
28     else {
29         cout << -1 << endl;
30     }
31 }