Cod sursa(job #615204)

Utilizator DDeidaraSzasz Tamas Csaba DDeidara Data 8 octombrie 2011 21:27:03
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>

using namespace std;

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

int main()
{
	ifstream f ("fact.in");
	ofstream g ("fact.out");
	
	f>>p;
	
	f.close ();
	
	if (!p)
	   g<<"1";
    else
    {
	no = binary(0,2000000000)*5;
	
	if (!no)
	   g<<"-1";
    else g<<no;
    } 
	g.close ();
	
	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 start, unsigned long end)
{
    if (start>end) return 0;
    else {
    unsigned long k = (start + end) /2;
	if (calc(k) == p) return k;
	if (calc(k) < p) return binary (k+1,end);
	else return binary (start,k-1);
         }
}