Cod sursa(job #217338)

Utilizator ProtomanAndrei Purice Protoman Data 28 octombrie 2008 00:26:26
Problema Ecuatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <stdio.h>
#include <algorithm>
#include <math.h>
#define mx 55000

using namespace std;

pair <pair <int, int>, pair <int, int> > s[mx];
pair <int, int> vec[mx];
int a, b, c, k, ma;

int md(int x)
{
	if (x > 0)
		return x;
	return -x;
}

int cmmdc(int a, int b)
{
	while (b)
	{
		int c = a % b;
		a = b;
		b = c;
	}
	return a;
}

void baga(int x, int y)
{
	vec[++vec[0].first].first = x;
	vec[vec[0].first].second = y;
}

int main()
{
	freopen("ecuatie.in","r",stdin);
	freopen("ecuatie.out","w",stdout);
	scanf("%ld %ld %ld %ld", &a, &b, &c, &k);
	ma = cmmdc(md(a), cmmdc(md(b), md(c)));
	int x1 = -(-b - sqrt((double) b * b - 4 * a * c)) / (2 * a);
	int x2 = -(-b + sqrt((double) b * b - 4 * a * c)) / (2 * a);
	for (int i = 1; i <= sqrt((double) ma); i++)
		if (ma % i == 0)
		{
			baga(i, ma / i);
			baga(-i, -ma / i);
			if (ma / i != i)
			{
				baga(ma / i, i);
				baga(-ma / i, -i);
			}
		}
	for (int i = vec[0].first; i; i--)
	{
		s[i].first.first = vec[i].first;
		s[i].first.second = x1 * vec[i].first;
		s[i].second.first = vec[i].second;
		s[i].second.second = x2 * vec[i].second;
		s[vec[0].first + i].first = s[i].second;
		s[vec[0].first + i].second = s[i].first;
	}
	sort(s + 1, s + 1 + 2 * vec[0].first);
	
	// afisare

	printf("(");
	printf("%ldx", s[k].first.first);
	if (s[k].first.second >= 0)
		printf("+");
	printf("%ld}", s[k].first.second);
	printf("(");
	printf("%ldx", s[k].second.first);
	if (s[k].second.second >= 0)
		printf("+");
	printf("%ld}", s[k].second.second);
	fclose(stdin);
	fclose(stdout);
	return 0;
}