Cod sursa(job #2522592)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 12 ianuarie 2020 18:18:42
Problema Ecuatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <bits/stdc++.h>
using namespace std;

const int DIM = 1e5 + 5;

pair<int, int> divA[DIM], divC[DIM];

int main(void) {
	freopen("ecuatie.in", "r", stdin);
	freopen("ecuatie.out", "w", stdout);
	int a, b, c, k;
	cin >> a >> b >> c >> k;
	int aa = abs(a), cc = abs(c);
	int n = 0;
	for (int i = 1; i * i <= aa; ++i) {
		if (aa % i == 0) {
			divA[++n] = make_pair(i, a / i);
			divA[++n] = make_pair(-i, -a / i);
			if (i != aa / i) {
				divA[++n] = make_pair(a / i, i);
				divA[++n] = make_pair(-a / i, -i);
			}
		}
	}
	sort(divA + 1, divA + n + 1);
	int m = 0;
	for (int i = 1; i * i <= cc; ++i) {
		if (cc % i == 0) {
			divC[++m] = make_pair(i, c / i);
			divC[++m] = make_pair(-i, -c / i);
			if (i != cc / i) {
				divC[++m] = make_pair(c / i, i);
				divC[++m] = make_pair(-c / i, -i);
			}
		}
	}
	sort(divC + 1, divC + m + 1);
	int nr = 0;
	for (int i = 1; i <= n; ++i) {
		int x, xx, y, yy;
		tie(x, xx) = divA[i];
	for (int j = 1; j <= m; ++j) {
		tie(y, yy) = divC[j];
		if (xx * y + yy * x == b)
			++nr;
		if (nr == k) {
			cout << "(";
			if (x == -1) cout << "-";
			else if (x != 1) cout << x;
			cout << "x";
			if (y > 0) cout << "+";
			cout << y;
			cout << ")";
					
	
			cout << "(";
			if (xx == -1) cout << "-";
			else if (xx != 1) cout << xx;
			cout << "x";
			if (yy > 0) cout << "+";
			cout << yy;
			cout << ")";
			return 0;
		}
	} }
	cout << -1;	
	return 0;
}