Cod sursa(job #328351)

Utilizator Anonymous1010Chilivercu Cristian Anonymous1010 Data 1 iulie 2009 19:03:04
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include<stdio.h>

long p;

void citeste();
long cauta(long,long);
long nrz(long);

int main()
{
	freopen("fact.out","w",stdout);
	long a,b,ok;
	citeste();
	if(!p)
		printf("1");
	else
		if(p==1)
			printf("0");
		else
		{
			a=0;
			b=1400000000;
			ok=cauta(a,b);
			if(ok==-1)
				printf("-1");
			else
            	printf("%ld",ok-ok%5);
		}
	return 0;
}
void citeste()
{
	freopen("fact.in","r",stdin);
	scanf("%d",&p);
}
long cauta(long x,long y)
{
	long z;
	if(x==y)
		return -1;
	else
	{
		z=nrz((x+y)/2);
		if(z==p)
			return ((x+y)/2);
		else
		{
			if(z<p)
				return cauta(x,((x+y)/2));
			else
				return cauta(((x+y)/2)+1,y);
		}
	}
}
long nrz(long nr)
{
	long i,aux=0,bux;
	for(i=5;i<=nr;i+=5)
	{
		bux=i;
		while(bux%5==0)
		{
			aux++;
			bux/=5;
		}
		if(aux>p)
        	break;
	}
	return aux;
}