Cod sursa(job #486775)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 22 septembrie 2010 19:09:47
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <stdio.h>

using namespace std;

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

int main()
{
    freopen ("euclid3.in","r",stdin);
    freopen ("euclid3.out","w",stdout);

    int N;
    scanf("%d",&N);

    int a,b,c,d,x,y;
    while(N--)
    {
        scanf("%d %d %d",&a,&b,&c);
        euclid(a,b,&d,&x,&y);
        if( c % d)
            printf("0 0\n");
        else
            printf("%d %d\n",x * (c / d),y * (c / d));
    }
    return 0;
}