Cod sursa(job #2271567)

Utilizator divianegoescuDivia Negoescu divianegoescu Data 28 octombrie 2018 21:17:38
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
//int v[100000002],i,k;
///400000015 ultimul n
int c;
long long p,s,x,n,st,dr,mid;
int main()
{
    fin>>c;
    if(c==0){fout<<"1";return 0;}
    st=5;
    dr=400000015;
    bool ok=0;
    while(st<=dr)
    {
        mid=(st+dr)/2;
      //  fout<<"mid="<<mid<<"\n";
        x=mid;
        p=5;
        s=0;
        while(x/p>0)
        {
            s+=x/p;
            p*=5;
        }
        if(s==c) {ok=1;break;}
        else if(s<c) st=mid+1;
        else dr=mid-1;
    }
    if(ok==1)
        fout<<mid/5*5;
    else fout<<"-1";
    return 0;
}