Pagini recente » Cod sursa (job #639842) | Cod sursa (job #1094655) | Cod sursa (job #2864353) | Cod sursa (job #2804935) | Cod sursa (job #1964978)
#include<fstream>
#include<algorithm>
#include<cmath>
using namespace std;
ifstream fin("ecuatie.in");
ofstream fout("ecuatie.out");
int a,b,c,k,na,nc,nr,av[10000],cv[10000];
int i,p,j,q;
void divizori(int x, int &n, int y[])
{
int s = x;
if (s < 0)
s = -s;
int i = (int)sqrt((double)s);
int t = (int)sqrt((double)s);
n = 0;
if (t*t == x)
{
y[++n] = t;
t--;
}
for (i=1; i<=t; i++)
if (s%i == 0)
{
n += 2;
y[n-1] = i;
y[n] = s/i;
}
for (i=1; i<=n; i++)
y[n+i] = -y[i];
n *= 2;
sort(y+1, y+n+1);
}
int main()
{
fin >> a >> b >> c >> k;
divizori(a, na, av);
divizori(c, nc, cv);
for (i=1; i<=na; i++)
{
p = a/av[i];
for(j=1; j<=nc; j++)
{
q = c/cv[j];
if (cv[j]*p+av[i]*q == b)
{
nr++;
if (nr == k)
{
fout << "(";
if (av[i] == -1)
fout << "-";
else
{
if (av[i] != 1)
fout << av[i];
}
fout << "x";
if (cv[j] >= 0)
fout << "+";
fout << cv[j] << ")(";
if (p == -1)
fout << "-";
else
{
if (p != 1)
fout<<p;
}
fout << "x";
if (q >= 0)
fout << "+";
fout << q << ")";
}
}
}
}
if (nr < k)
fout << -1;
return 0;
}