深圳幻海软件技术有限公司 欢迎您!

codeforces 7C 【扩展欧几里得算法】

2023-03-30

B- 楼下水题TimeLimit:1000MS     MemoryLimit:262144KB     64bitIOFormat:%I64d&%I64uSubmit S
B - 楼下水题
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  CodeForces 7C

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
 
    
思路:
 
    
这道题是扩展欧几里得算法模板题,就是在最后求出x,y之后,还要将x,y带入到原方程里面,求出方程的结果,然后
用c再确定x,y的具体的值!
代码:
 
    
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. typedef long long ll;
  5. using namespace std;
  6. ll a,b,c;
  7. void gcd(ll a,ll b,ll& d,ll& x,ll& y)
  8. {
  9. if(!b)
  10. {
  11. d=a;
  12. x=1;
  13. y=0;
  14. }
  15. else
  16. {
  17. gcd(b,a%b,d,y,x);
  18. y-=x*(a/b);
  19. }
  20. }
  21. int main()
  22. {
  23. while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF)
  24. {
  25. ll d,x,y;
  26. gcd(a,b,d,x,y);
  27. ll sum=a*x+b*y;
  28. if(-c%sum!=0)
  29. {
  30. printf("-1\n");
  31. }
  32. else
  33. {
  34. ll k=-c/sum;
  35. printf("%lld %lld\n",k*x,k*y);
  36. }
  37. }
  38. return 0;
  39. }


文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览42515 人正在系统学习中