Cod sursa(job #349594)

Utilizator proflaurianPanaete Adrian proflaurian Data 20 septembrie 2009 13:23:48
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.56 kb
#include<stdio.h>
int p,L,R,M,CL,CR,CM,c5(int u);
void read(),solve();
int main()
{
	read();
	solve();
	return 0;
}
void read()
{
	freopen("fact.in","r",stdin);
	freopen("fact.out","w",stdout);
	scanf("%d",&p);
}
void solve()
{
	if(p==0){printf("1\n");return;}
	L=1;CL=0;
	R=500000000;CR=c5(R);
	while(R-L-1)
	{
		M=(R+L)/2;
		CM=c5(M);
		if(CM>=p)
		{
			R=M;CR=CM;
		}
		else
		{
			L=M;CL=CM;
		}
	}
	CR==p?printf("%d\n",R):printf("-1\n");
}
int c5(int u)
{
	int ret=0;
	while(u)
	{
		ret+=u/5;
		u/=5;
	}
	return ret;
}