Cod sursa(job #544141)

Utilizator blastoiseZ.Z.Daniel blastoise Data 1 martie 2011 09:09:45
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <stdio.h>
#include <string.h>

long v[20];
long N,P,size,i,pow;

inline long f(long N)
{
	long ind,ret;

	ret=0;
	ind=1;

	while(v[ind]<=N&&ind<=size)
		ret+=N/v[ind++];

	return ret;
}

inline long long BS(long long L,long long R)
{
	long long M=(L+R)/2;
	long x=0;

	if(L<=R)
	{
		x=f(M);

		if(x>P) return BS(L,M-1);
		else
		if(x<P) return BS(M+1,R);
		else return M;
	}
	else return -1;
}

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

	scanf("%ld",&P);

	if(P==0)
	{
		printf("1\n");
		return 0;
	}

	size=0;
	pow=5;

	while(pow<=P*5)
	{
		v[++size]=pow;
		pow*=5;
	}

	N=BS(1,P*5);

	if(N>1)
		while(f(N-1)==P) N--;

	printf("%ld\n",N);

	return 0;
}