exgcd-Line(cf)
C. Line
time limit per test:1 second
memory limit per test
256 megabytesinput
standard inputoutput
standard outputA line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·10^18 to 5·10^18 inclusive, or to find out that such points do not exist.
Input
The first line contains three integers A, B and C ( - 2·10^9 ≤ A, B, C ≤ 2·10^9) — corresponding coefficients of the line equation. It is guaranteed that A^2 + B^2 > 0.
Output
If the required point exists, output its coordinates, otherwise output -1.
Examples
input
2 5 3
output
6 -3
exgcd 的模板题。exgcd用于求形如ax+by=c的方程的解,引用得到的是一个特解,可以凭此得到一个解系。
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll exgcd(ll a,ll b,ll &x,ll &y) { ll d=a; if(b!=0){ d=gcd(b,a%b,y,x); y-=(a/b)*x; } else {x=1;y=0;} return d; } int main() { ll a,b,c,x,y; cin>>a>>b>>c; ll d=exgcd(a,b,x,y);//这里求的是ax+by=gcd(a,b)的解 if(c%d!=0)cout<<"-1\n";//如果c不是d的倍数,则无解 else cout<<-x*(c/d)<<" "<<-y*(c/d)<<endl;//方程ax+by=gcd(a,b),两边同时乘c/gcd(a,b),要获得其他解只需要x-b,y+a; return 0; }
作者:凤涅
本文版权归作者所有,遵循CC-BY-4.0协议,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.