Cod sursa(job #433578)

Utilizator O_NealS. Alex O_Neal Data 3 aprilie 2010 21:30:40
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include<cstdio>
#include<fstream>
using namespace std;

unsigned long long rez=0,p;

int bun(unsigned long long x)
{
	int cont=0;
	unsigned long long  putere=5,xx=x;
	while(x>=putere)
	{
		cont+=(x/putere);
		putere*=5;
	}
	if (cont>p) return 1;
	else if ( cont == p ) 
		{
			rez = xx;
			return 1;
		}
	return 0;
}


int main()
{
	ifstream fin("fact.in");
	//freopen("fact.out","w",stdout);
	ofstream fout("fact.out");
	fin>>p;
	
	unsigned long long s=0,d=4200000000,m;
	if(p==0) { fout<<1; return 0; }
	else
	while(s<=d)
	{
		m=(s+d)>>1;
		if(bun(m)) 
			{
				d=m-1;
			}
		else s=m+1;
	}
	
	//printf("%lld\n",rez);
	if(rez) fout<<rez;
	else fout<<-1;
	return 0;
}