Cod sursa(job #1115817)

Utilizator olly2204Olly2204 olly2204 Data 22 februarie 2014 01:56:15
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
//#include "stdafx.h"

 //references
//http://www.infoarena.ro/algoritmul-lui-euclid

#include <fstream>
#include <iostream>

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

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

int main()
{
    int T;
    in >> T;

    for (int i = 0; i < T; i++)
    {
        long a,b,c;
        in >> a >> b >> c;
        long x, y, d = 0;

        euclid(a, b, &d, &x, &y);

        if (c % d != 0)
        {
            x = 0;
            y = 0;
        }
        else 
        {
            long mult = c / d;
            x *= mult;
            y *= mult;
        }

        out << x << " " << y << "\n";
    }
}