Cod sursa(job #471944)

Utilizator igsifvevc avb igsi Data 21 iulie 2010 23:49:51
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>

using namespace std;

unsigned long long calculeazaNrZero ( unsigned long long x )
{
	unsigned long long nr = 0;

	while ( x )
	{
		nr += x / 5 ;
		x /= 5 ;
	}

	return nr;
}

int main()
{
	ifstream fin("fact.in");
	ofstream fout("fact.out");
	int p, sw = 1;
	unsigned long long a, b, c, nrZero;

	fin >> p;
	a = 1;
	c = 490000000;
	
	if ( p == 0 )
		fout<< "1\n" ;
	else
	{
	  while(a <= c)
	  {
		b = ( a + c ) / 2;
		nrZero = calculeazaNrZero ( b ) ;

		if( p == nrZero )
			fout << b - b % 5 << '\n', sw = 0, c = a - 1;
		else
            if( nrZero > p )
				c = b - 1;
			else
				a = b + 1;
	  }

	  if ( sw ) fout << "-1\n" ;
	}

	fout.close();
	fin.close();

	return 0;
}