Cod sursa(job #1299463)

Utilizator RazecBotez Cezar Razec Data 23 decembrie 2014 17:48:42
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include<cstdio>
int ok;
long long p,m,L,R;
long long number(long long v)
{
    long long nr=0;
    while(v>=1)
        {
            v=v/5;
            nr+=v;
        }
    return nr;
}
int main()
{
    freopen("fact.in","r",stdin);
    freopen("fact.out","w",stdout);
    scanf("%lld",&p);
    L=0;R=(1<<31)-1;ok=1;
    if(!p)
        {
            printf("1\n");
            return 0;
        }
    while(L<=R && ok)
    {
        m=(L+R)/2;
        if(number(m)==p)
            {ok=0;continue;}
        if(number(m)>p)
            {R=m-1;continue;}
        if(number(m)<p)
            {L=m+1;continue;}
    }
    if(ok) printf("-1\n");
    else
    {
        while(m%5) m--;
        printf("%lld\n",m);
    }
    return 0;
}