Cod sursa(job #2766709)

Utilizator BossBobsterRobert Alexandru Costin BossBobster Data 2 august 2021 22:43:41
Problema Ecuatie Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.45 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");

int fac1[3010], fac2[3010];
int main()
{
    int a, b, c, k, cur = 0, idx1 = 0, idx2 = 0;
    fin >> a >> b >> c >> k;
    for(int i = 1; i <= sqrt(abs(a)); i ++)
        if(a % i == 0)
        {
            fac1[idx1++] = i; if(abs(a/i) != abs(i)) fac1[idx1++] = a/i;
            fac1[idx1++] = -i; if(abs(a/i) != abs(i)) fac1[idx1++] = -a/i;
        }
    for(int i = 1; i <= sqrt(abs(c)); i ++)
        if(c % i == 0)
        {
            fac2[idx2++] = i; if(abs(c/i) != abs(i)) fac2[idx2++] = c/i;
            fac2[idx2++] = -i; if(abs(c/i) != abs(i)) fac2[idx2++] = -c/i;
        }
    sort(fac1, fac1+idx1); sort(fac2, fac2+idx2);
    if(k > idx1*idx2)
    {
        fout << "-1\n";
        return 0;
    }
    for(int i = 0; i < idx1; i ++)
        for(int j = 0; j < idx2; 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";
}