Cod sursa(job #951987)

Utilizator apopeid14Apopei Daniel apopeid14 Data 22 mai 2013 14:52:49
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
 using namespace std;
long desc(long aux);
 
long nr,min=0,med,val;
unsigned long max=4000000000;
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==5)
 {
  fout<<-1;
  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;
   if(min-1==med)
    sw=0;
   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;
}