Cod sursa(job #1509625)

Utilizator SunKaiBursuc Tudor Gheorghe SunKai Data 24 octombrie 2015 09:43:31
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
#include <climits>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int main()
{long i,j,ok,m,nr,P,prod;
    fin>>P;
    i=1;
    j=LONG_MAX;
    ok=0;
    if(P==0)fout<<1;
    else

    {

    while(i<=j&&ok==0)
    {
        m=(i+j)/2;
        nr=0;
        prod=5;
        while(prod<=m)
        {
            nr=nr+m/prod;
            prod=prod*5;

        }
        if(nr==P)ok=1;
        if(nr>P)j=m-1;
        else i=m+1;
    }
        if(ok==0)fout<<-1;
        else
        {
            while(m%5!=0)
            {
                m--;

            }
            fout<<m;
        }
    }

    return 0;
}