Cod sursa(job #149467)

Utilizator jeanFMI - Petcu Ion Cristian jean Data 5 martie 2008 19:19:52
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.48 kb
#include<fstream.h>
#include<math.h>
long n,m=1,i=4,aux,j=1,k,ok=1;
int main()
{
ifstream f("fact.in");
ofstream g("fact.out");
f>>n;

if(n==0) g<<"1";
else if(n==1) g<<"5";
else

{while(m<n)
 {aux=m;
 m+=i+j;
 i*=5; j++;
 }
 if(m>n) {
	  i/=5;j-=2;
	  for(k=1;k<=j;k++)
	   if(aux-k==n) {ok=0;break;}
	  if(ok) {m=i+pow(5,j)+(n-aux)*5;g<<m;}
	  else g<<"-1";
	  }
    else if(m==n) {i/=5;j--;
		   m=i*5+pow(5,j); g<<m;}
}
f.close();
g.close();
return 0;
}