Cod sursa(job #471943)

Utilizator igsifvevc avb igsi Data 21 iulie 2010 23:46:19
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 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;
	
	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;
}