Cod sursa(job #727283)
#include<fstream>
#define i64 unsigned long long
using namespace std;
ifstream f("fact.in"); ofstream g("fact.out");
i64 d61=1LL<<61, c[30];
int i, p, nr, w, m, st, dr;
inline int fct(int n)
{int s=0, i=1;
while(c[i]<=n) s+=n/c[i++];
return s;
}
int main()
{ c[0]=1;
while(4*c[i]<d61-c[i]){i++; c[i]=c[i-1]*5;}
f>>p; st=1; dr=p*5;
if(p==0) {g<<"1\n"; w=0;} else w=1;
while((st<=dr) && w)
{m=(st+dr)/2;
nr=fct(m);
if(nr==p) {g<<m-m%5<<"\n"; w=0;}
else if(nr<p) st=m+1; else dr=m-1;
}
if(w) g<<"-1\n";
return 0;
}