Cod sursa(job #111497)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 30 noiembrie 2007 08:46:36
Problema Ecuatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <stdio.h>
#include <stdlib.h>
long a, b, c, k;
long x[1000], y[1000], nra, nrc;

void citire()
{
   freopen("ecuatie.in","r",stdin);
   freopen("ecuatie.out","w",stdout);
   scanf("%ld %ld %ld %ld", &a, &b, &c, &k);
}

long ab(long x)
{
   if (x < 0) return -x;
   return x;
}

void descomp_a()
{
   long i, n;
   n = ab(a);
   for (i = 1; i <= n; i++)
      if (n % i == 0) x[nra++] = i;
   for (i = 0; i < nra; i++) x[nra + i] = -x[i];
   nra *= 2;
   nra--;
}

void descomp_c()
{
   long i, n;
   n = ab(c);

   for (i = 1; i <= n/2; i++)
      if (n % i == 0) y[nrc++] = i;
   for (i = 0; i < nrc; i++) y[nrc + i] = -y[i];
   nrc *= 2;
   nrc--;
}

int cmp(const void *a, const void *b)
{
   return *(long*)a - *(long*)b;
}

int main()
{
   citire();
   descomp_a();
   descomp_c();
   qsort(x,nra+1,sizeof(long),cmp);
   qsort(y,nrc+1,sizeof(long),cmp);
   int i, j;
   i = 1;
   while (i * nrc <= k) i++;
   i--;
   k -= ((i-1) * nrc);
   j=1;
   while (j < k) j++;

   printf("(");
   if (x[i] == -1) printf("-");
   else if (x[i] != 1) printf("%d",x[i]);
   printf("x");

   if (y[j] < 0) printf("%d",y[j]);
   else printf ("+%d",y[j]);
   printf(")(");

   long aux;
   aux = a / x[i];
   if (aux == -1) printf("-");
   else if (aux != 1) printf("%d",aux);
   printf("x");

   aux = c / y[j];
   if (aux < 0) printf("%d",aux);
   else printf("+%d",aux);
   printf(")\n");
   return 0;
}