Codeforces7C 扩展欧几里得

Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

 Status

Description

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.

Sample Input

Input
2 5 3
Output
6 -3

Source

题意:给定a,b,c,求ax+by= - c 时的x和y。

题解:扩展欧几里得模板题。

#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
void exgcd(ll a,ll b,ll &x,ll &y)
{
   if(b)
   {
       exgcd(b,a%b,y,x);
       y-=(a/b)*x;
   }
   else
   {
       x=1;
       y=0;
   }
}
int main()
{
    ll a,b,c;
    while(cin>>a>>b>>c)
    {
        c=-c;
        ll d=gcd(a,b);
        if(c%d)
            cout<<-1<<endl;
        else
        {
            a/=d;b/=d;c/=d;
            ll x,y;
            exgcd(a,b,x,y);
            cout<<x*c<<" "<<y*c<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Ritchie/p/5312396.html