Cod sursa(job #471942)

Utilizator igsifvevc avb igsi Data 21 iulie 2010 23:42:19
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>

using namespace std;

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

	for( unsigned long long i = 5; i <= x ; i += 5 )
	{
		unsigned long long temp = i, nrTemp = 0 ;
		while ( temp % 5 == 0 )
		{
			nrTemp++ ;
			temp /= 5 ;
		}
		nr += nrTemp ;
	}

	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;
	
	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;
}