Pagini recente » Cod sursa (job #1944917) | Cod sursa (job #2230600) | Cod sursa (job #1433918) | Cod sursa (job #1690866) | Cod sursa (job #2270279)
//
// main.cpp
// Algoritm extins Euclid
//
// Created by Darius Buhai on 27/10/2018.
// Copyright © 2018 Darius Buhai. All rights reserved.
//
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("euclid3.in");
ofstream fout("euclid3.out");
pair<long, long> euclid(long a,long b)
{
if(b==0)
return {1,0};
pair<long,long> p = euclid(b,a%b);
return {p.second, p.first - (a/b) * p.second};
}
int main() {
int n, a, b;
fin>>n;
while(n){
fin>>a>>b;
pair<long, long> e = euclid(a, b);
fout<<e.first<<" "<<e.second<<endl;
n--;
}
return 0;
}
/*
ak + bl = d
bk' + (a%b)l' = d
ak + bl = bk' + (a%b)*l'
a = b*(a/b) + a%b
a%b = a-b*(a/b)
ak + bl = bk' + (a-b * (a/b)) * l'
ak + bl = bk' + al' - bl' * (a/b)
k = l'
b = k'-l*(a/b)
72 % 21 = 9 //
21 % 9 = 3 //
9 % 3 = 0 // 0, 0 - 3*1
3 % 0 = 0 // 1, 0
*/