Pagini recente » Cod sursa (job #1372502) | Cod sursa (job #908547) | Cod sursa (job #1646552) | Cod sursa (job #991229) | Cod sursa (job #342489)
Cod sursa(job #342489)
//Problema factorial - Infoarena
#include<stdio.h>
int p;
long x,y,m;
long nrz(long);
int main()
{
freopen("fact.in","r",stdin);
freopen("fact.out","w",stdout);
scanf("%d",&p);
if(p==0)
printf("1");
else
{
x=0;
y=2000000000;
while(x<y)
{
m=(x+y)/2;
if(p==nrz(m))
{
x=m;
y=m;
}
else
{
if(p>nrz(m))
x=m+1;
else
y=m-1;
}
}
if(nrz(x)!=p&&nrz(x+1)!=p)
printf("-1");
else
{
if(nrz(x)==p)
{
x=x-(x%5);
printf("%ld",x);
}
else
printf("%ld",x+1);
}
}
return 0;
}
long nrz(long z)
{
long a;
long b;
a=5;
b=0;
while(z>=a)
{
b=b+z/a;
a*=5;
}
return b;
}