Cod sursa(job #1604488)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 18 februarie 2016 12:42:06
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <iostream>
#include <fstream>

using namespace std;

struct triple{
    int first, second, third;
    triple(const int a, const int b, const int c):
        first(a), second(b), third(c){}
};

triple euclid_extins(const int a, const int b){
    if(b == 0){
        return triple(1, 0, a);
    }
    const triple tmp = euclid_extins(b, a%b);
    const int x_ = tmp.second, y_ = tmp.first, g = tmp.third;

    // a * x + b * y = g
    // a * x - b*(a/b)*x + b*(a/b)*x + b*y = g
    // (a%b)*x + b * (y + (a/b)*x)
    return triple(x_, y_ - x_*(a/b), g);
}

void solve(ifstream& f, ofstream& g){
    int a, b, c;
    f >> a >> b >> c;
    triple t = euclid_extins(a, b);
    if(c%t.third != 0){
        g << "0 0\n";
        return;
    }
    const int nr = c / t.third;
    g << (t.first * nr) << ' ' << (t.second * nr) << '\n';
}

int main()
{
    ifstream f("euclid3.in");
    ofstream g("euclid3.out");
    int t;
    f >> t;
    for( ; t > 0; --t){
        solve(f, g);
    }
    return 0;
}