7C - Line 扩展欧几里得

原题链接

居然一时没反应过来这是扩欧,我是sb
题意:给你一条直线方程,问你能否在找到两个整数点,满足直线方程。

思路:\(Ax + By = C\), 求x和y的整数解,这不是扩欧吗?所以直接扩欧算一波就行,注意检验算出来后的符号即可。

代码如下

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set> 
#include<iomanip>
#define IOS 	cin.tie(0), ios::sync_with_stdio(false) 
#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10, M = 1e5 + 10, mod = 1e9;
const double eps = 1e-4;
const double pi = acos(-1);

ll a, b, c;

ll exgcd(ll a, ll b, ll &x, ll &y)
{
	if(!b)
	{
		x = 1, y = 0;
		return a;
	}
	
	ll d = exgcd(b, a % b, y, x);
	y -= a / b * x;
	return d;
}

int main()
{
	IOS;
	cin >> a >> b >> c;
	ll x, y;
	ll d = exgcd(a, b, x, y);
	if(c % d)	cout << -1;
	else
	{
		ll k = c / d;
		x *= k, y *= k;
		if(a * x + b * y + c != 0)
			x = -x, y = -y;
		cout << x << " " << y << endl;
	}

	return 0;
}
posted @ 2021-04-05 19:02  beatlesss  阅读(61)  评论(0编辑  收藏  举报