Cod sursa(job #2522571)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 12 ianuarie 2020 18:04:38
Problema Ecuatie Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 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;
	for (int i = 1; i * i <= abs(a); ++i) {
		if (abs(a) % 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 <= abs(c); ++i) {
		if (abs(c) % 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 (1LL * xx * y + 1LL * 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;
		}
	} }	
	return 0;
}