Cod sursa(job #614322)

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

using namespace std;

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

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

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

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