Cod sursa(job #1532113)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 21 noiembrie 2015 15:00:06
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("euclid3.in");
ofstream out("euclid3.out");
int gcd(int a,int b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

void extendedgcd(int a, int b, int &x, int &y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return ;
    }
    int x0, y0;
    extendedgcd(b, a % b, x0, y0); /// calculam solutiile ecuatiei b * x0 + (a % b) * y0 = d
    x = y0;
    y = x0 - (a / b) * y0;
}

int main()
{
    int n,a,b,c;
    in>>n;
    for(int i=1;i<=n;i++)
    {
        in>>a>>b>>c;
        int d = gcd(a, b);
        int x, y;
        extendedgcd(a, b, x, y);
        /// solutiile ecuatiei a * x + b * y = d  solutiile ecuatiei a * x + b * y = c
                                                        /// (c / d) * (a * x + b * y) = c
                                                        /// a * (x * c / d) + b * (y * c /d) = c
        if(c % d != 0)
            out << "0 0\n";
        else
            out << x * (c / d) << ' ' << y * (c / d) << '\n';
    }
    return 0;
}