Cod sursa(job #1834225)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 24 decembrie 2016 06:27:57
Problema Ecuatie Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

vector<ll> all_divs(ll x){
	x = max(x, -x);
	vector<ll> rez;
	for(ll i = 1; i*i <= x; ++i){
		if(x%i) continue;
		rez.push_back(i);
		rez.push_back(-i);
		rez.push_back(x/i);
		rez.push_back(-x/i); }
	sort(begin(rez), end(rez));
	rez.erase(unique(begin(rez), end(rez)), end(rez));
	return rez; }

void print_paren(ostream& lhs, const ll p, const ll q){
	lhs << '(';
	if(p == 1);
	else if(p == -1) lhs << '-';
	else lhs << p;
	lhs << 'x';
	if(q >= 0) lhs << '+';
	lhs << q;
	lhs << ')'; }

int main(){
	ifstream f("ecuatie.in");
	ofstream g("ecuatie.out");
	ll a, b, c, k;
	f >> a >> b >> c >> k;

	auto da = all_divs(a), dc = all_divs(c);

	for(const auto p1 : da){
		const auto p2 = a / p1;
		for(const auto q1 : dc){
			const auto q2 = c / q1;
			if(p1 * q2 + p2 * q1 == b){
				--k;
				if(!k){
					print_paren(g, p1, q1);
					print_paren(g, p2, q2);
					return 0; } } } } }