题解 CF7C 【Line】

公式变形:

ax+by+c=0\large∵ ax + by + c = 0

ax+by=c\large∴ ax + by = -c

然后就请出我们的贝祖(也叫pei shu)定理:

ax+by=n\large ax + by = na,b,nZ\large a, \,b, \,n \in Z,当且仅当gcd(a,b)n\large gcd(a, b) \,|\, n 时有整数解。

证明:

c=gcd(a,b)\large c = gcd(a, \,b),可将式子变为:

cp+cx=n\Large cp + cx = n

然后乘法分配律:

c(p+x)=n\Large c(p + x) = n

所以,gcd(a,b)n\large gcd(a, b) \,|\, n时有解

然后判断,有解 exgcd ,无解 -1

但是我们用 exgcd 是对于形如 ax+by=gcd(a,b)\large ax + by = gcd(a, b) 的,那么我们既然知道gcd(a,b)n\large gcd(a, b)\,|\,n,将其结果除以gcd(a,b)\large gcd(a, b) 然后乘以拓欧的结果可得到正确答案。

代码:

#include <iostream>
using namespace std;

#define int long long

inline int gcd(const int a, const int b)
{
	if(b == 0)	return a;
	return gcd(b, a % b);
}

inline bool bz(const int& a, const int& b, const int& n) {return n % gcd(a, b);} 

inline void exgcd(int& x, int& y, int a, int b)
{
	if(b == 0) {x = 1; y = 0; return ;}
	exgcd(y, x, b, a % b);
	y -= a / b * x;
}

signed main()
{
	int a, b, c;
	cin >> a >> b >> c; c = -c;
	if(bz(a, b, c)) {cout << "-1\n"; return 0;}
	int x, y; x = y = 0; exgcd(x, y, a, b);
	x = c / gcd(a, b) * x;
	y = c / gcd(a, b) * y;
	cout << x << " " << y << endl;
	return 0;
}
posted @ 2020-12-27 20:00  HappyBobb  阅读(17)  评论(0)    收藏  举报  来源