Cod sursa(job #284805)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 21 martie 2009 23:12:59
Problema Factorial Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include<stdio.h>
#include<math.h>
//#include<conio.h>

long put(long x)
{
 long k=0;
 while(x%5==0) { k++; x/=5; }
 return k;
}

/*long puti(long x, long &k1, long p)
{
 long k=0;
 while(x%5==0 && k1<p) { x/=5; k1++; }
 return k;
}*/

int main()
{
 //clrscr();
 long r,p,i,t1,t2,k1,k2=0,k;
 freopen("fact.in","r",stdin);
 freopen("fact.out","w",stdout);
 scanf("%ld",&p);
 if(p==0) printf("1");
 else{
 for(i=1; k2<p; i++)
 {
  k2=(pow(5,i)-1)/4;
  t2=pow(5,i);
 }
 if(k2==p) printf("%ld",t2);
 else{
 t1=t2/5;
 k1=(pow(5,put(t1))-1)/4;
 if(p<(k1+k2)/2) for(k=t1+5; k1<p; k+=5) k1+=put(k);
	    else for(k=t2; k2>p; k-=5) k2-=put(k);
 printf("%ld",k);}}
 return 0;
}