CodeForces 7C Line
ax+by+c=0可以转化为ax+by=-c;
可以用扩展欧几里德算法来求ax1+by1=gcd(a,b)来求出x1,y1
此时gcd(a,b)不一定等于-c,假设-c=gcd(a,b)*z,可得z=-c/gcd(a,b);
则ax+by=-c <==> (ax1+by1)*z=gcd(a,b)z;
<==> ax1*z+bx2*z=gcd(a,b)z;
因此可以得知x与x1的关系,y与y1的关系:
x=x1*z,y = y1*z(z上面已经求出来了)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> using namespace std; typedef long long ll; ll r; void gcd(ll a, ll b, ll &x, ll &y) { if(b == 0) { x = 1; y = 0; r = a; return ; } gcd(b, a % b, x, y); int t = x; x = y; y = t - a / b * y; } int main() { ll a, b, c, x, y; while(~scanf("%lld%lld%lld", &a, &b, &c)) { c = -c; gcd(a, b, x, y); if(c % r != 0) printf("-1\n"); else printf("%lld %lld\n", c / r * x, c / r * y); } return 0; }