Pagini recente » Cod sursa (job #2161586) | Cod sursa (job #925999) | Cod sursa (job #2388103) | Cod sursa (job #1407819) | Cod sursa (job #1527910)
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std;
int len,v[20000],nrsol,i;
long long A,B,C,k,D;
struct solutie
{
long p1,p2,q1,q2;
};
solutie sol[10000];
inline void Read()
{
ifstream fin("ecuatie.in");
fin>>A>>B>>C>>k;
fin.close();
}
inline long long Delta()
{
long long delta;
delta=B*B-4*A*C;
if(delta<0)
return -1;
long double t=sqrt(delta);
if(t*t==delta)
return t;
return -1;
}
inline bool Cmp(const solutie A, const solutie B)
{
if (A.p1 == B.p1)
return A.q1 < B.q1;
return A.p1 < B.p1;
}
inline void Divizori()
{
int d;
long long val;
val=A;
if(A<0)
val=-A;
for(d=1;d*d<=val;d++)
if(A%d==0)
{
v[++len]=d;
v[++len]=-d;
v[++len]=A/d;
v[++len]=-(A/d);
}
if((d-1)*(d-1)==val)
len-=2;
}
inline void Solutie(long double x1, long double x2, int poz)
{
sol[poz].p1=v[i];
sol[poz].p2=A/v[i];
sol[poz].q1=(-1)*x1*v[i];
sol[poz].q2=((-1)*x2*A)/v[i];
}
int Verifica(int poz)
{
if((sol[poz].q1*sol[poz].q2==C)&&((sol[poz].p1*sol[poz].p2)==A)&&(sol[poz].p1*sol[poz].q2+sol[poz].q1*sol[poz].p2)==B)
return 1;
return 0;
}
inline void Solve()
{
ofstream fout("ecuatie.out");
int d;
long double x1,x2;
D=Delta();
if(D==-1)
fout<<"-1\n";
else
{
Divizori();
x1=(long double)(-B-D)/(long double)(2*A);
x2=(long double)(-B+D)/(long double)(2*A);
nrsol=1;
for(i=1;i<=len;i++)
{
Solutie(x1,x2,nrsol);
if(Verifica(nrsol)!=0)
nrsol++;
if(D!=0)
{ Solutie(x2,x1,nrsol);
if(Verifica(nrsol)!=0)
nrsol++;
}
}
nrsol--;
if(nrsol>=k)
{
sort(sol+1,sol+nrsol+1,Cmp);
fout<<'(';
if(sol[k].p1==-1)
fout<<"-";
else
if(sol[k].p1!=1)
fout<<sol[k].p1;
fout<<'x';
if(sol[k].q1!=0)
{
if(sol[k].q1>0)
fout<<'+';
fout<<sol[k].q1;
}
fout<<")(";
if(sol[k].p2==-1)
fout<<"-";
else
if(sol[k].p2!=1)
fout<<sol[k].p2;
fout<<'x';
if(sol[k].q2!=0)
{
if(sol[k].q2>0)
fout<<'+';
fout<<sol[k].q2;
}
fout<<')'<<'\n';
}
else
fout<<"-1\n";
}
fout.close();
}
int main()
{
Read();
Solve();
return 0;
}