Cod sursa(job #3047)

Utilizator Sorin_IonutBYSorynyos Sorin_Ionut Data 20 decembrie 2006 16:08:25
Problema Factorial Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream.h>

long desc(long aux);

long nr,min=0,max=100000000,med,val;
int sw=1;

int main()
{
 ifstream fin("fact.in");
 fin>>nr;
 fin.close();

 ofstream fout("fact.out");
 if(nr==0)
  fout<<'1';
 else
 {
  while((sw)&&(min!=max))
  {
   med=(min+max)/2-((min+max)/2)%5;
   val=desc(med);
   if(val==nr)
   {
    sw=0;
    fout<<med;
    fout.close();
    return 0;
   }
   else
       if(val>nr)
	max=med-5;
       else
	min=med+5;
  }
 }
 if(min==max)
 {
  val=desc(med);
  if(val-1==nr)
   fout<<max-5;
  else
   fout<<min+5;
 }
 fout<<"-1";
 fout.close();
 return 0;
}

long desc(long aux)
{
 long y=0,y1=aux;
 int sw=1;
 
 while(sw)
 {
  y1=y1/5;
  y=y+y1;
  if(y1==0)
   sw=0;
 }
return y;
}