Cod sursa(job #1984652)

Utilizator ruxandramateiMatei Ruxandra ruxandramatei Data 25 mai 2017 16:57:39
Problema Algoritmul lui Euclid extins Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <assert.h>

using namespace std;

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

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



int main(){
  int n;
  in>>n;
  for(int i=1;i<=n;i++){
    int x = 0, y= 0,d=0;
    int a, b , c;
    bool ok = false;
    in>>a>>b>>c;
    assert( -1000000000 <= a && a <= 1000000000 );
    assert( -1000000000 <= b && b <= 1000000000 );
    assert( -2000000000 <= c && c <= 2000000000 && c != 0 );
    if(a<b){
      int temp = a;
      a = b;
      b = temp;
      ok = true;
    }
    euclid(a,b,d,x,y);
    if(c == (c/d)*d){
      x = x * c/(d);
      y = y * c/(d);
      if(ok==true){
        int temp = x;
        x = y;
        y = temp;
      }
    }
    else{
      x = y = 0;
    }
      out<<x<<' '<<y<<'\n';
  }
  return 0;
}