Pagini recente » Cod sursa (job #2406329) | Cod sursa (job #2733171) | Cod sursa (job #2336101) | Cod sursa (job #1249106) | Cod sursa (job #343473)
Cod sursa(job #343473)
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
using namespace std;
int a, b, c, k, x1, x2;
vector <pair <int, int> > vctDiviz;
vector <pair <pair <int, int>, pair <int, int> > > vctSol;
inline int gcd(int a, int b)
{
if (!b)
return a;
return gcd(b, a % b);
}
inline void form(int coef, int af)
{
printf("(");
if (coef < 0 && coef == -1)
printf("-");
if (coef != 1 && coef != -1)
printf("%d", coef);
printf("x");
if (af > 0)
printf("+");
printf("%d)", af);
}
int main()
{
freopen("ecuatie.in", "r", stdin);
freopen("ecuatie.out", "w", stdout);
scanf("%d %d %d %d", &a, &b, &c, &k);
k--;
int cmmdc = abs(gcd(abs(a), gcd(abs(b), abs(c))));
int x1 = (b - sqrt((double) b * b - 4 * a * c)) / (2 * a);
int x2 = (b + sqrt((double) b * b - 4 * a * c)) / (2 * a);
for (int diviz = 1; diviz * diviz <= cmmdc; diviz++)
if (cmmdc % diviz == 0)
{
vctDiviz.pb(mp(diviz, cmmdc / diviz));
vctDiviz.pb(mp(-diviz, -cmmdc / diviz));
if (diviz * diviz != cmmdc)
{
vctDiviz.pb(mp(cmmdc / diviz, diviz));
vctDiviz.pb(mp(-cmmdc / diviz, -diviz));
}
}
for (int i = 0; i < vctDiviz.size(); i++)
vctSol.pb(mp(mp(vctDiviz[i].f, vctDiviz[i].f * x1), mp(vctDiviz[i].s, vctDiviz[i].s * x2)));
for (int i = 0; i < vctDiviz.size(); i++)
vctSol.pb(mp(mp(vctDiviz[i].f, vctDiviz[i].f * x2), mp(vctDiviz[i].s, vctDiviz[i].s * x1)));
sort(vctSol.begin(), vctSol.end());
if (k >= vctSol.size())
printf("-1\n");
else
{
form(vctSol[k].f.f, vctSol[k].f.s);
form(vctSol[k].s.f, vctSol[k].s.s);
printf("\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}