本文主要是介绍G - Line CodeForces - 7C,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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·1018inclusive, or to find out that such points do not exist.
Input
The first line contains three integers A, B 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
题目意思就是给一个二元一次方程AX+BY+CZ =0 给出了A,B,C让从 - 5*10^18 to 5·10^18
;里找出满足 A*A+B*B>0条件的值。没有的话输出-1;
这题就是欧几里得算法扩展的应用把AX+BY+CZ =0化成 AX+BY=-C/gcd(A,B)*gcd(A,B);
对于不完全为0的非负整数a,b,gcd(a, b)表示a, b的最大公约数,必定存在整数对x,y,满足a*x+b*y==gcd(a, b)
求解不定方程;如a*x+b*y=c; 已知a, b, c的值求x和y的值a*x+b*y=gcd(a, b)*c/gcd(a, b);
最后转化为 a*x/(c/gcd(a, b))+b*y/(c/gcd(a, b))=gcd(a, b)最后求出的解x0,y0乘上c/gcd(a, b)就是最终的结果了
x1=x0*c/gcd(a, b); y1=y0*c/gcd(a, b);
代码
#include<stdio.h>
long long gcd(long long a,long long b,long long &x,long long &y)
{long long 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()
{long long a,b,c,d,x,y;scanf("%lld%lld%lld",&a,&b,&c);d=gcd(a,b,x,y);c=-c;if(c%d!=0)printf("-1\n");elseprintf("%lld %lld\n",x*c/d,y*c/d);return 0;}
这篇关于G - Line CodeForces - 7C的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!