Cod sursa(job #1974310)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 27 aprilie 2017 13:03:18
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

void euclid(long long a, long long b, long long &x ,long long &y, long long &d)
{
    if(!b) {
        d = a;
        x = 1;
        y = 0;
        return;
    }
    long long x0, y0;
    euclid(b, a % b, x0, y0, d); /// b * x0 + [a - [a/b]*b]*y0 = d
    x = y0;
    y = x0 - (a/b) * y0;
}

int main()
{
    freopen("euclid3.in", "r", stdin);
    freopen("euclid3.out", "w", stdout);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    long long a,b,c;

    for(int i = 1; i <= N; ++i) {
        cin >> a >> b >> c;
        long long x,y,d;
        euclid(a,b,x,y,d);
        if(c % d != 0)
            cout << "0 0\n";
        else
            cout << c/d * x << " " << c/d * y << "\n";
    }


    return 0;
}