Cod sursa(job #1571462)

Utilizator claudiuarseneClaudiu Arsene claudiuarsene Data 18 ianuarie 2016 09:35:00
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.46 kb
#include <cstdio>
#define NMax 500000001

int zero(int x)
{ int i=5,s=0;
while(x/i)
{ s+=x/i; i*=5; }
return s;
}

int main(){
    freopen("fact.in","r",stdin);
    freopen("fact.out","w",stdout);

int n,ok,mid,st,dr,i,p;
scanf("%d",&p);

for(st=1,dr=NMax,ok=0;st<=dr;)
{ mid=(st+dr)/2;
    if(zero(mid)>p) dr=mid-1;
    else if(zero(mid)<p) st=mid+1;
    else { ok=1; n=mid; dr=mid-1; }
}
if(!ok) printf("-1");
else printf("%d",n);

return 0;
}