Cod sursa(job #2719976)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 10 martie 2021 14:29:53
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <algorithm>
#include <fstream>
#include <assert.h>

using namespace std;

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

typedef long long ll;

void euclid_extins(ll a,ll b,ll &x,ll &y){
    if(b == 0){
        x = 1;
        y = 0;
        return;
    }

    euclid_extins(b,a % b,x,y);

    ll xnou = y;
    ll ynou = x - y * (a / b);
    x = xnou;
    y = ynou;
}

int main()
{
    int t;
    in>>t;
    for(int i = 1; i <= t; i++){
        ll a,b,c;
        in>>a>>b>>c;
        ll d = __gcd(a,b);
        if(c % d != 0){
            out<<0<<" "<<0<<'\n';
        }else{
            ll x,y;
            euclid_extins(a,b,x,y);
            x = x * c / d;
            y = y * c / d;
            x -= b / d * 2;
            y += a / d * 2;
            out<<x<<" "<<y<<'\n';
            assert(1ll * a * x + 1ll * b * y == 1ll * c);
        }
    }
    return 0;
}