Cod sursa(job #3141)

Utilizator Sorin_IonutBYSorynyos Sorin_Ionut Data 21 decembrie 2006 06:07:30
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream.h>

long desc(long aux);

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

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

 ofstream fout("fact.out");
 if(nr==1)
 {
  fout<<5;
  fout.close();
  return 0;
 }
 if(nr<0)
 {
 fout<<-1;
 fout.close();
 return 0;
 }
 if(nr>100000000)
 {
  fout<<-1;
  fout.close();
  return 0;
 }
 if(nr==0)
  fout<<'1';
 else
 {
  while((sw)&&(max>=min))
  {
   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-1;
       else
	if(val<nr)
	 min=med+1;
  }
 }
 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;
}