Cod sursa(job #788904)

Utilizator dogDaysAreOverAndreea Gheorghe dogDaysAreOver Data 16 septembrie 2012 01:59:47
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>
#include <cmath>

#define inputFile "fact.in"
#define outputFile "fact.out"
#define MAX 100000000

using namespace std;

long long p;

long long countZ(long long end){
	long long res = 0;
	long long localMax = log2(end)/log2(5);

	for(int k=1; k<=localMax; k++){
		res += (floor)((long double) end / pow((double) 5, (double) k));
	}

	return res;
}

int main(){
	ifstream in(inputFile);
	in>>p ;

	long long start = 0;
	long long end = MAX;


	ofstream output;
	output.open(outputFile);
	long long res;

	if(p == 0)
		start = 1;
	else{
		while(start < end){
			int mid = (start + end)/2;
			res = countZ( mid) ;

			if(res >= p)
				end = mid;
			else
				start = mid +1;
		}

		if(countZ(start) != p)
			start = -1;
	}

	output << start << endl;
	output.close();
}