Cod sursa(job #2522581)

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

const int DIM = 1e5 + 5;

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] = i;
			divA[++n] = -i;
			if (i != aa / i) {
				divA[++n] = a / i;
				divA[++n] = -a / i;
			}
		}
	}
	sort(divA + 1, divA + n + 1);
	int m = 0;
	for (int i = 1; i * i <= cc; ++i) {
		if (cc % i == 0) {
			divC[++m] = i;
			divC[++m] = -i;
			if (i != cc / i) {
				divC[++m] = c / i;
				divC[++m] = -c / i;
			}
		}
	}
	sort(divC + 1, divC + m + 1);
	int nr = 0;
	for (int i = 1; i <= n; ++i) {
	for (int j = 1; j <= m; ++j) {
		int x = divA[i], y = divC[j];
		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;
}