Cod sursa(job #675842)

Utilizator nautilusCohal Alexandru nautilus Data 8 februarie 2012 12:45:35
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include<fstream>
using namespace std;

int n;

void euclid_extins(int a, int b, int d, int &x, int &y, int &cmmdc)
{
 int x2,y2;
 if (d == -1)
	{
	 d = a / b;
	 euclid_extins(a,b,d,x2,y2,cmmdc);
	 x = y2;
	 y = x2 - y2 * d;
	} else

	{
	 swap(a, b);
	 b = b % a;
	 d = a / b;
	 if (a % b)
		{
		 euclid_extins(a,b,d,x2,y2,cmmdc);
		 x = y2;
		 y = x2 - y2 * d;
		} else
		{
		 x = 0;
		 y = 1;
		 cmmdc = b;
		}
	}
}

void read_solve()
{
 int i,a,b,c,x,y,cmmdc;
 ifstream fin("euclid3.in");
 ofstream fout("euclid3.out");
 fin>>n;
 for (i=1; i<=n; ++i)
	{
	 fin>>a>>b>>c;
	 euclid_extins(a, b, -1, x, y, cmmdc);
	 if (c % cmmdc == 0)
		{
		 x *= c / cmmdc;
		 y *= c / cmmdc;
		 fout<<x<<" "<<y<<'\n';
		} else
		fout<<"0 0"<<'\n';
	}
 fin.close();
 fout.close();
}

int main()
{
 read_solve();
 return 0;
}