Link:
Http://codeforces.com/problemset/problem/7/C
Question:
Given the equation ax found + identified by formula + found C numbers = 0. where A, B, and C are known, X and Y are obtained.
Analysis and Summary:
Extends the template of Euclidean algorithms. This algorithm can be found in several books or on the Internet.
This algorithm is used to obtain the linear equation AX + by = gcd (A, B );
Then, this equation can be converted:
Ax + by = gcd (A, B)
=> AX + by =-C/Z, where-C/z = gcd (A, B)
=> Ax * z + by * z = C.
X and Y can be obtained by extending the Euclidean algorithm,
Then, we only need to find Z, while Z =-C/gcd (A, B );
Therefore, the final answer x = x * (-C/gcd (A, B), Y = y * (-C/gcd (a, B ));
Code:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long LL;const LL INF = 5*1e18;void gcd(LL a, LL b, LL& d,LL& x, LL& y){ if(!b){d=a; x=1; y=0; } else {gcd(b,a%b,d,y,x); y -= x*(a/b); }}int main(){ LL a,b,c,d,x,y; cin >> a >> b >> c; gcd(a,b,d,x,y); if(c%d != 0) puts("-1"); else cout << -x*(c/d) << " " << -y*(c/d) << endl; return 0;}
-- The significance of life is to give it a meaningful person.
Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)