Cod sursa(job #2159129)

Utilizator mirelPmirel p mirelP Data 10 martie 2018 19:22:23
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>
#include <iostream>

using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
long long n,p,i,st,dr,med,k,s,p1;
int dx[19]={
25,
125,
625,
3125,
15625,
78125,
390625,
1953125,
9765625,
48828125,
244140625,
1220703125};

int main()
{

    fin>>p;
    st=1;
    dr=2100000000;
    while(st<=dr)
    {
        med=(st+dr)>>1;
        p1=5;
        s=0;
        while(p1<=med)
        {
            s+=med/p1;
            p1*=5;
        }
        if(s==p)
        {
            k=med;
            dr=med-1;
        }
        else
            if(s<p)
            st=med+1;
        else
            dr=med-1;
            //cout<<s<<endl;
            s=0;
    }
    if(k)
        fout<<k;
    else
        fout<<-1;
    return 0;
}