Cod sursa(job #614307)

Utilizator DDeidaraSzasz Tamas Csaba DDeidara Data 5 octombrie 2011 22:34:57
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <fstream>

using namespace std;

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

int main()
{
	ifstream f ("factorial.in");
	ofstream g ("factorial.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 );
}