Cod sursa(job #614309)

Utilizator DDeidaraSzasz Tamas Csaba DDeidara Data 5 octombrie 2011 22:35:48
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <fstream>

using namespace std;

int calc(int h);
int binary(int p);
unsigned long p;

int main()
{
	ifstream f ("fact.in");
	ofstream g ("fact.out");
	
	f>>p;
	
	f.close ();
	
	unsigned long sum = binary(p/2);
	
	if (!p)
		g<<"1";
	else if ( sum==0 )
			g<<"-1";
		else 
			g<<sum*5;
	
	return 0;
}

int calc(int h)
{
	unsigned long sum = h;
	while (h>4)
	{
		sum = sum + h/5;
		h = h/5;
	}
	
	return sum;
}

int binary(int n)
{
	unsigned long  q = calc(n);
	if (q == p)
		return n;
	else if (q > p ) 
		return 0;
	else if (q < p ) 
		return binary( (n+p)/2 );
}