Cod sursa(job #602491)

Utilizator SteveStefan Eniceicu Steve Data 11 iulie 2011 17:19:33
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream.h>

long v[13];
int i;

long functie (long T)
{
	long cutzu=0;
	for (i=0; i<12; i++)
	{
		cutzu+=T/v[i];
	}
	return cutzu;
}

long max (long asdf, long ghjkl)
{
	if (asdf>ghjkl) return asdf;
	return ghjkl;
}

int main()
{
	long P;
	long lsup, linf, aux;
	v[0]=5;
	for (i=1; i<12; i++)
	{
		v[i]=5*v[i-1];
	}
	ifstream ins;
	ins.open("fact.in");
	ins>>P;
	linf=max (4*P, 1);
	lsup=(P+12)*4-5;
	ofstream ous;
	ous.open("fact.out");
	while (P != functie(linf))
	{
		aux=(lsup+linf) / 2;
		if (functie(aux) > P)
		{
			lsup=aux;
		}
		else
		{
			linf=aux;
		}
		if (linf+1>=lsup)
		{
			ous<<"-1";
			ous.close();
			return 0;
		}
	}
	ins.close();
	if (linf!=1)
	{
		ous<<linf-linf%5;
		ous.close();
		return 0;
	}
	ous<<"1";
	ous.close();
	return 0;
}