Codeforces Beta Round #7 C. Line 扩展欧几里德

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·1018 to 5·1018 inclusive, or to find out that such points do not exist.

Input

The first line contains three integers AB and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0.

Output

If the required point exists, output its coordinates, otherwise output -1.

Examples
input
2 5 3
output
6 -3
思路:简单的扩展欧几里德,就是b==0的情况小心点;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll __int64
#define inf 0xfffffff
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
ll gcd(ll x,ll y)
{
    return x%y==0?y:gcd(y,x%y);
}
void extend_Euclid(ll a, ll b, ll &x, ll &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    extend_Euclid(b, a % b, x, y);
    ll tmp = x;
    x = y;
    y = tmp - (a / b) * y;
}
int main()
{
    ll a,b,c,x,y;
    cin>>a>>b>>c;
    c=-c;
    if(b==0)
    {
        if(c%a==0)
        {
            cout<<c/a<<" 0"<<endl;
        }
        else
        cout<<"-1"<<endl;
        return 0;
    }
    ll gg=gcd(a,b);
    if(c%gg==0)
    {
        extend_Euclid(a,b,x,y);
        cout<<c/gg*x<<" "<<c/gg*y<<endl;
    }
    else
    cout<<"-1"<<endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jhz033/p/5458358.html