Cod sursa(job #2613073)

Utilizator SmeusSmeu Tudor Smeus Data 9 mai 2020 14:36:57
Problema Algoritmul lui Euclid extins Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
///ax + b y = d
///d = cmmdc(a, b)
///x, y = ?
#include <iostream>
#include <fstream>
using namespace std;


void cmmdc_extins(int a, int b,int &d, int &x, int &y)
{
    if(b == 0)
    {
        x = 1;
        y = 1;
        d = a;
    }
    int x2, y2;
    cmmdc_extins(b, a % b, d, x2, y2);
    x = y2;
    y2 = x2 - (a/b)*y2;

}

int cmmdc_extins(int a, int b,int &x, int &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int x2, y2;
    int d = cmmdc_extins(b, a%b, x2, y2);
    x = y2;
    y = x2 - (a/b)*y2;
    a = b;
    b = a% b;
    return d;
}

int main()
{
    int n, a, b, c, x, y;
    ifstream cin("euclid3.in");
    ofstream cout("euclid3.out");
    cin>>n;
    while(cin>>a>>b>>c)
    {
        int d = cmmdc_extins(a, b,x, y);
        if(c % d == 0)
            cout<<x*c/d<<" "<<y*c/d<<"\n";
        else
            cout<<0<<" "<<0<<"\n";
    }


    return 0;
}