Cod sursa(job #1649641)

Utilizator savulescustefanSavulescu Stefan savulescustefan Data 11 martie 2016 14:27:30
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <cstdio>

using namespace std;
int p,n,s,m,st,dr,nr,poz,p1;
int main()
{
    freopen ("fact.in","r",stdin);
    freopen ("fact.out","w",stdout);
    scanf ("%d", &p1);
    if (p1==0)
        printf  ("%d", 1);
    else
    {
    st=5;
    dr=500000000;
    while (st<=dr)
    {
        m=(st+dr)/2;
        p=5;
        s=0;
        while (m>=p)
        {
            s=(0LL)+s+m/p;
            p*=5;
        }
        if (s==p1)
        {
            poz=m;
            break;
        }
        else if (p1>s)
            st=m+1;
        else
            dr=m-1;
    }
    if (poz>0)
    {
        while (poz%5!=0)
            poz--;
        printf ("%d", poz);
    }
    else
    {
        p=5;
        while (st>p)
        {
            s=(0LL)+s+st/p;
            p*=5;
        }
        if (s==p1)
        {
            while (st%5!=0)
            st--;
            printf ("%d", st);
        }
        else
            printf ("%d", -1);
    }
    }
    return 0;
}