Cod sursa(job #405437)

Utilizator ProtomanAndrei Purice Protoman Data 28 februarie 2010 00:27:03
Problema Ecuatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <math.h>

#define ll long long
#define Ld long double
#define pb push_back
#define mp make_pair
#define f first
#define s second

using namespace std;

ll a, b, c, k;
Ld delta;
vector <pair <ll, ll> > vctSol;

inline void test(ll x)
{
	ll p1 = x, p2 = a / x;

	if ((b + (ll) delta) % (2 * p2) == 0 && c % ((b + (ll) delta) / (2 * p2)) == 0)
		vctSol.pb(mp(p1, (b + delta) / (2 * p2)));
	if ((b - (ll) delta) % (2 * p2) == 0 && c % ((b - (ll) delta) / (2 * p2)) == 0)
		vctSol.pb(mp(p1, (b - delta) / (2 * p2)));
}

inline void afis(ll p1, ll q1)
{
	printf("(");

	if (p1 < 0)
		printf("-"), p1 = -p1;
	if (p1 != 1)
		printf("%d", p1);
	printf("x");

	if (q1 >= 0)
		printf("+");
	printf("%d)", q1);
}

int main()
{
	freopen("ecuatie.in", "r", stdin);
	freopen("ecuatie.out", "w", stdout);

	scanf("%lld %lld %lld %lld", &a, &b, &c, &k);

	delta = (Ld) b * b - 4 * a * c;

	if (delta < 0 || sqrt(delta) * sqrt(delta) != delta)
	{
		printf("-1\n");

		return 0;
	}
	delta = sqrt(delta);

	for (int i = 1; i < sqrt((Ld) a); i++)
		if (a % i == 0)
		{
			test(i);
			test(a / i);

			test(-i);
			test(-a / i);
		}
	if (sqrt((Ld) a) == (ll) sqrt((Ld) a))
	{
		test(sqrt((Ld) a));
		test(-sqrt((Ld) a));
	}

	sort(vctSol.begin(), vctSol.end());

	for (int i = 0; i < vctSol.size(); i++)
		if (a % vctSol[i].f != 0 || c % vctSol[i].s != 0 || (vctSol[i].f * c / vctSol[i].s + a / vctSol[i].f * vctSol[i].s != b))
			for (; ; );

	if (k > vctSol.size())
	{
		printf("-1\n");
		
		return 0;
	}
	else afis(vctSol[k - 1].f, vctSol[k - 1].s), afis(a / vctSol[k - 1].f, c / vctSol[k - 1].s);

	printf("\n");
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}