Cod sursa(job #186015)

Utilizator NicholasDragos Nicolae Nicholas Data 26 aprilie 2008 15:58:25
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <stdio.h>
long int t,a,b,mij,p,n,i,k,x;
bool ok;
long int putere(long int k)
{
    t=0;
    t=t+(k/5);
    t=t+(k/25);
    t=t+(k/125);
    t=t+(k/625);
    t=t+(k/3125);
    t=t+(k/15625);
    t=t+(k/78125);
    return t;
}    
int main()
{
       freopen("fact.in","r",stdin);
       freopen("fact.out","w",stdout);
       scanf("%ld",&p);
       if (p==0) printf("10");
       else{
       a=0;b=100000000;ok=0;
       do
       {
           mij=(a+b)/2;
           x=putere(mij);
           if (x<p) a=mij+1;
           if (x>p) b=mij-1;
           if (x==p) {ok=1;
                       if ((mij-1)%5==0) mij=mij-1;
                       if ((mij-2)%5==0) mij=mij-2;
                       if ((mij-3)%5==0) mij=mij-3;
                       if ((mij-4)%5==0) mij=mij-4;}   
       }
       while (ok==0 && (a<b));
       if (ok==1) printf("%ld",mij);
                      else printf("-1");}
       fclose(stdin);
       fclose(stdout);
       return 0;
}