Cod sursa(job #1004276)

Utilizator The_DisturbedBungiu Alexandru The_Disturbed Data 2 octombrie 2013 14:56:47
Problema Factorial Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.57 kb
#include<stdio.h>
long long q,p,st,dr,m,x;
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\n");return 0;}
	st=0;
	dr=20000000;
	while(st!=dr)
	{
		m=(st+dr)/2;
		x=putere(m);
		if(p==x)
		{
			while(m%5!=0)--m;
			printf("%lld\n",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("%d\n",m);return 0;}
	printf("-1\n");
	return 0;
}