#7 C. Line (扩展欧几里得)

题目链接:点击打开链接

http://codeforces.com/contest/7/problem/C

C. Line

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

题解:

扩展GCD。


// 扩展欧几里德算法,解gcd(a, b) = ax + by
// 结果存储在x,y中,用户调用时保证a、b、c都是整数
// 返回a,b的最大公约数
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!a && !b)return -1;
    if(!b)return x=1,y=0,a;
    ll d=exgcd(b,a%b,y,x);
    return y-=a/b*x,d;
}


// 等式ax+by=c,已知a、b、c,求x和y。
// 解该线性方程等同于解同余式ax = c(mod b)
// 返回值表示是否有解,true有解,false无解
bool linear_equation(int a, int b, int c, int &x, int &y)
{
    int n = exgcd(a, b, x, y);
    if(c%n)
        return false;
    int k = c/n;
    x *= k;
    y *= k;
    return true;
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!a && !b)return -1;
    if(!b)return x=1,y=0,a;
    ll d=exgcd(b,a%b,y,x);
    return y-=a/b*x,d;
}
int main()
{
    ll a,b,c,x,y,t;
    scanf("%lld%lld%lld",&a,&b,&c);
    t=exgcd(a,b,x,y);
   // cout<<t<<endl;
    //cout<<x<<endl;
   // cout<<y<<endl;
    if(c%t) return 0*printf("-1");
    x *= -c/t;
	y *= -c/t;
    return 0*printf("%lld %lld",x,y);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值