Cod sursa(job #141110)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 22 februarie 2008 19:11:56
Problema Factorial Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include<fstream.h>
unsigned long n, np;
unsigned long x[13]={0, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625};
ifstream f("fact.in");
ofstream g("fact.out");

int factorial(int n)
{	unsigned long nr, i;
	for(i=12; i>0; i--)
		nr+=n/x[i];
	return nr;
}

int binara(unsigned long p, unsigned long u)
{	unsigned long mj;
	if(p<=u)
	{	mj=(p+u)/2;
		if(factorial(mj)==np)
			return mj;
		else
			if(factorial(mj)>np)
				return binara(p, mj-1);
			else
				return binara(mj+1, u);
	}
	else
		return -1;
 }

int main()
{	f>>np;
	if(np==0)
		g<<1;
	else
	{   	n=binara(1, 4000000000);
		n=n-n%5;
		g<<n;
	 }
	 g.close();
	 return 0;
 }