Cod sursa(job #625269)

Utilizator thesvcoolmanLucian Bicsi thesvcoolman Data 24 octombrie 2011 09:46:55
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
using namespace std;
#include<fstream>
ifstream fin("fact.in");
ofstream fout("fact.out");
int zero (int a)
{
	int s=0,p=5;
	while(a>=p)
	{
		s=s+a/p;
		p=p*5;
	}
	return s;
}
int main()
{
	int N,a,k=5,c1,c2;
	fin>>N;
	if(N==0)
	{
		fout<<"1";
		return 0;
	}
	while((k-1)/4<=N)
		k=k*5;
	k=k/5;
	c1=(k-1)/4/5;
	c2=N;
	int gata=0;
	while(c1<c2&&!gata)
	{
		a=(c1+c2)/2;
		if(zero(a*5)<N)
			c1=a+1;
		else
			if(zero(a*5)>N)
				c2=a-1;
			else
			{
				fout<<a*5;
				gata=1;
			}
	}
	if(!gata)
		if(zero(c1*5)==N)
			fout<<c1*5;
		else
			fout<<"-1";
	return 0;
}