Cod sursa(job #1498854)

Utilizator sebinechitasebi nechita sebinechita Data 9 octombrie 2015 16:26:53
Problema Algoritmul lui Euclid extins Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("euclid3.in");
ofstream fout("euclid3.out");
#define cout fout
int gcd(int a, int b, int &x, int &y)
{
    /**
    a * x + b * y = d;
    b * x' + (a%b) * y' = d
    ax + by = bx' + ay' - b*[a / b]y'
    a(x - y') = b(x' - y - [a / b]y')
    ===> x = y'
    ===> y = x' - [a / b]y'
    **/
    if(!b)
    {
        x = 1;
        y = 0;
        return a;
    }
    int x0, y0;
    int d = gcd(b, a % b, x0, y0);
    x = y0;
    y = x0 - (a / b) * y0;
    return d;
}

int main()
{
    int t, a, b, c, d, x, y;
    fin >> t;
    while(t--)
    {
        fin >> a >> b >> c;
        d = gcd(a, b, x, y);

        if(c % d != 0)
        {
            cout << "0 0";
        }
        else
        {
            cout << x * c / d << " " << y * c / d << "\n";
        }
    }
}