Cod sursa(job #1364595)

Utilizator VladutMihaiViraVladut Mihai Vira VladutMihaiVira Data 27 februarie 2015 18:53:29
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>
#define nmax 4000000005
using namespace std;
int main ()
{
    unsigned long long n,x,f,p,ls,ld,gasit;
    ifstream f1("fact.in");
    ofstream g("fact.out");
    f1>>p;
    ls=5;
    ld=nmax;
    gasit=0;
    if(p==0)
        g<<1;
    else
    {
        while(ls<=ld && gasit==0)
        {
            n=(ls+ld)/2;
            x=0;
            f=5;
            while(f<=n)
            {
                x=x+n/f;
                f=f*5;
            }
            if(x==p)
            {
                g<<n/5*5;
                gasit=1;

            }
            else if(x<p)
                ls=n+1;
            else ld=n-1;

        }

        if(gasit==0)
            g<<-1;
    }
    return 0;
}