exgcd-Line(cf)

C. Line
time limit per test:1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A 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 AB 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;
}

 


posted @ 2019-08-15 19:26  wengsy150943  阅读(332)  评论(0编辑  收藏  举报