Cod sursa(job #1865334)

Utilizator msciSergiu Marin msci Data 1 februarie 2017 18:14:11
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <tuple>
#include <limits>
#include <functional>
#include <complex>
#include <bitset>
#include <list>

//#include <atomic>
//#include <condition_variable>
//#include <future>
//#include <mutex>
//#include <thread>

using namespace std;

#define debug(x) cerr << #x << " = " << x << "\n"

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

int gcd(int a, int b)
{
    if (a < 0 && b < 0) a = -a, b = -b;
    if (a > b) swap(a, b);
    while (a > 0)
    {
        int r = b % a;
        b = a;
        a = r;
    }
    return b;
}

int main(int argc, char* argv[]) 
{
#ifndef LOCAL
    freopen("euclid3.in", "r", stdin);
    freopen("euclid3.out", "w", stdout);
#endif
    cin.sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        int d = gcd(a, b);
        int k = 1;
        if (c % d != 0)
        {
             a /= d;
             a *= c;
             b /= d;
             b *= c;
             d = c;
        }
        else
        {
            k = c / d;
        }
        int x, y;
        extended_euclid(a, b, d, x, y);
        x *= k, y *= k;
        cout << x << " " << y << "\n";
    }
    return 0;
}