Cod sursa(job #352938)

Utilizator chriss16Bota Cristian chriss16 Data 3 octombrie 2009 19:41:34
Problema Factorial Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <stdio.h>

int p,st,dr,mij,m,gasit,nr0;

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

    scanf("%d",&p);
    if(p==0)
        printf("1");
    else
    {
        st=0;
		dr=p*5;

		while(!gasit)
		{
			nr0=0;
			mij=(st+dr)/2;
			m=mij;
			while(m!=0)
			{
			   m/=5;
			   nr0=nr0+m;
			}
			if(nr0==p)
			{
				gasit=1;
				while(nr0==p)
				{
					mij--;
					m=mij;
                    nr0=0;
					while(m!=0)
					{
						m/=5;
						nr0=nr0+m;
					}
				}
			mij++;
			}
			if(nr0<p)
				st=mij;
			if(nr0>p)
				dr=mij;
			if(st>dr)
			{
				gasit=1;
				mij=-1;
			}
		}
		printf("%d",mij);
	}
}