Cod sursa(job #988541)

Utilizator ludacrivasilii teodorovici ludacri Data 23 august 2013 10:09:32
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include<stdio.h>
#define LL long long
#define min(a,b) (a<b ? a : b)
LL p;
void read()
{
freopen("fact.in","r",stdin);
freopen("fact.out","w",stdout);
scanf("%lld",&p);
}
 
LL p2(LL n)
{
LL num=2,s=0;
while(n/num)
    {
    s+=n/num;
    num=num<<1;
    }
return s;
}
 
LL p5(LL n)
{
LL num=5,s=0;
while(n/num)
    {
    s+=n/num;
    num=num*5;
    }
return s;
}
 
LL okay(LL n)
{
return min(p2(n),p5(n));
}
 
void solve()
{
LL st=1,dr=(LL)1<<50,m,last=-1;
while(st<=dr)
    {
    m=(st+dr)>>1;
    if(okay(m)>=p)
       {
       dr=m-1;
       if(okay(m)==p && (m<last || last==-1))
        last=m;
       }
    else
       st=m+1;
    }
printf("%lld\n",last);
}
int main()
{
read();
solve();
return 0;
}