Cod sursa(job #2470780)

Utilizator NavadaruCalinNavadaru Calin NavadaruCalin Data 9 octombrie 2019 18:53:46
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda basic_stuff Marime 0.81 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f("euclid3.in");
ofstream g("euclid3.out");

int eucl_ext(int a, int b, int c, int &x, int &y)
{
    int q[100], x0, y0, r, pas = 0;
    while(b)
    {
        q[++pas]=a/b;
        r=a%b;
        a=b;
        b=r;
    }
    if(c%a)
    {
        x=y=0;
        return 0;
    }
    x = x0 = c/a;
    y = y0 = 0;
    while(pas)
    {
        x = y0;
        y = x0 - q[pas--]*y0;
        x0 = x;
        y0 = y;
    }
    return 1;
}
int main()
{
    int a, b, c, x, y, n;
    f>> n;
    while(n)
    {
        f >> a >> b >> c;
        int e = eucl_ext(a,b,c,x,y);
        if(e==1)
            g << x << ' ' << y << '\n';
        else
            g << 0 << ' ' << 0 << '\n';
        n--;
    }
    return 0;
}