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

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

时间:2016-05-04 22:38:25      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/Recoder/p/5459841.html

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