Pagini recente » Cod sursa (job #432925) | Cod sursa (job #2904294) | Cod sursa (job #2380861) | Cod sursa (job #225056) | Cod sursa (job #1609648)
#include <algorithm>
#include <cmath>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ecuatie.in");
ofstream fout("ecuatie.out");
typedef long long i64;
i64 a, b, c, k;
double x1, x2;
struct str {
i64 p1, p2, q1, q2;
};
vector <str> v;
inline str mp( i64 p1, i64 p2, i64 q1, i64 q2 ) {
str sol;
sol.p1= p1, sol.p2= p2, sol.q1= q1, sol.q2= q2;
return sol;
}
void check( int x ) {
i64 auxq1= (i64)x*x1, auxq2= (i64)a/x*x2;
if ( (double)auxq1==(double)x*x1 && (double)auxq2==(double)a/x*x2 ) {
v.push_back(mp(x, a/x, -auxq1, -auxq2));
v.push_back(mp(a/x, x, -auxq2, -auxq1));
}
auxq1= (i64)x*x2, auxq2= (i64)a/x*x1;
if ( (double)auxq1==(double)x*x1 && (double)auxq2==(double)a/x*x2 ) {
v.push_back(mp(x, a/x, -auxq1, -auxq2));
v.push_back(mp(a/x, x, -auxq2, -auxq1));
}
}
bool cmp( str x, str y ) {
if ( x.p1!=y.p1 ) {
return x.p1<y.p1;
} else {
if ( x.q1!=y.q1 ) {
return x.q1<y.q1;
} else {
if ( x.p2!=y.p2 ) {
return x.p2<y.p2;
} else {
return x.q2<y.q2;
}
}
}
}
int main( ) {
fin>>a>>b>>c>>k;
i64 delta= ((i64)b*b-(i64)4*a*c), sqr;
sqr= sqrt((i64)delta);
if ( delta<0 || (i64)sqr*sqr!=delta ) {
fout<<"-1";
return 0;
}
x1= ((double)-b+sqr)/a/2, x2= ((double)-b-sqr)/a/2;
i64 newa= a;
if ( newa<0 ) {
newa= -newa;
}
i64 sqra= sqrt(newa);
for ( i64 i= 1; i<=sqra; ++i ) {
if ( a%i==0 ) {
check(i);
check(-i);
check(a/i);
check(-a/i);
}
}
sort( v.begin(), v.end(), cmp );
for ( vector <str>::iterator it= v.begin(), it2; it!=v.end(); ++it ) {
it2= it;
++it2;
if ( it2!=v.end() ) {
if ( (*it2).p1==(*it).p1 && (*it2).p2==(*it).p2 && (*it2).q1==(*it).q1 && (*it2).q2==(*it).q2 ) {
v.erase(it2);
}
}
}
if ( (i64)v.size()<k ) {
fout<<"-1\n";
return 0;
}
--k;
fout<<"(";
if ( v[k].p1==-1 ) {
fout<<"-x";
} else if ( v[k].p1!=1 ) {
fout<<v[k].p1<<"x";
} else {
fout<<"x";
}
if ( v[k].q1<0 ) {
fout<<v[k].q1<<")(";
} else {
fout<<"+"<<v[k].q1<<")(";
}
if ( v[k].p2==-1 ) {
fout<<"-x";
} else if ( v[k].p2!=1 ) {
fout<<v[k].p2<<"x";
} else {
fout<<"x";
}
if ( v[k].q2<0 ) {
fout<<v[k].q2<<")";
} else {
fout<<"+"<<v[k].q2<<")";
}
fout<<"\n";
return 0;
}