Cod sursa(job #1004295)

Utilizator The_DisturbedBungiu Alexandru The_Disturbed Data 2 octombrie 2013 15:31:27
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include<stdio.h>
long long q,p,st,dr,m,x,i;
long long putere(long long x)
{
	if(x==0)return 0;
	return x/5+putere(x/5) ;
}
int main()
{
	freopen("fact.in","r",stdin);
	freopen("fact.out","w",stdout);
	scanf("%lld",&p);
	if(p==0)
	{
		printf("1");
		return 0;
	}
	st=0;
	dr=100000000;
	while(st!=dr)
	{
		m=(st+dr)/2;
		x=putere(m);
		if(p==x)
		{
			while(m%5!=0)--m;
			if(putere(m)==p)
			{
				printf("%lld",m);
				return 0;
			}
		}
		else
		if(p>x)st=m+1;
		else
		if(p<x)dr=m;
	}
	m=st;
	while(m%5!=0)--m;
	if(putere(m)==p)
	{
		printf("%lld",m);
		return 0;
	}
	printf("-1");
	return 0;
}