Cod sursa(job #2766706)

Utilizator BossBobsterRobert Alexandru Costin BossBobster Data 2 august 2021 22:40:02
Problema Ecuatie Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <iostream>
#include <string.h>
#include <random>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <valarray>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<string, string> pss;
typedef pair<int, char> pic;
typedef pair<pii, int> piii;
typedef pair<double, double> pdd;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ull, ull> pull;
typedef complex<double> cd;
//#define max(n, m) ((n>m)?n:m)
//#define min(n, m) ((n<m)?n:m)
#define f first
#define s second
#define input() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

ifstream fin("ecuatie.in");
ofstream fout("ecuatie.out");

vector<int> fac1, fac2;
int main()
{
    int a, b, c, k, cur = 0;
    fin >> a >> b >> c >> k;
    for(int i = 1; i <= sqrt(abs(a)); i ++)
        if(a % i == 0)
        {
            fac1.push_back(i); if(abs(a/i) != abs(i)) fac1.push_back(a/i);
            fac1.push_back(-i); if(abs(a/i) != abs(i)) fac1.push_back(-a/i);
        }
    for(int i = 1; i <= sqrt(abs(c)); i ++)
        if(c % i == 0)
        {
            fac2.push_back(i); if(abs(c/i) != abs(i)) fac2.push_back(c/i);
            fac2.push_back(-i); if(abs(c/i) != abs(i)) fac2.push_back(-c/i);
        }
    sort(fac1.begin(), fac1.end()); sort(fac2.begin(), fac2.end());
    for(int i = 0; i < fac1.size(); i ++)
        for(int j = 0; j < fac2.size(); j ++)
        {
            if(fac1[i] * (c/fac2[j]) + (a/fac1[i]) * fac2[j] == b)
                cur ++;
            if(cur == k)
            {
                string s1 = ((fac1[i]==1)?"":"-");
                fout << "(" << ((abs(fac1[i])==1)?s1:to_string(fac1[i])) << "x" << ((abs(fac2[j])==fac2[j])?"+":"-") << abs(fac2[j]) << ")";
                s1 = (((a/fac1[i])==1)?"":"-");
                fout << "(" << ((abs(a/fac1[i])==1)?s1:to_string(a/fac1[i])) << "x" << ((abs(c/fac2[j])==c/fac2[j])?"+":"-") << abs(c/fac2[j]) << ")\n";
                return 0;
            }
        }
    fout << "-1\n";
}