Cf 7C line (Extended Euclidean linear equation)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.