Cod sursa(job #2969339)

Utilizator sandry24Grosu Alexandru sandry24 Data 22 ianuarie 2023 21:23:24
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

int gcd(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
    g = gcd(abs(a), abs(b), x0, y0);
    if (c % g) {
        return false;
    }

    x0 *= c / g;
    y0 *= c / g;
    if (a < 0) x0 = -x0;
    if (b < 0) y0 = -y0;
    return true;
}

void solve(){
    int a, b, c, x, y, g;
    cin >> a >> b >> c;
    if(a == b && a == 0){
        cout << "1 1\n";
    } else {
        if(find_any_solution(a, b, c, x, y, g))
            cout << x << ' ' << y << '\n';
        else
            cout << "0 0\n";
    }
}
 
int main(){
    freopen("euclid3.in", "r", stdin);
    freopen("euclid3.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}