Cod sursa(job #938890)

Utilizator kassay_akosKassay Akos kassay_akos Data 14 aprilie 2013 11:54:08
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <iostream> 
#include <stdio.h>
#include <math.h>
using namespace std ;





int main(){
	
	freopen("fact.in","r",stdin);
	freopen("fact.out","w",stdout);
	int n;
	cin >> n ;
	
	if (n < 0) {
		cout << -1 << endl;
	}
	else if (n == 0){		
		cout << 1 << endl;
	}
	else {
		int beg = 0 , end = 500000000 , mij ,k,v;
		while (beg < end) {
			mij = (beg + end )/2 ;
			v = mij ;
			k = 0;
			while ( v > 0){
				k += v/5;
				v /=5;
			}
			if (k == n){
				cout << mij - mij % 5 << endl;
				return 0 ;
			}
			else if (k < n)
				beg = mij +1 ;
			else 
				end = mij - 1 ;
		}
		cout << -1 << endl;
	}
	return 0 ;
}