Cod sursa(job #778283)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 14 august 2012 13:46:39
Problema Factorial Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
//============================================================================
// Name        : Factorial.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <fstream>
using namespace std;

int get_zero_count(int n) {
	int sum = 0;

	for(int i = 5; i <= n; i *= 5)
		sum += n / i;

	return sum;
}

int bsearch(int target) {

	int low = 1, high = 100000000, mid;

	while(low <= high) {
		mid = (low + high) / 2;

		int zero_count = get_zero_count(mid);

		if(zero_count > target)
			high = mid - 1;
		else if(zero_count < target)
			low = mid + 1;
		else
			return mid;

	}

	return -1;
}

int main() {

	int p;

	ifstream fin("fact.in");
	ofstream fout("fact.out");
	fin >> p;

	if(p == 0) {
		fout << 1;
		return 0;
	}
	int k = bsearch(p);

	if(k % 5)
		fout << k - k % 5;
	else
		fout << k;


	fin.close();
	fout.close();



	return 0;
}