Cod sursa(job #460674)

Utilizator liviu12345Stoica Liviu liviu12345 Data 3 iunie 2010 16:30:58
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>

using namespace std ;

ifstream f ( "fact.in" ) ; 
ofstream g ("fact.out" ) ;

int tintaImpusa ;

int nrZerouri ( int Numar , int Putere ) 
{
	if ( Putere > Numar )
		return 0 ;
	return Numar / Putere + nrZerouri ( Numar , Putere * 5 ) ;
}

int main ( ) 
{
	f >> tintaImpusa ;
	int Left = 0 , Right = 1e+9 ;
	int Mid = ( Left + Right ) / 2 ;
	while ( Left != Mid )
	{
		if ( nrZerouri ( Mid , 5 ) > tintaImpusa )
		{
			Right = Mid ;
			Mid = ( Left + Right ) / 2 ;
			continue ;
		}
		if ( nrZerouri ( Mid , 5 ) < tintaImpusa )
		{
			Left = Mid ;
			Mid = ( Left + Right ) / 2 ;
			continue ;
		}
		if ( nrZerouri ( Mid , 5 ) == tintaImpusa )
			break ;
	}
	while ( Mid % 5 != 0 )
		Mid -- ;
	if ( tintaImpusa == 0 )
	{
		g << 1 << endl ;
		return 0 ;
	}
	if ( nrZerouri ( Mid , 5 ) != tintaImpusa )
	{
		g << -1 << endl ;
		return 0 ;
	}
	g << Mid ;
	return 0 ;
	
}