Cod sursa(job #2542720)

Utilizator memecoinMeme Coin memecoin Data 10 februarie 2020 15:40:17
Problema Ecuatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>

#define INF 0x3f3f3f3f

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "ecuatie";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

void printNr(int x) {
    if (abs(x) == 1) {
        if (x == -1) {
            fout << "-";
        }
    } else {
        fout << x;
    }
}

int main() {
    
    int x,y,z,k;
    
    fin >> x >> y >> z >> k;

    vector<int> divX;
    vector<int> divZ;
    
    for (int i = 1; i <= abs(x); ++i) {
        if (x % i == 0) {
            divX.push_back(i);
            divX.push_back(-i);
        }
    }
    
    for (int i = 1; i <= abs(z); ++i) {
        if (z % i == 0) {
            divZ.push_back(i);
            divZ.push_back(-i);
        }
    }
    
    sort(divX.begin(), divX.end());
    sort(divZ.begin(), divZ.end());
    
    int i = 1;
    
    for (auto a: divX) {
        int c = x / a;
        for (auto b: divZ) {
            int d = z / b;
            
            if (a * d + b * c == y) {
                if (i == k) {
                    fout << "(";
                    printNr(a);
                    fout << "x";
                    if (b > 0) {
                        fout << "+";
                    }
                    printNr(b);
                    fout << ")";
                    fout << "(";
                    printNr(c);
                    fout << "x";
                    if (d > 0) {
                        fout << "+";
                    }
                    printNr(d);
                    fout << ")";
                    return 0;
                }
                i++;
            }
        }
    }
    
    return 0;
}