Cod sursa(job #2522577)

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

vector<int> divA, divC;

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);
	for (int i = 1; i * i <= aa; ++i) {
		if (aa % i == 0) {
			divA.push_back(i);
			divA.push_back(-i);
			divA.push_back(a / i);
			divA.push_back(-a / i);
		}
	}
	sort(divA.begin(), divA.end());
	divA.resize(unique(divA.begin(), divA.end()) - divA.begin());
	for (int i = 1; i * i <= cc; ++i) {
		if (cc % i == 0) {
			divC.push_back(i);
			divC.push_back(-i);
			divC.push_back(c / i);
			divC.push_back(-c / i);
		}
	}
	sort(divC.begin(), divC.end());
	divC.resize(unique(divC.begin(), divC.end()) - divC.begin());
	int nr = 0;
	for (int x : divA) {
	for (int y : divC) {
		int xx = a / x, yy = c / y;
		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;
}