Cod sursa(job #1875671)

Utilizator xtreme77Patrick Sava xtreme77 Data 11 februarie 2017 13:59:34
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>

using namespace std ;

ifstream cin ("fact.in") ;
ofstream cout ("fact.out") ;

long long NrZero (long long x) {
	long long S = 0 ;
	while ( x / 5LL ) {
		S = S + (x / 5LL) ; 
		x = x / 5LL ;
	}
	return S ; 
}

int main()
{
	long long P ;
	cin >> P ; 
	long long st = 1 ; 
	long long dr = 2e9 ;
	long long found = -1 ; 
	while ( st <= dr ){
		long long mij = (st + dr) >> 1LL ;
		if (NrZero(mij) >= P) {
			dr = mij - 1 ; 
			found = mij ; 
		}
		else {
			st = mij + 1 ;
		}
	}
	if (found == -1 or NrZero(found) != P) {
		cout << -1 << '\n' ;
	}
	else {
		if ( found - found % 5 == 0 ) {
			cout << 1 << '\n' ;
			return 0 ; 
		}
		cout << found - found % 5 << '\n' ;
	}
	return 0 ; 
}