Cod sursa(job #544134)

Utilizator blastoiseZ.Z.Daniel blastoise Data 1 martie 2011 08:57:08
Problema Factorial Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <stdio.h>
#include <string.h>

int v[11];
int N,P,size,i,pow;

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

	ret=0;
	ind=1;

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

	return ret;
}

inline int BS(int L,int R)
{
	int M=(L+R)/2,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("%d",&P);

	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("%d\n",N);

	return 0;
}