Cod sursa(job #1687847)

Utilizator vladvlad00Vlad Teodorescu vladvlad00 Data 13 aprilie 2016 09:21:19
Problema Ecuatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.05 kb
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <fstream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

ifstream fin("ecuatie.in");
ofstream fout("ecuatie.out");

struct descompunere
{
	int p1, q1, p2, q2;
};

void rez(long long&x1, long long&x2);
bool comp(descompunere a, descompunere b)
{
	return a.p1 < b.p1 || (a.p1 == b.p1 && a.q1 < b.q1);
}
void afisare();

long long a, b, c;
int k, nrsol;
descompunere sol[60005];

int main()
{
	int i, t1, t2;
	long long x1, x2;

	fin >> a >> b >> c >> k;
	rez(x1, x2);
	for (i = 1; i*i < a;i++)
		if (!(a%i))
		{
			t1 = i;
			t2 = a / i;
			//t1(x-x1)t2(x-x2)
			sol[++nrsol].p1 = t1;
			sol[nrsol].p2 = t2;
			sol[nrsol].q1 = -(x1*t1);
			sol[nrsol].q2 = -(x2*t2);
			
			//t1(x-x2)t2(x-x1)
			sol[++nrsol].p1 = t1;
			sol[nrsol].p2 = t2;
			sol[nrsol].q1 = -(t1*x2);
			sol[nrsol].q2 = -(t2*x1);

			//-t1(x-x1)-t2(x-x2)
			sol[++nrsol].p1 = -t1;
			sol[nrsol].p2 = -t2;
			sol[nrsol].q1 = -(x1*-t1);
			sol[nrsol].q2 = -(x2*-t2);

			//-t1(x-x2)-t2(x-x1)
			sol[++nrsol].p1 = -t1;
			sol[nrsol].p2 = -t2;
			sol[nrsol].q1 = -(-t1*x2);
			sol[nrsol].q2 = -(-t2*x1);

			//le inversez
			//t2(x-x2)t1(x-x1)
			sol[++nrsol].p2 = t1;
			sol[nrsol].p1 = t2;
			sol[nrsol].q2 = -(x1*t1);
			sol[nrsol].q1 = -(x2*t2);

			//t2(x-x1)t1(x-x2)
			sol[++nrsol].p2 = t1;
			sol[nrsol].p1 = t2;
			sol[nrsol].q2 = -(t1*x2);
			sol[nrsol].q1 = -(t2*x1);

			//-t2(x-x2)-t1(x-x1)
			sol[++nrsol].p2 = -t1;
			sol[nrsol].p1 = -t2;
			sol[nrsol].q2 = -(x1*-t1);
			sol[nrsol].q1 = -(x2*-t2);

			//-t2(x-x1)-t1(x-x2)
			sol[++nrsol].p2 = -t1;
			sol[nrsol].p1 = -t2;
			sol[nrsol].q2 = -(-t1*x2);
			sol[nrsol].q1 = -(-t2*x1);
		}
	if (i*i == a)
	{
		t1 = i;
		t2 = i;
		//t1(x-x1)t2(x-x2)
		sol[++nrsol].p1 = t1;
		sol[nrsol].p2 = t2;
		sol[nrsol].q1 = -(x1*t1);
		sol[nrsol].q2 = -(x2*t2);

		//t1(x-x2)t2(x-x1)
		sol[++nrsol].p1 = t1;
		sol[nrsol].p2 = t2;
		sol[nrsol].q1 = -(t1*x2);
		sol[nrsol].q2 = -(t2*x1);

		//-t1(x-x1)-t2(x-x2)
		sol[++nrsol].p1 = -t1;
		sol[nrsol].p2 = -t2;
		sol[nrsol].q1 = -(x1*-t1);
		sol[nrsol].q2 = -(x2*-t2);

		//-t1(x-x2)-t2(x-x1)
		sol[++nrsol].p1 = -t1;
		sol[nrsol].p2 = -t2;
		sol[nrsol].q1 = -(-t1*x2);
		sol[nrsol].q2 = -(-t2*x1);
	}
	if (k > nrsol)
	{
		fout << "-1\n";
		return 0;
	}
	sort(sol + 1, sol + 1 + nrsol, comp);
	afisare();
	return 0;
}

void rez(long long&x1, long long&x2)
{
	long long radicalDelta = (long long)(sqrt(double(b*b - 4 * a*c)));

	x1 = (-b + radicalDelta) / (2 * a);
	x2 = (-b - radicalDelta) / (2 * a);
}

void afisare()
{
	//prima paranteza
	fout << '(';
	if (sol[k].p1 == -1) fout << '-';
	else if (sol[k].p1 != 1) fout << sol[k].p1;
	fout << 'x';
	if (sol[k].q1 >= 0) fout << '+';
	fout << sol[k].q1;
	fout << ')';

	//a doua paranteza
	fout << '(';
	if (sol[k].p2 == -1) fout << '-';
	else if (sol[k].p2 != 1) fout << sol[k].p2;
	fout << 'x';
	if (sol[k].q2 >= 0) fout << '+';
	fout << sol[k].q2;
	fout << ')';
}