Cod sursa(job #1402053)

Utilizator danalex97Dan H Alexandru danalex97 Data 26 martie 2015 11:56:33
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <iostream>
using namespace std;

ifstream F("euclid3.in");
ofstream G("euclid3.out");

int t,a,b,c,d;

int cmmdc(int x,int y)
{
    return y == 0 ? x : cmmdc(y,x%y);
}

void euclid(int a,int &x,int b,int &y,int c)
{
    if ( b == 0 )
    {
        x = 1;
        y = 0;
        return;
    }
    int x0 = 0 , y0 = 0;
    euclid(b,x0,a%b,y0,c);

    x = y0;
    y = x0 - (a/b) * y0;
}

int main()
{
    F>>t;
    for (int i=1;i<=t;++i)
    {
        F>>a>>b>>c;
        d = cmmdc(a,b);
        if ( c % d )
            G<<"0 0\n";
        else
        {
            int x = 0, y = 0;
            euclid(a,x,b,y,d);

            G<<x*(c/d)<<' '<<y*(c/d)<<'\n';
        }
    }
}