Cod sursa(job #2222320)

Utilizator icansmileSmileSmile icansmile Data 16 iulie 2018 21:09:33
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long int cmmdc(long long int a, long long int b) {
    if (b == 0)
        return a;

    return cmmdc(b, a % b);
}

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

int main() {
    int tests;
    long long int first, second, third, d, x, y;

    ifstream input("euclid3.in");
    ofstream output("euclid3.out");

    input >> tests;

    for (int i = 0; i < tests; i++) {
        input >> first >> second >> third;
        x = 0;
        y = 1;
        d = cmmdc(first, second);

        solve(first, second, &x, &y, &d);

        if (third % d != 0) {
            output << "0 0\n";
            continue;
        }

        output << x * third / d  << " " << y * third / d << endl;
    }

    input.close();
    output.close();

    return 0;
}