Cod sursa(job #2233736)

Utilizator skoda888Alexandru Robert skoda888 Data 24 august 2018 12:30:29
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
/*
//Arhiva Educationala - 002. Algoritmul lui Euclid extins

#include <iostream>
#include <fstream>
#include <vector>

//cmmdc(a, b) = cmmdc(b, a % b)

void Euclid_extins(long long _a, long long _b, long long& _x, long long& _y, long long& _cmmdc)
{
    if(_b == 0){
        _x = 1;
        _y = 0;
        _cmmdc = _a;
    }
    else{
        long long x0, y0;
        Euclid_extins(_b, _a % _b, x0, y0,  _cmmdc);
        _x = y0;
        _y = x0 - (_a / _b) * y0;
    }
}

int main()
{
    std::ifstream in("euclid3.in");
    std::ofstream out("euclid3.out");

    short T;
    in >> T;

    while(--T >= 0){
        long long a;
        long long b;
        long long c;
        in >> a >> b >> c;

        long long x;
        long long y;
        long long cmmdc;
        Euclid_extins(a, b, x, y, cmmdc);
        if(c % cmmdc != 0){
            out << 0 << ' ' << 0 << '\n';
        }
        else{
            out << (c / cmmdc) * x << ' ' << (c / cmmdc) * y << '\n';
        }

    }
    return 0;
}
*/
#include <fstream>
#include <iostream>
using namespace std;

int gcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    else
    {
        int x0, y0, d;
        d = gcd(b, a % b, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
        return d;
    }
}


int main()
{
    ifstream _in("euclid3.in");
    ofstream _out("euclid3.out");
    int T;
    _in >> T;
    for (int i = 0; i < T; i++)
    {
        int a, b, c, d, x, y;
        _in >> a >> b >> c;
        // cout << a << " " << b << " " << c << endl;
        d = gcd(a, b, x, y);
        if (c % d != 0)
        {
            _out << "0 0"
                 << "\n";
        }
        else
        {
            int coefX = x * (c / d);
            int coefY = y * (c / d);
            _out << coefX << " " << coefY << "\n" ;
        }
    }

    return 0;
}